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:
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top