I need to fill an array with random numbers 1-10 using a function call to generate the random numbers
#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std;
int randNumGen(int);
int main()
{
int randNum;
int counter = 0; //counter top a count on random numbers
int myArray[11]; //array size declaration
srand(time(0));
for (int i = 0; i < 11; i++) //for loop to set parameters for array
{
int myArray[i] = randNumGen(randNum); //fills array with random number function
}
for (int i = 0; i < 11; i++)
{
cout << randNum << " ";
}
system("pause");
return 0;
}
int randNumGen(int randNum)
{
int ranNum;
static bool init = false;
include <iostream>
#include <ctime>
#include <iomanip>
usingnamespace std;
int randNumGen(int);
int main()
{
int randNum;
int counter = 0; //counter to keep a count on random numbers
int myArray[11]; //array size declaration
srand(time(0));
for (int i = 0; i < 11; i++) //for loop to set parameters for array
{
int myArray[i] = randNumGen(randNum); //fills array with random number function
}
for (int i = 0; i < 11; i++) //parameters for printing out the array
{
cout << randNum << " "; //Array outputs the random numbers
}
system("pause");
return 0;
}
int randNumGen(int randNum)
{
int ranNum; //holds random number
staticbool init = false; //used to insure a random number "seed"
if (!init)
{
srand(time(NULL));
init = true;
}
randNum = (rand() % 10) + 1; //generates random number between 1 and 10
cout << randNum << ""; //prints out random number
return (randNum);
}
@LB most people are just following instructions from their universities and they just have to use srand, but its nice that you mention the random header since its much better, and if OP is just coding to learn he should probably pick it up.
@OP, please be more specific in your next post about whats actually wrong, if its not compiling, if its wrong output etc. In this case...
int randNum; // initialize it to 0
int myArray[i] = randNumGen(randNum);
Here you're not actually filling the array, but instead you're recreating an array all the time, which doesnt work becuase "i" is not a constant value. So you want to use the array you already created. Meaning remove the "int" part of it so it looks like this
myArray[i] = randNumGen(randNum);
And lastly,
1 2 3 4
for (int i = 0; i < 11; i++)
{
cout << randNum << " ";
}
If you fill the array with random numbers, why are you not printing out the outputs of the array, but rather a normal variable? Change that to
1 2 3 4
for (int i = 0; i < 11; i++)
{
cout << myArray[i] << " ";
}
Now you're printing out the contents of the array.
int main()
{
int randNum = 0;
int counter = 0; //counter to keep a count on random numbers
int myArray[11]; //array size declaration
srand(time(0));
for (int i = 0; i < 11; i++) //for loop to set parameters for array
{
myArray[i] = randNumGen(randNum); //fills array with random number function
}
for (int i = 0; i < 11; i++) //parameters for printing out the array
{
cout << myArray[i] << " "; //Array outputs the random numbers
}
cout << endl;
system("pause");
return 0;
}
int randNumGen(int randNum)
{
int ranNum; //holds random number
staticbool init = false; //used to insure a random number "seed"
if (!init)
{
srand(time(NULL)); // remove this its not needed.
init = true;
}
randNum = (rand() % 10) + 1; //generates random number between 1 and 10 //prints out random number
return (randNum);
}