By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,331 Members | 1,438 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,331 IT Pros & Developers. It's quick & easy.

Strange Infinite Loop in My Code

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
"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

P: n/a
>
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.