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

Click For Summary
SUMMARY

The discussion focuses on printing elements of a vector in C++ both forwards and backwards using two for loops. The vector, named courseGrades, is initialized with four integer values. The first loop correctly prints the elements in order, while the second loop must iterate in reverse, starting from NUM_VALS - 1 down to zero. The correct condition for the second loop is i >= 0, ensuring all elements are printed in reverse order.

PREREQUISITES
  • Understanding of C++ syntax and structure
  • Familiarity with vectors in C++
  • Knowledge of for loop mechanics
  • Basic debugging skills in C++
NEXT STEPS
  • Learn about C++ vector operations and methods
  • Explore advanced for loop techniques in C++
  • Investigate debugging strategies for C++ programs
  • Study the use of iterators with vectors in C++
USEFUL FOR

C++ programmers, computer science students, and anyone looking to improve their skills in vector manipulation and loop control in C++.

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:
We have many threads on AI, which are mostly AI/LLM, e.g,. ChatGPT, Claude, etc. It is important to draw a distinction between AI/LLM and AI/ML/DL, where ML - Machine Learning and DL = Deep Learning. AI is a broad technology; the AI/ML/DL is being developed to handle large data sets, and even seemingly disparate datasets to rapidly evaluated the data and determine the quantitative relationships in order to understand what those relationships (about the variaboles) mean. At the Harvard &...

Similar threads

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