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
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