For C++ ONLY please, Write a for loop to print all elements in courseGrades, fol
ID: 3812777 • Letter: F
Question
For C++ ONLY please,
Write a for loop to print all elements in courseGrades, following each element with a space (including the last). Print forwards, then backwards. End each loop with a newline. Ex: If courseGrades = {7, 9, 11, 10}, print:
Hint: Use two for loops. Second loop starts with i = NUM_VALS - 1.
#include <iostream>
using namespace std;
int main() {
const int NUM_VALS = 4;
int courseGrades[NUM_VALS];
int i = 0;
courseGrades[0] = 7;
courseGrades[1] = 9;
courseGrades[2] = 11;
courseGrades[3] = 10;
//Your solution goes here
Explanation / Answer
C++ Program :
#include <iostream>
using namespace std;
int main() {
const int NUM_VALS = 4;
int courseGrades[NUM_VALS];
int i = 0;
courseGrades[0] = 7;
courseGrades[1] = 9;
courseGrades[2] = 11;
courseGrades[3] = 10;
//Your solution goes here
for(i=0;i<NUM_VALS;i++){
cout << courseGrades[i] << " ";
}
cout << endl;
for(i=NUM_VALS-1;i>=0;i--){
cout << courseGrades[i] << " ";
}
cout << endl;
}
OUTPUT :
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.