don't understand vectors

This was the assignment i was give i don't understand what i am suppose to do here can anyone explain in a pseudo code method please.

//Input: Get the numbers of numbers from user allow user to store numbers in a vector

//Process: Double the size of the vector put the squares of the vector in reverse order(using loops)

//copy vector into temporary vector

//Then put the squares of the vector in reverse order also the vector

//Ouput:

//print the vector

//Sample --> 3 5 8 4 2 11
//< 121 4 16 64 25 9 11 2 4 8 5



it might seem obvious to you guys but please explain. i am a slow learner
Last edited on
I'll try to help. You need to create 2 vectors of int type (one for the numbers and one for the numbers squared). Then you could make a void function to square the numbers. Prompt the user for a set amount of numbers. Set up a for loop to read the numbers into the both of the vectors. After that, set up 2 for loops that will output the lists backwards. I'm willing to help more. I just wrote the program real quick, but I'm assuming you want a push in the right direction, not the answer.
Hi, first of all thanks for helping me, this is all i have so far.am i going in the right direction? Also how am i suppose to create the reversing for loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <vector>

using namespace std;


int main(){
int aofnum;

cout<<"Enter the amount of numbers";
cin>>aofnum;



int squared=aofnum*aofnum;




vector<int>num(aofnum);
vector<int>numsq(squared);






for(int i=0; i<aofnum; i++){

  num.resize(aofnum);
  num.resize(squared);




cout<<squared<<" "<<aofnum;


}




}
We will get you through this. First of all, line 15: You don't need to square the amount of numbers. You need to square the numbers that the user inputs later on in the program.

And about the function: I was trying to lean you towards making a simple void function to square the numbers.
(There are actually a good many ways you could get the squaring done, but since you're learning vectors now, I feel like you must have covered user defined functions recently.)



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
#include<vector>

using namespace std;

void squareNum(int&);  // prototype for function to square num of int data type

vector<int> numbers;       // vector to hold numbers input by user
vector<int> numsSquared;  // vector to hold numbers squared

int main()
{

int amount;  // stores the users desired amount of numbers 
int num;      //  receives the numbers input by user and passes them to vectors via .push_back(num);

} // end main  



As for Reversing the loop, I want you to think about how arrays/vectors work.
Just write down in words what you need to loop to do, and then make it happen.
Start by thinking about the variables you are using. Remember how you prompted the user for the amount of numbers they wanted to work with?

(Its the "int amount" in my code). So we say, "The amount of times this loop needs to cycle = the amount of numbers the user asked for."

You kind of answered this question yourself when you typed the word "reversing". Well, we used i++ to go frontwards. So we should do what to i to do the opposite?
Last edited on
I am still stuck on reversing i don't know whats wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <vector>

using namespace std;


void squareNum(int&);

vector<int>numbers;
vector<int>numsSquared;



int main(){


int amount;
int num;
int sq;




cout<<"Enter amount: ";
cin>>amount;





for(int j=0; j<amount; j++){


cout<<"Enter number: ";

cin>>num;
sq=num*num;







numbers.push_back(num);
numsSquared.push_back(sq);





for(int j=0; j>=amount; j--){



cout<<sq;




}






}


}
in your second for loop, you need j to start at j = amount-1 and end up at j>=0 .

for(j = amount-1; j >=0; j-=1)
Last edited on
When executing the loop its only showing the result for the last value entered.
can you post the program i have been trying for a while and i am not figuring it out.
I went back and read your original post better than I did the first time and I see that I didn't really do what pseudo code tells you to, but maybe something within my code will help you out.

This is what I ended up with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<iostream>
#include<vector>


using namespace std;

vector<int> numbers;
vector<int> numbersSquared;

void square(int&);

int main()

{
    int amount, num;
    
    cout << "Enter the amount of numbers you wish to use: \t";
    cin >> amount;
    
    cout << "\nEnter " << amount << " number(s): \t";
    
    for(int i = 0; i < amount; i+=1)
    {
          cin >> num;
          numbers.push_back(num);
          square(num);
          numbersSquared.push_back(num);         
          
          }
          
          
    for(int i = amount-1; i >= 0; i-=1)
      cout << numbersSquared[i] << " ";
      
      for(int i = amount-1; i >= 0; i-=1)
      cout << numbers[i] << " ";
          
          
          

return 0;

}

void square(int& x)
{
     x = x*x;
}          
          
          
          
Last edited on
Topic archived. No new replies allowed.