C/C++ Printing Elements of a Vector Forwards and Backwards

AI Thread Summary
The discussion focuses on writing a for loop to print elements of a vector, `courseGrades`, both forwards and backwards. The initial code provided correctly prints the elements in the forward direction, but there is confusion regarding the condition for the second loop intended to print the elements in reverse. Participants highlight that the second loop should start with `i = NUM_VALS - 1` and continue while `i >= 0`, decrementing `i` with each iteration. This setup allows for the correct backward printing of the vector's elements. Suggestions emphasize maintaining the same variable `i` for both loops and adjusting the loop condition to ensure all elements are printed correctly. The final solution provided demonstrates the correct implementation of both loops, ensuring the output format includes a space after each number and ends with a newline.
ineedhelpnow
Messages
649
Reaction score
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
ineedhelpnow said:
what should my condition be for the second loop?

What kind of condition can you come up with? (Wondering)
 
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?
 
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.
 
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;
 
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:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top