Answer to Question #260507 in C++ for malika

Question #260507

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;

}


1
Expert's answer
2021-11-03T17:48:19-0400

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





Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS