Question

In: Computer Science

Write a loop that subtracts 1 from each element in lowerScores. If the element was already...

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}.

please bold the solution

import java.util.Scanner;
public class StudentScores {
public static void main (String [] args) {
Scanner scnr = new Scanner(System.in);
final int SCORES_SIZE = 4;
int[] lowerScores = new int[SCORES_SIZE];
int i;

for (i = 0; i < lowerScores.length; ++i) {
lowerScores[i] = scnr.nextInt();
}

/* Your solution goes here */

for (i = 0; i < lowerScores.length; ++i) {
System.out.print(lowerScores[i] + " ");
}
System.out.println();
}
}

Solutions

Expert Solution

Java program :

StudentScores.java

//import package
import java.util.Scanner;
//Java class
public class StudentScores {
   //main() method
   public static void main(String[] args) {
       //Scanner class Object
       Scanner scnr = new Scanner(System.in);
       //variable that store array size
       final int SCORES_SIZE = 4;
       //integer array with name lowerScores
       int[] lowerScores = new int[SCORES_SIZE];
       int i;//declaring variable i
       //using for loop to take input from user
       for (i = 0; i < lowerScores.length; ++i) {
           //reading each score entered by user and
           //storing in the array
           lowerScores[i] = scnr.nextInt();
       }
       //using for loop
       for (i = 0; i < lowerScores.length; ++i) {
           //checking element
           if(lowerScores[i]<=0)
           {
               //if element is negative then assign 0
               lowerScores[i]=0;//set element as 0
           }
           else {
               //when element is non zero this means positive then
               //subtract -1 from the element
               lowerScores[i]=lowerScores[i]-1;
           }
       }

      
       //using for loop to print the array elements
       for (i = 0; i < lowerScores.length; ++i) {
           //print each array element
           System.out.print(lowerScores[i] + " ");
       }
       System.out.println();//used for new line
   }
}

=========================================

Output :


Related Solutions

In C. Write a loop that subtracts 1 from each element in lowerScores. If the element...
In C. 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}.
Write a loop that sets each array element to the sum of itself and the next...
Write a loop that sets each array element to the sum of itself and the next element, except for the last element which stays the same. Be careful not to index beyond the last element. Ex: Initial scores: 10, 20, 30, 40 Scores after the loop: 30, 50, 70, 40 The first element is 30 or 10 + 20, the second element is 50 or 20 + 30, and the third element is 70 or 30 + 40. The last...
Write a loop that sets each array element to the sum of itself and the next...
Write a loop that sets each array element to the sum of itself and the next element, except for the last element which stays the same. Be careful not to index beyond the last element. Ex: Initial scores: 10, 20, 30, 40 Scores after the loop: 30, 50, 70, 40 The first element is 30 or 10 + 20, the second element is 50 or 20 + 30, and the third element is 70 or 30 + 40. The last...
Write a for loop from 1 to 3 using i as the variable. For each value...
Write a for loop from 1 to 3 using i as the variable. For each value of i: Create a vector x of 10 random numbers between 0 and 1. Create a second vector t which is equal to ten integers from 1 to 10. Plot x versus t in figure 1. Use hold on to keep each plot. Use a different color for the line for each value of i. At the very end, add the text 'time' using...
Write a Formula for the ionic compound that forms from each pair of element Aluminum and...
Write a Formula for the ionic compound that forms from each pair of element Aluminum and oxygen Beryllium and iodine Calcium and sulfur Calcium and iodine
Write a program that uses a loop to read 10 integers from the user. Each integer...
Write a program that uses a loop to read 10 integers from the user. Each integer will be in the range from -100 to 100. After all 10 integers have been read, output the largest and smallest values that were entered, each on its own line in that order. Avoiding using the max or min functions from Python, and definitely use a loop to read the integers instead of 10 input statements.
goal of this function will be to add an element to an already existing array in...
goal of this function will be to add an element to an already existing array in C/C++. bool add(structure dir[], int& size, const char nm[], const char ph[]){ //code here } add = function name structure = structure with two fields { char name[20]; char phone[13]; } int& size = # of entries in dir nm = name that's going to be added ph = phone that's going to be added. return true if entry was successfully added, false if...
Write a program that adds and subtracts two polynomials. It creates an array of nodes and...
Write a program that adds and subtracts two polynomials. It creates an array of nodes and connects them into the freeStore. This implementation uses one array to store multiple array to store multiple polynomial instances and the free store. I need help to finish the LinkedListInArrayPolynomial class. Output should look like below: Forth test is linked list of terms in an array. linkInArray1 = 3x^11+4x^10+4x^4 linkInArray2 = 4x^19+5x^14-3x^12-78 sum of linkInArray1 and linkInArray2 = 4x^19+5x^14-3x^12+3x^11+4x^10+4x^4-78 linkInArray1 minus linkInArray2 = -4x^19-5x^14+3x^12+3x^11+4x^10+4x^4+78...
Write a for-loop in MATLAB that generates a list of numbers such that each number is...
Write a for-loop in MATLAB that generates a list of numbers such that each number is the sum of the previous three. Initialize your list of numbers at the values of 0, 0 and 1. In other words, "0" is the first element of the list, "0" is the second element of the list, and "1" is the third element of the list. What is the 20th value in the list?
Write Java code to generate 100 random integers ranging from 0..9, inserting each element into an...
Write Java code to generate 100 random integers ranging from 0..9, inserting each element into an ArrayList. Then search for the first instance of the number 3, print the position, and then remove it from the list.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT