15 October, 2020

Write a loop that subtracts 1 from each element in lowerScores.




1. Write a loop that subtracts 1 from each element inlowerScores. If the element was already 0 or negative, assign 0 tothe element. Ex: lowerScores = {5, 0, 2, -3} becomes {4, 0, 1,0}.

what i am given

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();
}
}

2.

Write a loop that sets newScores to oldScores shifted once left,with element 0 copied to the end. Ex: If oldScores = {10, 20, 30,40}, then newScores = {20, 30, 40, 10}.

Note: These activities may test code with different test values.This activity will perform two tests, both with a 4-element array(int oldScores[4]). See "How to Use zyBooks".

Also note: If the submitted code tries to access an invalid arrayelement, such as newScores[9] for a 4-element array, the test maygenerate strange results. Or the test may crash and report "Programend never reached", in which case the system doesn't print the testcase that caused the reported message.
what i am give.

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[] oldScores = new int[SCORES_SIZE];
int[] newScores = new int[SCORES_SIZE];
int i;

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

/* Your solution goes here */

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

3. Write a loop that sets each array element to the sum ofitself and the next element, except for the last element whichstays the same. Be careful not to index beyond the last element.Ex:

Initial scores: 10, 20, 30, 40Scores after the loop: 30, 50, 70, 40

The first element is 30 or 10 + 20, the second element is 50 or20 + 30, and the third element is 70 or 30 + 40. The last elementremains the same.

what i am given

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[] bonusScores = new int[SCORES_SIZE];
int i;

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

/* Your solution goes here */

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