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

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

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
1 answer 15 views
15 views
I'm missing something that would make the code work completely.
asked Dec 7, 2020 TeamScript 13.5k points
0 votes
1 answer 39 views
39 views
Problem: Hi there! I am confused in while loops and I need to write a program in which a value is taken as input from the user. If the value is less than 0, the program should increment it by 5 as long as the value remains less than 0. As the value becomes greater than or equal to 0, it should print the number of times the value has been incremented and the final value. Any help??
asked Jan 21 Code Learner 9.2k points
0 votes
1 answer 2 views
2 views
Problem: As I previously mentioned in my question, I am stuck while studying. Could someone help assist me in completing my project?
asked Mar 30 rakib1 51.3k points
0 votes
1 answer 73 views
73 views
Problem: Hi there! I am a beginner, recently started learning Java, came across an objective question that was The "less than or equal to" comparison operator in java is used to _________. I left this blank as I was unable to recall what this operator does. Could someone please tell me the right answer? Also, give an example program that shows the functionality of this operator. Thanks!
asked Jan 17 Code Learner 9.2k points
0 votes
1 answer 51 views
51 views
I am new to this. I just want to know how to do that?
asked Nov 6, 2020 TeamScript 13.5k points
1 vote
1 answer 5 views
5 views
Problem: How to fix this error : please enter a value less than or equal to 2147483647
asked Apr 2 MUHAMMAD MUNEEB 80k points
0 votes
1 answer 9 views
9 views
Problem: I have this error, and I need to fix it, any help: the truth value of an array with more than one element is ambiguous. use a.any() or a.all()
asked Mar 14 Wafa Abu Yousef 6.1k points
0 votes
2 answers 1.9K views
1.9K 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