Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a for loop to print all elements in courseGrades, following each element w

ID: 3596699 • Letter: W

Question

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:


Also note: If the submitted code tries to access an invalid array element, such as courseGrades[9] for a 4-element array, the test may generate strange results. Or the test may crash and report "Program end never reached", in which case the system doesn't print the test case that caused the reported message.

#include
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 */

return 0;
}

I cant figure this one out. Can someone help me?

Explanation / Answer


#include <stdio.h>

int main()
{
int i, d, a[4], b[4];

const int NUM_VALS = 4;

int courseGrades[4]={7,9,11,10};


printf("you are given values");
for (i= 0; i <NUM_VALS ; i++)
printf("%d ", courseGrades[i]);

/*
* Copying elements into array b starting from end of array a
*/

for (i = NUM_VALS - 1, d = 0; i >= 0; i--, d++)
b[d] = courseGrades[i];

/*
* Copying reversed array into original.
* Here we are modifying original array, this is optional.
*/

for (i = 0; i < NUM_VALS; i++)
courseGrades[i] = b[i];

printf("Reverse array is ");

for (i = 0; i < NUM_VALS; i++)
printf("%d ", courseGrades[i]);


return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote