44.5k views
5 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.

User Turbo J
by
8.3k points

2 Answers

7 votes

Final answer:

To subtract 1 from each element in the lowerscores array, you can use a for loop. Within the loop, you can check if the current element is already 0 or negative. If it is, assign 0 to the element. Otherwise, subtract 1 from the element.

Step-by-step explanation:

To subtract 1 from each element in the lowerscores array, you can use a for loop. Within the loop, you can check if the current element is already 0 or negative. If it is, assign 0 to the element. Otherwise, subtract 1 from the element.

for (int i = 0; i < lowerscores.length; i++) {
if (lowerscores[i] <= 0) {
lowerscores[i] = 0;
} else {
lowerscores[i] = lowerscores[i] - 1;
}
}

In this example, 'lowerscores' represents the array that contains the scores. The loop iterates through each element of the array and performs the desired subtraction or assignment based on the given condition.

User Roni Antonio
by
7.5k points
6 votes
(pseudo-code)

var element = input

if element = or < than 0;

assign 0 to element;

else subtract 1 from element;

repeat

This problem will be seen in many introductory programming classes. The task assigned is to create a program that constantly subtracts 1 from a variable until the variable reaches 0, which then causes the variable to stay at 0. If said variable started out negative or 0, it will also stay at 0.
User Fuseblown
by
8.1k points