Sovi.AI - AI Math Tutor

Scan to solve math questions

QUESTION IMAGE

712986 5539894.qx3zqy7 or the lines of code to create two for loops tha…

Question

712986 5539894.qx3zqy7
or the lines of code to create two for loops that print all elements in coursegrades, following each element with a space including the last. print forwards, then backwards. after each loop, print a newline.
ex: if the input is 7 9 11 10, then the output is:
7 9 11 10
10 11 9 7
how to use this tool
unused
cout << coursegradesi << \ \;
}
cout << coursegradesi << \ \;
cout << endl;
}
for (i = num_vals - 1; i >= 0; --i) {
cout << endl;
for (i = 0; i < num_vals; ++i) {
main.cpp
load default template...
#include <iostream>
using namespace std;
int main() {
const int num_vals = 4;
int coursegradesnum_vals;
int i;

for (i = 0; i < num_vals; ++i) {
cin >> coursegradesi;
}

return 0;
}

Explanation:

Step1: Add forward loop

for (i = 0; i < NUM_VALS; ++i) {
    cout << courseGrades[i] << " ";
}
cout << endl;

Step2: Add backward loop

for (i = NUM_VALS - 1; i >= 0; --i) {
    cout << courseGrades[i] << " ";
}
cout << endl;

Answer:

The completed main.cpp code is:

#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];
    }

    // Forward print loop
    for (i = 0; i < NUM_VALS; ++i) {
        cout << courseGrades[i] << " ";
    }
    cout << endl;

    // Backward print loop
    for (i = NUM_VALS - 1; i >= 0; --i) {
        cout << courseGrades[i] << " ";
    }
    cout << endl;

    return 0;
}

When given input 7 9 11 10, the output will be:

7 9 11 10
10 11 9 7