Anne
Anne

Reputation: 31

Printing array elements with a for loop

This is a challenge question from my online textbook I can only get the numbers to prin forward... :(

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.

Note: These activities may test code with different test values. This activity will perform two tests, the first with a 4-element array (int courseGrades[4]), the second with a 2-element array (int courseGrades[2]).

import java.util.Scanner;

 public class CourseGradePrinter {
 public static void main (String [] args) {
  final int NUM_VALS = 4;
  int[] courseGrades = new int[NUM_VALS];
  int i = 0;

  courseGrades[0] = 7;
  courseGrades[1] = 9;
  courseGrades[2] = 11;
  courseGrades[3] = 10;

  /* Your solution goes here  */

  for(i=0; i<NUM_VALS; i++){
     System.out.print(courseGrades[i] + " ");
  }

  for(i=NUM_VALS -1; i>3; i++){
     System.out.print(courseGrades[i]+ " ");
     }

  return;
}
}

Upvotes: 1

Views: 91676

Answers (5)

Robert Heavner
Robert Heavner

Reputation: 1

  for (i = 0; i < courseGrades.length; ++i) {
     System.out.print(courseGrades[i] + " ");
  }
  System.out.println();

  for (i = courseGrades.length - 1; i >= 0; --i) {
     System.out.print(courseGrades[i] + " ");
  }
  System.out.println();

Upvotes: -1

Nora Southeast
Nora Southeast

Reputation: 1

I also have been working on the this textbook question. The issue with the above code is that i has already been assigned, so trying using int in the for loop will cause an error. Below is the code I used to successfully achieve the desired outcome.

for ( i = 0 ; i <NUM_VALS; ++i) {
     if (i > 0) {
        System.out.print(""); 
     }
     System.out.print(courseGrades[i] + " ");
  }
  System.out.println("");
  
  for ( i = NUM_VALS -1; i >=0; --i) {
     if (i > 0) {
        System.out.print(""); 
     }
     System.out.print( courseGrades[i] + " ");
  }
     System.out.println();

Upvotes: 0

A. Nony Mous
A. Nony Mous

Reputation: 41

This is the code to answer the question from zyBooks, 6.2.3: Printing array elements with a for loop.

 for (i = 0; i < NUM_VALS; i++) {
     System.out.print(courseGrades[i] + " ");
 }
 System.out.println("");

 for (i = NUM_VALS - 1; i >= 0; i--) {
    System.out.print(courseGrades[i] + " ");
 }

 System.out.println(""); 

Upvotes: 4

Joels Elf
Joels Elf

Reputation: 784

To print backwards you want:

for(i = NUM_VALS - 1; i >= 0; i--) {
    System.out.print(courseGrades[i] + " ");
}
// To end with a newline
System.out.println("");

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522636

Your two loops almost were correct. Try using this code:

for (int i=0; i < NUM_VALS; i++) {
    // this if statement avoids printing a trailing space at the end.
    if (i > 0) {
        System.out.print(" ");
    }
    System.out.print(courseGrades[i]);
}

for (int i=NUM_VALS-1; i >= 0; i--) {
    if (i > 0) {
        System.out.print(" ");
    }
    System.out.print(courseGrades[i] + " ");
}

Upvotes: 0

Related Questions