Arrays-this gives an error saying that "passing NULL to non-pointer argument 1 of ‘void srand(unsigned int)’

#include <iostream>
using namespace std;

void arrTest()
{
int arr[6];
srand(NULL);
for(int i = 0; i < 6; i++)
{
int r = rand() % 99;
if(r >= 10 && r % 2 == 0)
{
arr[i] = r;
cout << arr[i] << endl;
}
}
}

int main ()
{
arrTest();

return 0;
}
Don't pass NULL to srand(). I think what you were going for was srand(time(NULL));
yea it compiled but i want it to print 6 random numbers, however it only printed 2 random numbers.
What you might consider doing is not advancing i unless the if's condition is met (in other words moving the i++ into the if statement).

The way it is now, the loop will go exactly 6 times regardless of whether or not you got a random number that you wanted. If you move i++ into the if, it will keep going until you have six random numbers that you can use.

Advance warning, be careful where in the if you put the i++. ;)

-Albatross
#include <iostream>
using namespace std;

void arrTest()
{
int arr[6];
srand(time(NULL));
int i=0;
while(i<6)
{
int r = rand() % 99;
if(r >= 10 && r % 2 == 0)
{
i++;
arr[i] = r;
cout << arr[i] << endl;
}
}
}
int main ()
{
arrTest();

return 0;
}


I changed it up a bit and my output was 6 random numbers however one of them wasnt a two digit number, it was like a 10 digit number
Albatross
Advance warning, be careful where in the if you put the i++. ;)

It's like you knew Albatross...

You are incrementing the i before using it, so you are storing the last number out of bounds, put i++ after you make all calls to it.

1
2
3
4
5
6
... {
  //i++;
  arr[i] = r;
  cout << arr[i] << endl;
  i++; //<- here
} ...
haha thanks got it now, and thanks to albatross as well
Topic archived. No new replies allowed.