Task is to write two methods that use a two-dimensional array as input and retur
ID: 3641941 • Letter: T
Question
Task is to write two methods that use a two-dimensional array as input and return a one-dimensional array that contains the row-by-row average or the column-by-column average, as directed. Name your class TwoDaverage, which shall contain the two methods described below.ROW-BY-ROW AVERAGE
Name this method rowAverage. The input parameter for rowAverage() is a rectangular, two-dimensional array of doubles. rowAverage() returns a one-dimensional array of doubles whose length is the number of rows in the two-dimensional input array and whose values are the averages of each row. For example, the zeroth element of the one-dimensional array that is returned is the average of all the values in the zeroth row of the two-dimensional array.
Be sure to include Javadoc comments for your method.
COLUMN-BY-COLUMN AVERAGE
Name this method columnAverage. The input parameter is the same as for rowAverage(). columnAverage() returns a one-dimensional array of doubles whose length is the number of columns in the two-dimensional input array and whose values are the averages of each column.
Be sure to include Javadoc comments for your method.
Explanation / Answer
please rate - thanks
import java.util.*;
public class main
{
public static void main(String[] args)
{Scanner in = new Scanner(System.in);
int n,i,j,m;
System.out.println("Enter rows in the matrix: ");
n=in.nextInt();
System.out.println("Enter columns in the matrix: ");
m=in.nextInt();
double [][] a = new double [n][m];
for(i=0;i<n;i++)
for(j=0;j<m;j++)
a[i][j]=Math.random()*100.;
System.out.println("The starting matrices:");
for(i=0;i<n;i++)
{for(j=0;j<m;j++)
System.out.printf("%5f ",a[i][j]);
System.out.println();
}
double b[]=rowAverage(a);
System.out.println("Row averages");
for(i=0;i<b.length;i++)
System.out.printf("%5f ",b[i]);
System.out.println();
double c[]=columnAverage(a);
System.out.println("Column averages");
for(i=0;i<c.length;i++)
System.out.printf("%5f ",c[i]);
System.out.println();
}
public static double[] rowAverage(double a[][])
{int i,j;
double sum;
double b[]=new double[a.length];
for(i=0;i<a.length;i++)
{sum=0;
for(j=0;j<a[0].length;j++)
sum+=a[i][j];
b[i]=sum/a[0].length;
}
return b;
}
public static double[] columnAverage(double a[][])
{int i,j;
double sum;
double b[]=new double[a[0].length];
for(i=0;i<a[0].length;i++)
{sum=0;
for(j=0;j<a.length;j++)
sum+=a[j][i];
b[i]=sum/a.length;
}
return b;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.