473,473 Members | 1,902 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Strange Infinite Loop in My Code

Hello. I'm working on some simple code to generate a list of random
integers and store them in a 50 element array. I have two conditions
that I'm checking, first, that i < length(array) and second, that no
random integer a[i] > random integer a[i+1].

My problem is that my loop won't exit. i will continue to be
iterated, but is reset to 0 once the exit condition should be met,
rather than exiting the while loop. I've ran this through gdb, but
haven't figured out the reason.

Here's the code in question:

i = 0;
randNum = rand() % size +1;
array[i] = randNum;

while (i < size){
randNum = rand() % size;
while (randNum > array[i])
randNum = rand() % size;
i++;
array[i] = randNum;
}

I've noticed that deleting the assignment of a[i] = randNum at the end
of the while loop fixes this, but unfortunately, that's critical to
the sorting. If anyone has any ideas, I'd really appeciate it. I'll
toss a break condition in if I have to, but I'd prefer to figure out
what's causing this in the first place.

Thanks in advance

Morgan
Jul 22 '05 #1
3 1451
"Morgan Wolfe" <wo***@cecs.pdx.edu> wrote...
Hello. I'm working on some simple code to generate a list of random
integers and store them in a 50 element array. I have two conditions
that I'm checking, first, that i < length(array) and second, that no
random integer a[i] > random integer a[i+1].

My problem is that my loop won't exit. i will continue to be
iterated, but is reset to 0 once the exit condition should be met,
rather than exiting the while loop. I've ran this through gdb, but
haven't figured out the reason.

Here's the code in question:

i = 0;
randNum = rand() % size +1;
array[i] = randNum;

while (i < size){
randNum = rand() % size;
while (randNum > array[i])
randNum = rand() % size;
i++;
array[i] = randNum;
Here is your problem. Imagine that 'i' is (size-1). It will
still execute it one more time, right? So, right here it will
make 'i' == 'size', and attempt to access an element _beyond_
the last one of the 'array' (if 'size' here denotes how many
elements the 'array' has).

Perhaps doing

while (i < size - 1)

will fix it...
}

I've noticed that deleting the assignment of a[i] = randNum at the end
of the while loop fixes this, but unfortunately, that's critical to
the sorting. If anyone has any ideas, I'd really appeciate it. I'll
toss a break condition in if I have to, but I'd prefer to figure out
what's causing this in the first place.

Thanks in advance

Morgan

Jul 22 '05 #2
>
Hello. I'm working on some simple code to generate a list of random
integers and store them in a 50 element array. I have two conditions
that I'm checking, first, that i < length(array) and second, that no
random integer a[i] > random integer a[i+1].

My problem is that my loop won't exit. i will continue to be
iterated, but is reset to 0 once the exit condition should be met,
rather than exiting the while loop. I've ran this through gdb, but
haven't figured out the reason.

Here's the code in question:

i = 0;
randNum = rand() % size +1;
array[i] = randNum;

while (i < size){
randNum = rand() % size;
while (randNum > array[i])
randNum = rand() % size;
i++;
array[i] = randNum;
}

I've noticed that deleting the assignment of a[i] = randNum at the end
of the while loop fixes this, but unfortunately, that's critical to
the sorting. If anyone has any ideas, I'd really appeciate it. I'll
toss a break condition in if I have to, but I'd prefer to figure out
what's causing this in the first place.

Thanks in advance

Morgan

If you insert some output statements in your loops it sheds some light. The
inner loop continues until random number is zero, I don't think that's what
you're after.
I think your best approach would be to store random numbers in the array, and
then sort the array.
Jul 22 '05 #3
Hello. I'm working on some simple code to generate a list of random
integers and store them in a 50 element array. I have two conditions
that I'm checking, first, that i < length(array) and second, that no
random integer a[i] > random integer a[i+1].


One problem is that you access array[i] after incrementing i up to the value
size, which goes beyond your array, causing undefined behavior.

But I'm not sure I understand what you really want in the first place. The
statements above indicate that you want an array of increasing random
values. But your code creates an array of decreasing values! Also, your
code limits the maximum value of the random number to the size of the array.
Is that a requirement also? Given the requirment above, you'll end up with
something like this:

13,20,34,34,41,49,49,49,49,49,49......

Or if you do it descending like your code shows:

41,34,34,20,13,0,0,0,0,0.....

In either case, you approach has no control over how fast the random value
approaches the limit (whether that's 0 or size). You should probably be
writing random numbers to the array and then sorting, unless this is the
kind of distribution that you want (which is not how you've stated the
problem).

-Howard


Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

43
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a...
2
by: Dave | last post by:
I'm crossposting this to both comp.lang.c++ and gnu.gcc because I'm not sure if this is correct behavior or not, and I'm using the gcc STL and compiler. When calling vector<int>::push_back(0),...
6
by: chandra.somesh | last post by:
I am having trouble understanding why the code given belows enters an infinite loop when a char is entered instead of an int. i.e.on subsequent looping ,control doesn't wait for user input and just...
5
by: mailpitches | last post by:
Hello, Is there any way to kill a Javascript infinite loop in Safari without force-quitting the browser? MP
4
by: LOPEZ GARCIA DE LOMANA, ADRIAN | last post by:
Hi all, I have a question with some code I'm writting: def main(): if option == 1: function_a()
6
by: Joe Piscapo | last post by:
Hello, When I compile my program in Visual Studio it does not work for every input, some numbers make it crash. But compiling in gcc works for those numbers that made it crash in Visual Studio. ...
11
by: Martin Joergensen | last post by:
Hi, I've encountered a really, *really*, REALLY strange error :-) I have a for-loop and after 8 runs I get strange results...... I mean: A really strange result.... I'm calculating...
10
by: Steven Woody | last post by:
i have a program which always run dead after one or two days, i think somewhere a piece of the code is suspicious of involving into a infinite loop. but for some reason, it is very hard to debug....
44
by: James Watt | last post by:
can anyone tell me how to do an infinite loop in C/C++, please ? this is not a homework question .
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.