Where to store the variables in this problem?

Hi guys,
I have a task where I have to ask a user to enter a number between 1-10 and I have to ask if he wants to enter another number (Y for yes, N for no) , if its yes, The user keeps entering numbers.

When the user says N, then I have to ask the user to enter a set of numbers of the same amount of the last numbers. Once that is done, I have to multiply each number of the new set with the corresponding value of the old set. So for example, (1st number of set A * first number of set B) etc and display the result of each multiplication.

What I thought of was to add an if Y then cin>>a (which is an int) , but the problem is that there can be infinite amount of these variables, so I don't really know where to store them, if anyone has any ideas or tips on what to do, I would be really appreciated.
is this a homework assignment? If not then i would use a vector / linked stack to hold each variable.

1
2
3
4
5
6
//push back var into vector/stack everytime user enters y
//push back var into second vector/stack everytime user enters y

//loop through size of stack/vector
   //pop off n from each stack/vector, multiply and show result
//n is incremented until reaching size 
vector:

http://cplusplus.com/reference/stl/vector/


A vector is basically a resizable array:


1
2
3
4
5
6
7
8
9
10
11
12
13
#include <vector>

...

std::vector<int> myvec;  // myvec starts with 0 elements

myvec.push_back( 10 );  // now it has one element:  { 10 }
myvec.push_back( 14 );  // now it has two:  { 10, 14 }
myvec.push_back( 6 );  // { 10, 14, 6 }

std::cout << myvec.size();  // prints '3' because there are 3 elements

std::cout << myvec[1]; // prints '14', the element at index '1' 
Yes, this is a homework assignment.

We never did any vectors in class, but thank you, I'll see the page and try to do it. :)

@ Amishuchak, I must admit these two forum members are pros and know more than I do so I suppose their advice is good.
However, if your class have not yet reached vectors, not sure if you should jump to use the above model for your solution (in this C++ world, you move from stage 1 to 2)?

Have you done arrays? If yes, perhaps you can use arrays instead

E.g. with regards to your first set of data (A)

Do something along these lines:

1
2
3
4
5
6
7
8
9
10
int a[10];

cout<<"Enter random numbers 1-10\n";
for (int i =0; i<10; i++)
cin> a[i];

cout<<"This is the array:"<<endl;
for (int i=0; i<10; i++)
cout<< a[i] << endl;

Do same for second set of numbers (I will leave you to figure out how to do the Yes/No- I am sure you will know how to use the while loop to do that)

Then if you know array calculation, multiply the elements in set a and set b?

Just my humble suggestion
Topic archived. No new replies allowed.