Ask Question
20 May, 12:55

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.#include int main (void) {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; }

+2
Answers (1)
  1. 20 May, 14:13
    0
    The first step is to;

    a. import Scanner class present in java. util package for take input in arrays.

    b. When final variable is initialized it can not be changed so fix the size to 4.

    c. create array to hold 4 integer elements in array.

    d. Now ensure users enter integer values separated by space in single line & assign elements at each index of array starting from index 0.

    e. Next is to print the array elements in forward directions first.

    reset i to 0 to print all elements of array from index 0.

    f. Then ensure to traverse array and display each element one by one separated by space in same line using for loop.

    g. Therefore to move to next line.

    reset i to array. length - 1. loop will go from last elements index till index 0.

    h. Finally print elements in array backwards from last elements of array.

    The second step is given here,

    import java. util. Scanner;

    public class CourseGradePrinter {

    public static void main (String args) {

    //using scanner to take input from user ...

    Scanner scnr = new Scanner (System. in);

    //fix the array size to 4.

    final int NUM_VALS = 4;

    //array created of size 4 to hold int data

    int[] courseGrades = new int[NUM_VALS];

    //declare i for user input and traversing array

    int i;

    for (i = 0; i < courseGrades. length; + +i) {

    //user enters integer values

    courseGrades[i] = scnr. nextInt ();

    }

    / * Your solution goes here * /

    //display the array elements forwards now

    i=0;

    Ensure to print elements in array.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a for loop to print all elements in courseGrades, following each element with a space (including the last). Print forwards, then ...” in 📘 Engineering if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers