Strange Infinite Loop in My Code

M

Morgan Wolfe

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 > 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 = randNum;

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

I've noticed that deleting the assignment of a = 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
 
V

Victor Bazarov

Morgan Wolfe said:
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 > 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 = randNum;

while (i < size){
randNum = rand() % size;
while (randNum > array)
randNum = rand() % size;
i++;
array = 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 = 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
 
J

jbruno4000

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 > 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 = randNum;

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

I've noticed that deleting the assignment of a = 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.
 
H

Howard

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 > random integer a[i+1].


One problem is that you access array 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,150
Messages
2,570,853
Members
47,394
Latest member
Olekdev

Latest Threads

Top