public class Lab10 { public static void main(String[] args) { int total = 0; dou
ID: 3532197 • Letter: P
Question
public class Lab10
{
public static void main(String[] args)
{
int total = 0;
double average = 0.0;
int row1Total = 0;
int column4Total = 0;
int maxInRow0 = 0;
int minInRow2 = 0;
// Declare a 2-Dimensional array called myNums
// It should hold 3 rows, and 5 columns of data
// Remember that 2D arrays are actually just arrays OF arrays
// Where the first [] is the row, and the second [] is the column
// Looking at the table on the lab sheet:
// 6 would be myNums[0][0], -100= myNums[1][1], -20=myNums[2][2]
// Remember that a nested FOR loop is needed to explore a 2D array.
// The primary structure will be like this:
/*
for(int i=0;i<myNums.length;i++)
{
for(int k=0;k<myNUms.length;k++)
{
}
}
*/
// Where the outer (i) loop is the row, and the inner (k) loop is the column
// ---------------------------------
// Uncomment this code block to test getTotal()
/*
total = getTotal(myNums);
System.out.println(" The total of myNums is: " + total);
*/
// Uncomment this code block to test getAverage()
/*
average = getAverage(myNums);
System.out.printf(" The average of myNums is: %.2f ", average);
*/
//Uncomment this code block to test getRowTotal
/*
row1Total = getRowTotal(myNums, 1);
System.out.println(" The total of row 1 of myNums is: " + row1Total);
*/
//Uncomment this code block to test getColumnTotal
/*
column4Total = getRowTotal(myNums, 4);
System.out.println(" The total of column 4 of myNums is: " + column4Total);
*/
//Uncomment this code block to test getMaxInRow()
/*
maxInRow0 = getMaxInRow(myNums, 0);
System.out.println(" The max value of row 0 of myNums is: " + maxInRow0);
*/
//Uncomment this code block to test getMinInRow()
/*
minInRow2 = getMinInRow(myNums, 2);
System.out.println(" The min value of row 2 of myNums is: " + minInRow2);
*/
}
/*
------------------
If you change names, make sure to change them everywhere in the method!
------------------
*/
public static int getTotal(int [][] a)
{
return total;
}
// It's best to use a double to avoid integer division
public static double getAverage(int [][] a)
{
return average;
}
public static int getRowTotal(int[][] a, int rowIndex)
{
return rowTotal;
}
public static int getColumnTotal(int [][] a, int columnIndex)
{
return columnTotal;
}
public static int getMaxInRow(int [][] a, int rowIndex)
{
return max;
}
public static int getMinInRow(int [][] a, int rowIndex)
{
return min;
}
}
Explanation / Answer
// i have defined all the functions and uncommented the required code block,now the code is working //fine just check itpublic class Lab10 { public static void main(String[] args) { int total = 0; double average = 0.0; int row1Total = 0; int column4Total = 0; int maxInRow0 = 0; int minInRow2 = 0;
// Declare a 2-Dimensional array called myNums // It should hold 3 rows, and 5 columns of data // Remember that 2D arrays are actually just arrays OF arrays // Where the first [] is the row, and the second [] is the column // Looking at the table on the lab sheet: // 6 would be myNums[0][0], -100= myNums[1][1], -20=myNums[2][2]
int myNums[][] =new int[3][5]; Scanner in=new Scanner(System.in);
System.out.println("fill the array myNums having 3 rows and 5 colums to check differnet functions defined"); for(int i=0;i<3;i++) for(int j=0;j<5;j++) { myNums[i][j]=in.nextInt(); }
// Remember that a nested FOR loop is needed to explore a 2D array. // The primary structure will be like this: /* for(int i=0;i<myNums.length;i++) { for(int k=0;k<myNUms.length;k++) {
} } */ // Where the outer (i) loop is the row, and the inner (k) loop is the column
// ---------------------------------
// Uncomment this code block to test getTotal() total = getTotal(myNums); System.out.println(" The total of myNums is: " + total);
// Uncomment this code block to test getAverage() average = getAverage(myNums); System.out.printf(" The average of myNums is: %.2f ", average);
//Uncomment this code block to test getRowTotal row1Total = getRowTotal(myNums, 1); System.out.println(" The total of row 1 of myNums is: " + row1Total);
//Uncomment this code block to test getColumnTotal column4Total = getColumnTotal(myNums, 4); System.out.println(" The total of column 4 of myNums is: " + column4Total);
//Uncomment this code block to test getMaxInRow() maxInRow0 = getMaxInRow(myNums, 0); System.out.println(" The max value of row 0 of myNums is: " + maxInRow0);
//Uncomment this code block to test getMinInRow() minInRow2 = getMinInRow(myNums, 2); System.out.println(" The min value of row 2 of myNums is: " + minInRow2); }
/* ------------------ If you change names, make sure to change them everywhere in the method! ------------------ */
public static int getTotal(int [][] a) {int total=0; for(int i=0;i<3;i++) for(int j=0;j<5;j++) total=total+a[i][j];
return total; }
// It's best to use a double to avoid integer division public static double getAverage(int [][] a) { double average=0.0; for(int i=0;i<3;i++) for(int j=0;j<5;j++) average=average+a[i][j]; average=average/15;
return average; }
public static int getRowTotal(int[][] a, int rowIndex) { int rowTotal=0; for(int i=0;i<5;i++) rowTotal=rowTotal+a[rowIndex][i];
return rowTotal; }
public static int getColumnTotal(int [][] a, int columnIndex) { int columnTotal=0; for(int i=0;i<3;i++) columnTotal=columnTotal+a[i][columnIndex]; return columnTotal; }
public static int getMaxInRow(int [][] a, int rowIndex) { int max=a[rowIndex][0]; for(int i=0;i<5;i++) if(max<a[rowIndex][i]) max=a[rowIndex][i]; return max; }
public static int getMinInRow(int [][] a, int rowIndex) {int min=a[rowIndex][0]; for(int i=0;i<5;i++) if(min>a[rowIndex][i]) min=a[rowIndex][i]; return min; } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.