Printing Elements of a Vector Forwards and Backwards

Click For Summary

Discussion Overview

The discussion revolves around writing a for loop to print elements of a vector in both forward and backward order. Participants are focused on coding challenges related to the implementation of loops in C++ and the specific conditions required for proper output.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant provides a sample program and asks for help with the condition for the second loop to print the vector in reverse.
  • Another participant expresses confusion about the second loop only outputting one number and seeks suggestions for fixing it.
  • A participant suggests using the same range of values for the index in the second loop but adjusting the index to access the elements in reverse order.
  • One participant notes that they can only modify the "" section and asks for advice under that constraint.
  • A later reply proposes a corrected version of the second loop, indicating that the index should decrease to print elements backwards.

Areas of Agreement / Disagreement

Participants generally agree on the need to adjust the loop conditions but have not reached a consensus on the exact implementation details, as some are still struggling with the logic of the second loop.

Contextual Notes

Some participants mention logical issues with their current implementations, indicating a need for clarity on loop conditions and index management without resolving the specific coding errors.

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:

Similar threads

Replies
2
Views
5K
  • · Replies 3 ·
Replies
3
Views
7K
Replies
1
Views
5K
  • · Replies 15 ·
Replies
15
Views
4K
Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K