# Double any element's value that is less than minval.

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.

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

## 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;
}
}``````

## Related questions

15 views
I'm missing something that would make the code work completely.
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??
17 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?
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!
51 views
I am new to this. I just want to know how to do that?
1 vote