Directions: Fix the code below to correctly create the array; correctly loop thr
ID: 3543468 • Letter: D
Question
Directions: Fix the code below to correctly create the array; correctly loop through the array with the for(...) statement; and return the average of the array. The output just needs to be the average number. No formatting is required.
// FixDebugEight2.java
// Sum and average an array of integers
public class DebugEight2 {
public static void main(String args[])
int[] someNums = 4, 17, 22, 8, 35;
int tot;
int x;
for(x = 0; x < somenums.length; ++x)
tot += someNums[x];
System.out.println(
Directions: Fix the code below to correctly create the array; correctly loop through the array with the for(...) statement; and return the average of the array. The output just needs to be the average number. No formatting is required.Explanation / Answer
//FixDebugEight2.java
//Sum and average an array of integers
public class DebugEight2 {
public static void main(String args[]){ // you missed the opening bracket of main
int[] someNums = {4, 17, 22, 8, 35}; // you missed the braces {}
int tot = 0; // initialised the variable 'tot'
int x;
for(x = 0; x < someNums.length; ++x) // the variable is someNums, not somenums
tot += someNums[x];
System.out.println("Sum is" + tot);
System.out.println("Average is " + tot * 1.0 / someNums.length); // it should be someNums.length
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.