Question
Can somebody help me with the solution. I have been trying all day
Student I me The following main method (TheFinal) should print on output "The Total" and "The Average" of all integer numbers of array int [I numbers. Please complete the coding part of methods getSum and getavg and submit your work for grading. You must use for or while loop statement inside the methods getSum and getavg, you will not get credit if you did not use the loop rour program MUST have loop to add the (10,40,15,20,25,30,35,5) togather to find the total and average public class TheFinal public static void main(String[] args) inE [1 numbers doub le sur-O, i0, 40, 15, 20,2s,30,35, 5): Average-0; um=ge t Sum (numbers) ; Average getavg (numbers) System.out.printin("The Total +sum) Write your codes +Average): pub1ic static int getSum(int Array[ int sum-0: Write your codes return (sum) public static double getavg(int Arrayt] int sum-0 return (sum/Array.length) : OPASP execs ava Examp le The TotAl100.0 The Averag2.5
Explanation / Answer
public static int getsum(int array[])
{
int sum = 0; // initialize sum
int i;
// Iterate through all elements and add them to sum
for (i = 0; i < array.length; i++)
sum += array[i];
return sum;
}
Output:
Sum of given array is 180.0
Average of given array is 22.0