QUESTION IMAGE
Question
array testgrades contains num_vals test grades read from input. organize the lines of code to create a for loop that sets sumextra to the total extra credit received. full credit is 100, so any grade over 100 has extra credit.
ex: if the input is 101 83 107 90, then the output is sumextra: 8, because 1 + 0 + 7 + 0 is 8.
note: not all lines of code on the left should be used in the final solution.
how to use this tool
unused
sumextra = 0;
if (testgradesi > 100) {
if (testgradesi < 100) {
}
for (i = 0; i < testgrades.length; ++i) {
for (i = 0; i <= testgrades.length; ++i) {
sumextra = sumextra + (testgradesi - 100);
}
sumofexcess.java
import java.util.scanner;
public class sumofexcess {
public static void main(string args) {
scanner scnr = new scanner(system.in);
final int num_vals = 4;
int testgrades = new intnum_vals;
int i;
int sumextra;
for (i = 0; i < testgrades.length; ++i) {
testgradesi = scnr.nextint();
}
system.out.println(\sumextra: \ + sumextra);
}
}
Step1: Initialize sumExtra
sumExtra = 0;
Step2: Iterate through testGrades
for (i = 0; i < testGrades.length; ++i) {
Step3: Check if grade > 100
if (testGrades[i] > 100) {
Step4: Calculate extra credit
sumExtra = sumExtra + (testGrades[i] - 100);
Step5: End if and for loops
}
}
Snap & solve any problem in the app
Get step-by-step solutions on Sovi AI
Photo-based solutions with guided steps
Explore more problems and detailed explanations
import java.util.Scanner;
public class SumOfExcess {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
final int NUM_VALS = 4;
int[] testGrades = new int[NUM_VALS];
int i;
int sumExtra = 0; // Initialize sumExtra
for (i = 0; i < testGrades.length; ++i) {
testGrades[i] = scnr.nextInt();
}
for (i = 0; i < testGrades.length; ++i) { // Iterate through testGrades
if (testGrades[i] > 100) { // Check if grade > 100
sumExtra = sumExtra + (testGrades[i] - 100); // Calculate extra credit
}
}
System.out.println("sumExtra: " + sumExtra);
}
}