I have tried to fill an array with randoms numbers between zero and nine. The code I have used so far is this.

int a[40], b[40];
for (int i=0; i<39; i++)
{
a=(0 rand() % 9);
b=(0 rand() % 9);
}


Why will this not work?

First off you are %ing by the wrong number rand()%y will generate a random number between 0 and y-1. Second your loop is only filling in 39 of the 40 values you have allocated to your arrays. Third (0 rand() % 9) will give you an error because there is no operator between 0 and rand(). Fourth although its not necessary, you should seed the random number generator with srand(uint) where the seed given is normally time(NULL) to pass the current time so that each run of the program generates new numbers based off of the current time. This last part would require you to include time.h also.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.