• Register
1 vote
31 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.

12 7 7
15,250 points

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

13 9 6
94,240 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;
    }
}

 

10 6 4
31,120 points

Related questions

0 votes
2 answers 1.3K views
1.3K views
Problem : I got following rather cryptic error message: the truth value of an array with more than one element is ambiguous. use a.any() or a.all()
asked Nov 18, 2019 peterlaw 6.9k points
0 votes
1 answer 37 views
37 views
Problem: I am little bit confused and need help regarding my query? Which of the following expressions will determine whether x is less than or equal to y? A) x > y B) x =< y C) x <= y D) x >= y
asked Feb 22 maddi86 5.4k points
1 vote
1 answer 47 views
47 views
Problem : I am very new to Java Programming also I have very recently started using the Intellij as the IDE. I have observed that If I try to use it then sometimes it is delayed a lot. I have tried changing my xms and xmx for the larger heap size (i.e. xms ... the maximum heap size". Because of above error message I have already rolled back my changes. Please let me know how can I fix above error?
asked May 9 stewart 4k points