Write a static method complete with a header line that has two integer array arg
ID: 3758136 • Letter: W
Question
Write a static method complete with a header line that has two integer array arguments, A and B. The arrays may have different lengths. Determine the average of the values in array A. Also determine the average of the values in array B. Your function should return the larger of the two average values.
Use nested loops to generate the output for each of the two shapes:
&&&& and &
&&& &&
&& &&&
& &&&&
Explanation / Answer
public static int largerArrayMean(int a[],int b[]){
int sumA = 0,sumB = 0;
for(int i = 0; i < a.length; i++)
sumA += a[i];
for(int i = 0; i < b.length; i++)
sumB += b[i];
return sumA/a.length > sumB/b.length ? sumA/a.length : sumB/b.length;
}
public static void pattern(int n){
for(int i = n ; i > 0; i--)
{
print(i,n-i);
}
}
public static void print(int n,int spaces){
for(int i = 0; i < spaces; i++)
System.out.print(" ");
for(int i = 0; i < n; i++)
System.out.print("&");
System.out.println();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.