MING KEAT LIM
MING KEAT LIM

Reputation: 29

Pass each value into array

I currently stuck on storing value to an array.

Below is my progress.I would like to store each random generated number into an array.

I just created a new function prototype where supposedly,it shall read each of the generated number and store in its array.

#include<iostream>

using namespace std;

//Function prototype
void random(int val);
void store(int val1);


int main()
{
    int nvalue;
    cout << "How many numbers would you like to generate?\n";
    cin >> nvalue;//get input from user
    random(nvalue);//pass user input into random() function

    system("pause");
    return 0;
}

void random(int val)
{
    int num;//represent random integer output
    for (int i = 1; i < val; i++)//loop that will continue to generate integers based on n value from user
    {
        num = rand() % val + 1;//randomly generate number
        cout << "Num [" << i << "]" << "is " << num<<endl;
    }
}

Upvotes: 0

Views: 94

Answers (1)

ars
ars

Reputation: 707

here is example of implementation of what you need:

void random_store(int val, vector<int> &aVec);
int main()
{
   int nvalue;
   cout << "How many numbers would you like to generate?\n";
   cin >> nvalue;//get input from user

   vector<int> int_vector;
   random_store(nvalue, int_vector);//pass user input into random() function

   system("pause");
   return 0;
}
void random_store(int val, vector<int> &aVec)
{
   int num;//represent random integer output
   for (int i = 0; i < val; i++)
   {
      aVec.push_back(rand() % val + 1);
      cout << "Num [" << i << "]" << "is " << aVec[i] <<endl;
   }
}

Upvotes: 3

Related Questions