• Register
1 vote
94 views

Problem:

I'm taking an online Java programming class (I'm a beginner) and I cannot figure how to correctly complete the code. I've already written what I think is to be included, however, I'm missing something that would make the code work completely. Please find my code snippet below:

public class MyClass {
   public static void main (String [] args) {
      final int num = 4;
      int[] dataPoints = new int[num];
      int minVal = 0;
      int i = 0;
      dataPoints[0] = 2;
      dataPoints[1] = 12;
      dataPoints[2] = 9;
      dataPoints[3] = 20;
      minVal = 10;

      for (n = 0; dataPoints[n] < minVal; ++n) {
         dataPoints[n] = dataPoints[n] * 2;
      }

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

      return;
   }
}

My goal was to double any element's value that is less than minval. Could any of you please figure the problem and let me move forward?
Thanks. I am grateful to you.

15.3k points

Please log in or register to answer this question.

2 Answers

1 vote

Solution:

Your termination condition should check whether you have looked at all values in the array. Once you've gone past all the values in your array your index variable is pointing all the way at the end. And remember, as soon as the termination condition is false your loop will stop executing. So you want to run your code while n (your index variable) is less than the size of the array (dataPoints.length).

So, use this iteration to avoid the error, below:

for (n = 0; dataPoints[n] < num ; ++n) {
         if(dataPoints[n] < minVal) {
         dataPoints[n] = dataPoints[n] * 2;
      }
  }

It may help you. Thanks

94.3k points
0 votes

Solution:

In your code, 12 < 10 assess to be false and so, it goes out of loop and therefore, it provides Wrong Output. Inspect the below code:

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

I looped through all elements of array and in case an element is < minVal, I multiplied it by 2.

The second section of the for statement is the termination condition: at one time it assess to false, your loop will stop implementing.

In this special problem, you're attempting to search at every value in your dataPoints array, and double it. This calls for the exercise of an index variable.

Your index variable will repose track of your position inside the array as you move from left to right. Begining on the left, at index 0, you will move right whilst you're at the end of the array.

++i takes care of incrementing your index variable every time the loop runs. This is what moves you via the array.

Your termination condition must inspect whether you have looked at all values in the array. At one time you've gone past all the values in your array your index variable is indicating all the way at the end. And keep in mind, as soon as the termination condition is false your loop will stop executing. Hence you need to run your code while i (your index variable) is less than the size of the array (dataPoints.length).

At one time you've found the for loop bounds, easily inspect your conditional with an if-then statement before updating the values:

for (i = 0; i < dataPoints.length; ++i) {
    if (dataPoints[i] < minVal) {
       dataPoints[i] = dataPoints[i] * 2;
    }
}

 

31.7k points
edited by