233k views
3 votes
Write a loop that subtracts 1 from each element in lowerScores. If the element was already 0 or negative, assign 0 to the element. Ex: lowerScores = {5, 0, 2, -3} becomes {4, 0, 1, 0}.

User Elitalon
by
5.2k points

2 Answers

3 votes

Answer:

import java.util.Scanner;

public class StudentScores {

public static void main(String[] args) {

int len = 5;

int i;

int[] lowerScores = new int[len];

lowerScores[0] = 5;

lowerScores[1] = 0;

lowerScores[2] = 2;

lowerScores[3] = -3;

System.out.println("The original elements of array are ");

for (i = 0; i < len; i++)

{

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

}

System.out.println();

System.out.println("The modified elements of array are ");

for (i = 0; i < len; ++i)

{

if (lowerScores[i] < 0) {

lowerScores[i] = 0;

} else if (lowerScores[i] > 0) {

lowerScores[i] = lowerScores[i] - 1;

}

}

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

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

}

System.out.println();

}

}

Step-by-step explanation:

The code is missing for loop: for (i = 0; i < lowerScores.length; ++i)

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

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

}

User Zia Khan
by
4.8k points
6 votes

Answer:

The JAVA program to implement for loop for given scenario is shown below.

public class MyProgram {

public static void main(String args[]) {

int len = 5;

int[] lowerScores = new int[len];

lowerScores[0] = 5;

lowerScores[1] = 0;

lowerScores[2] = 2;

lowerScores[3] = -3;

System.out.println("The original elements of array are ");

for(int i=0; i<len; i++)

{

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

}

System.out.println();

System.out.println("The modified elements of array are ");

for(int i=0; i<len; i++)

{

// if element is 0 or negative, 0 is assigned to this index

if(lowerScores[i] <= 0)

lowerScores[i] = 0;

// 1 is subtracted from every non zero and positive element

else

lowerScores[i] = lowerScores[i] - 1;

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

}

}

}

OUTPUT

The original elements of array are

5 0 2 -3 0

The modified elements of array are

4 0 1 0 0

Explanation:

The integer array is declared as shown.

int[] lowerScores = new int[len];

The length of the array is given by integer variable len. As per the question, we have taken four elements and size of 5 for the given array.

int len = 5;

The elements of the array are initialized inside the program with the elements given in the question.

lowerScores[0] = 5;

lowerScores[1] = 0;

The values of the array can be changed to test the program. Also, the size of the array can be changed for testing.

As per the given scenario, if the element is greater than zero, 1 is subtracted from this element. For elements less than or equal to zero, that particular element is assigned the value of 0.

The given program implements if else statement inside for loop for this purpose.

for(int i=0; i<len; i++)

{

// if element is 0 or negative, 0 is assigned to this index

if(lowerScores[i] <= 0)

lowerScores[i] = 0;

// 1 is subtracted from every non zero and positive element

else

lowerScores[i] = lowerScores[i] - 1;

}

User Bortdc
by
5.7k points