2

The loop causing issues is the second for loop.

import java.util.Scanner;
public class StudentScores {
   public static void main (String [] args) {
      Scanner scnr = new Scanner(System.in);
      final int NUM_POINTS = 4;
      int[] dataPoints = new int[NUM_POINTS];
      int controlValue;
      int i;

      controlValue = scnr.nextInt();

      for (i = 0; i < dataPoints.length; ++i) {
         dataPoints[i] = scnr.nextInt();
      }

      for (i = 0; dataPoints[i] < controlValue; ++i) {
         dataPoints[i] = dataPoints[i] * 2;
      }

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

Currently when i run the test, this is what pops up.

Testing with inputs: 10 2 12 9 20

Output differs.

Your output

4 12 9 20 

Expected output

4 12 18 20 

the first number in the list of inputs is the maximum number it should multiply by 2. Since 20 is > 10, it is not multiplied. However 9 should be multiplying to 18, but it is not. If anyone could give some insight into anything i might be doing wrong when forming this loop, I would greatly appreciate it. Thank you!

2
  • your second for loop is exiting because of your condition, you should instead iterate through the dataPoints in full and make the check within the loop before multiplying. Commented Jun 25, 2020 at 21:15
  • 1
    Andrew, if you're learning to program (you appear to be using one of the online programming challenge sites), the very best thing to learn is how to use a debugger to step through code that isn't doing what you want/expect it to. With a debugger, you will see what the code does, not what you think it does (in this case, exiting the loop when it sees the 12 that is not less than 10). Commented Jun 25, 2020 at 21:33

1 Answer 1

1

Your second for loop is exiting because of the condition and the order of your input data, and will stop when a control value is reached. For example, if you instead had an input of 10 1 2 3 4, you would get the results you expect (ie. 2 4 6 8), but your for loop is exiting when the condition dataPoints[i] < controlValue is reached (in your case at 12).

Instead you should loop through the entire dataPoints array, as you do in your 3rd loop, and check the controlValue before doubling the values.

import java.util.Scanner;
public class StudentScores {
   public static void main (String [] args) {
      Scanner scnr = new Scanner(System.in);
      final int NUM_POINTS = 4;
      int[] dataPoints = new int[NUM_POINTS];
      int controlValue;
      int i;

      controlValue = scnr.nextInt();

      for (i = 0; i < dataPoints.length; ++i) {
         dataPoints[i] = scnr.nextInt();
      }

      for (i = 0; i < dataPoints.length; ++i) {
         if (dataPoints[i] < controlValue) {
             dataPoints[i] = dataPoints[i] * 2;
         }
      }
      
      // This could be combined into the loop above...
      for (i = 0; i < dataPoints.length; ++i) {
         System.out.print(dataPoints[i] + " ");
      }
      System.out.println();
   }
}

You could also do all of this in a single loop if you wanted to, for example:

for (i = 0; i < dataPoints.length; ++i) {
    dataPoints[i] = scnr.nextInt();

    if (dataPoints[i] < controlValue) {
        dataPoints[i] = dataPoints[i] * 2;
    }

    System.out.print(dataPoints[i] + " ");
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.