27.8k views
5 votes
Double any element's value that is less than controlValue. Ex: If controlValue = 10, then dataPoints = {2, 12, 9, 20} becomes {4, 12, 18, 20}.

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) { System.out.print(dataPoints[i] + " "); } System.out.println(); } }

1 Answer

2 votes

Answer:

import java.util.Scanner;

public class numm3 {

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;

System.out.println("Enter the control Variable");

controlValue = scnr.nextInt();

System.out.println("enter elements for the array");

for (i = 0; i < dataPoints.length; ++i) {

dataPoints[i] = scnr.nextInt();

}

for (i = 0; i < dataPoints.length; ++i) {

System.out.print(dataPoints[i] + " ");

}

System.out.println();

//Doubling elements Less than Control Variable

for (i = 0; i < dataPoints.length; ++i) {

if (dataPoints[i]<controlValue){

dataPoints[i] = dataPoints[i]*2;

}

System.out.print(dataPoints[i] + " ");

}

System.out.println(); } }

Step-by-step explanation:

See the additional code to accomplish the task in bold

The trick is using an if statement inside of a for loop that checks the condition (dataPoints[i]<controlValue) If true, it multiplies the element by 2

User Betamos
by
7.2k points