I have an assignment I have been working on and am not too sure where to go from here: (my problems are at the bottom of post)
"Write a function that assigns a random value to the first n elements of the array x using "void assignRandom(double *x, intn);". You must enter n from the keyboard adn display the one-dimensional array."
This is what I have so far and I have no idea which way to go. Or even how close I am.
#include <iostream>
#include <iomanip>
using namespace std;
void assignRandom(double *x, int n); // the function declaration (prototype)
int main ()
{
int n;
cout << "please enter size of array" << endl;
cin >> n;
cout << assignRandom << endl;
system ("pause");
return 0;
}
/////////////////////////~~~~Assign Random Array~~~~//////////////////////////
//
void assignRandom(double *x, int n) //
{ //
//
int array [n]; //
srand(time(NULL)); // tells array to choose random number //
//
for (int x=0; x<8; x++) //
{ //
array[x]=rand()%100 + 1; //inserts rand # into array //
cout << setw(4) << array [x]; //
} //
cout<< endl << endl; // this is important there because //
// it prints out after each line telling //
// the loop to start on a new line. //
} //
//
//////////////////////////////////////////////////////////////////////////////
This program actually runs but when I enter in the number for the array size, no matter what number I put in, it prints out the number 1 and that is all. Any advice would be greatly appreciated.
There's your problem. The function doesn't return anything, AND you forgot the parameters. You can't possibly output the return if the function didn't return anything.
assignRandom(&some_variable,n); // Don't forget those parameters!
#include <iostream>
#include <iomanip>
usingnamespace std;
void assignRandom( int n); // the function declaration (prototype)
int main ()
{
int n = 0;
cout << "please enter size of array" << endl;
cin >> n;
assignRandom();
system ("pause");
return 0;
}
/////////////////////////~~~~Assign Random Array~~~~//////////////////////////
//
void assignRandom( int n) //
{ //
//
int array [n]; //
srand(time(NULL)); // tells array to choose random number //
//
for (int x=0; x<n; x++) //
{ //
array[x]=rand()%100 + 1; //inserts rand # into array //
cout << setw(4) << array [x]; //
} //
cout<< endl << endl; // this is important there because //
// it prints out after each line telling //
// the loop to start on a new line. //
}