Printing Elements of a Vector Forwards and Backwards

In summary, the code uses two for loops to print the elements of the vector courseGrades in both forward and backward order, separated by spaces and ending with a newline. The first loop starts at i = 0 and ends at i = NUM_VALS - 1, while the second loop starts at i = NUM_VALS - 1 and ends at i = 0, using the value of i to access the elements of courseGrades in reverse order.
  • #1
ineedhelpnow
651
0
Write a for loop to print all NUM_VALS elements of vector courseGrades, following each with a space (including the last). Print forwards, then backwards. End with newline. Ex: If courseGrades = {7, 9, 11, 10}, print:

7 9 11 10
10 11 9 7

Hint: Use two for loops. Second loop starts with i = NUMVALS - 1.Sample program:

Code:
#include <iostream>
#include <vector>
using namespace std;

int main() {
   const int NUM_VALS = 4;             
   vector<int> courseGrades(NUM_VALS); 
   int i = 0;                         

   courseGrades.at(0) = 7;
   courseGrades.at(1) = 9;
   courseGrades.at(2) = 11;
   courseGrades.at(3) = 10;

   <STUDENT CODE>

   return 0;
}
this is what i came up with
Code:
for(i=0;i<NUM_VALS;++i) {
    cout<<courseGrades.at(i)<<" ";
}
cout<<endl;
for(i=NUM_VALS-1;?;++i){
      cout<<courseGrades.at(i)<<" ";
}
cout<<endl;
what should my condition be for the second loop?
 
Technology news on Phys.org
  • #2
ineedhelpnow said:
what should my condition be for the second loop?

What kind of condition can you come up with? (Wondering)
 
  • #3
I am also having trouble with this problem everything works until the second for loop. The issue is the second loop only outputs one number, which is the ten. I see the logical issue, but do not know how to make it work. Any suggestions?
 
  • #4
Swag said:
I am also having trouble with this problem everything works until the second for loop. The issue is the second loop only outputs one number, which is the ten. I see the logical issue, but do not know how to make it work. Any suggestions?
You could keep the same range of values for i as in the first loop. Then use it to output courseGrades.at(j), where j = NUM_VALS - 1 - i.
 
  • #5
Hmmm, we are only aloud to change the "<student code>" section. Also, I meant to put my code in before, but I guess it didn't work. What would you suggest if I can only add code to the "<student code>" section and cannot initialize a new variable? @opalg
Code:
for (i = 0; i < NUM_VALS; i++) {
      cout << courseGrades.at(i) << " ";
     
   }
   cout << endl;
   
   for (i = NUM_VALS - 1; i < NUM_VALS; ++i) {
      cout << courseGrades.at(i) << " ";

   }
cout << endl;
 
  • #6
You are decreasing the value of i because you are trying to write the numbers backwards, so this should work

Code:
for (i = 0; i < NUM_VALS; i++) {
      cout << courseGrades.at(i) << " ";
     
   }
   cout << endl;
   
   for (i = NUM_VALS - 1; i >= 0; i--) {
      cout << courseGrades.at(i) << " ";

   }
   cout << endl;
 
Last edited by a moderator:

FAQ: Printing Elements of a Vector Forwards and Backwards

What is a vector?

A vector is a mathematical object that represents a quantity that has both magnitude and direction. In programming, a vector is a data structure that stores a collection of elements in a specific order.

How do you print elements of a vector forwards?

You can use a for loop to iterate through the vector and print each element in order. Alternatively, you can use the built-in print() function to print the entire vector at once.

How do you print elements of a vector backwards?

You can use a for loop to iterate through the vector in reverse order and print each element. You can also use the reverse() function to reverse the order of the vector and then print it.

What is the time complexity of printing elements of a vector forwards and backwards?

The time complexity of printing elements of a vector forwards and backwards is O(n), where n is the number of elements in the vector. This means that the time it takes to print the elements is directly proportional to the size of the vector.

Can you print specific elements of a vector?

Yes, you can print specific elements of a vector by accessing them using their index. In most programming languages, the index of the first element in a vector is 0, and the index of the last element is one less than the size of the vector.

Similar threads

Back
Top