Looping through int arrays in C???

  • Thread starter Thread starter Eric.
  • 1 comments
  • 625 views

Eric.

Premium
Messages
8,515
Messages
GTP_Eric
Messages
Ebiggs
This just brought the work on my programming assignment to a halt.

One of the basic things to do in the assignment was to create 4000 normally distributed numbers (the program was to simulate a page replacement algorithm) from 0-39. The function call to return the integer in that range is called "get_rand_int()". The problem with the code lies in this section:

Code:
int page_ref[4000] = {0};
int i;
for(i = 0; i == 399; i++){
page_ref[i] = get_rand_int();}

This should work just fine in Java. But C does not allow me to assign values within the int array if its done from a loop.

Code:
page_ref[5] = get_rand_int();
That will work when called directly by main, not in a loop.


How are you supposed to handle this?
 
You may wish to take this part:

Code:
for(i = 0; i == 399; i++){

and change it to this:
Code:
for(i = 0; i [b]<=[/b] 399; i++){

(I doubt that works as is in Java either, btw).

Otherwise it should work fine, as in, the assignment should succeed. The trick is to do it 400 (or 4000, shouldn't that 399 be 3999 too?) times, not just once as currently coded.
 
Back