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:
7 9 11 10
10 11 9 7
#include <iostream>
using namespace std;
int main() {
const int NUM_VALS = 4;
int courseGrades[NUM_VALS];
int i;
for (i = 0; i < NUM_VALS; ++i) {
cin >> courseGrades[i];
}
/* Your solution goes here */
return 0;
}
Source code
#include <iostream>
using namespace std;
int main() {
const int NUM_VALS = 4;
int courseGrades[NUM_VALS];
int i;
cout<<"\nEnter course grades: ";
for (i = 0; i < NUM_VALS; ++i) {
cin >> courseGrades[i];
}
cout<<"\nBefore: ";
for(int i=0;i<NUM_VALS;i++){
cout<<courseGrades[i]<<" ";
}
cout<<endl;
cout<<"\nAfter: ";
for(int i=(NUM_VALS-1);i>=0;i--){
cout<<courseGrades[i]<<" ";
}
return 0;
}
Output
Comments
Leave a comment