108k views
0 votes
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 = courseGrades.length - 1. (Notes) Note: These activities may test code with different test values. This activity will perform two tests, both with a 4-element array. See "How to Use zyBooks". 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.

1 Answer

4 votes

Answer:

Following are the loop in c language

t = sizeof(courseGrades)/sizeof(courseGrades[0]); // determine the size of array

for(int i=0;i<t;++i) // iterating loop

{

printf("%d ",courseGrades[i]); // print the array in forward direction with space

}

for(int i=t-1;i>=0;--i)// iterating loop

{

printf("%d ",courseGrades[i]); // print the array in backward direction with space

}

Step-by-step explanation:

In this firstly we find a size of the courseGrades array in variable "t" after that we created a loop for print the courseGrades array in forward direction with space.

finally we created a loop for printing the courseGrades array in backwards direction.

Following are the program in c language :

#include <stdio.h> // header file

int main() // main function

{

int courseGrades[]={7, 9, 11, 10}; // array declaration

int t; // variable t to store the length of the array

t = sizeof(courseGrades)/sizeof(courseGrades[0]); // determine the size of array

for(int i=0;i<t;++i) // iterating loop

{

printf("%d ",courseGrades[i]); //print the array in forward direction with space

}

for(int i=t-1;i>=0;--i) // iterating loop

{

printf("%d ",courseGrades[i]);// // print the array in backward direction with space

}

return 0;

}

Output:

7 9 11 10 10 11 9 7

User Naeem
by
7.4k points