139k views
1 vote
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 Hint: Use two for loops. Second loop starts with i = NUM_VALS - 1. (Notes) Note: These activities may test code with different test values. This activity will perform two tests, both with a 4-element array (int courseGrades[4]). See "How to Use zyBooks".

1 Answer

1 vote

Answer:

#include <iostream>

using namespace std;

int main() {

int courseGrades[50],NUM_VALS;//coursegrades and numvals variables declared.

cout<<"Enter the NUM_VALS"<<endl;

cin>>NUM_VALS;//taking input of the NUM_VALS.

cout<<"Enter the course Grades"<<endl;

for(int i=0;i<NUM_VALS;i++)

cin>>courseGrades[i];//takin input of the course grades.

for(int i=0;i<NUM_VALS;i++)

{

cout<<courseGrades[i]<<" ";//printing the course grades forward.

}

cout<<endl;

for(int i=NUM_VALS-1;i>=0;i--)//printing the course grades backward.

{

cout<<courseGrades[i]<<" ";

}

return 0;

}

Output:-

Enter the NUM_VALS

4

Enter the course Grades

7 9 11 10

7 9 11 10

10 11 9 7

Step-by-step explanation:

The above written code is in C++.

I have declared an array courseGrades and a variable NUM_VALS. Then first taking input of the NUM_VALS then taking the input of the courseGrades.Then the first loop is for printing tin forward direction then the other loop is for printing kin backward direction.

User Matthew Sant
by
7.3k points