6. Given the two dimensional array below: public class Class1 { public static vo
ID: 3594447 • Letter: 6
Question
6. Given the two dimensional array below:
public class Class1 {
public static void main (String [] args) {
int[][] list = { {9, 8},
{6, 5},
{3, 2},
{7, 7} };
}
}
a. What is the value of list.length _______________
b. What is the value of list[0].length _______________
c. Hand-write a loop to print every element of the array, list, (4 rows and 2 columns of output)
D.Hand-write a loop to print every element of the second row of the Array
e. Hand-write a loop to print every element of the first column of the Array
f. How would you sum all the elements of the Array? Hand-write a loop to do so.
g. How do you find the max or the min in the array? Hand-write a loop to do so.
h. How do you sum the elements of a column of an array? Hand-write a loop to do so.
i. How do you sum the elements of a row of an array? Hand-write a loop to do so.
Explanation / Answer
a. 4
b. 2
c.
int i,j;
for(i=0;i<4;i++){
for(j=0;j<2;j++)
System.out.print(list[i][j]+" ");
}
d.
int j;
for(j=0;j<2;j++){
System.out.print(list[1][j]+" "); // second row of an array has the index as 1, so we are using 1.
}
e.
int i;
for(i=0;i<4;i++){
System.out.print(list[i][0]+" ");
}
f.
int sum =0;
for(i=0;i<4;i++){
for(j=0;j<2;j++){
sum+=list[i][j];
}
}
System.out.println("sum of the elements of the list "+sum);
g.
int maximum = Integer.MIN_VALUE;
int minimum = Integer.MAX_VALUE;
for(int i = 0; i < list.length; i++) {
Arrays.sort(list[i]); // to use this inbuilt function ,you need to import Array class using "import java.util.Arrays;"
if(list[i][0] < minimum) minimum = list[i][0];
if(list[i][list[i].length - 1] > maximum) maximum = list[i][list[i].length - 1];
}
System.out.println("Minimum = " + minimum);
System.out.println("Maximum = " + maximum);
h.
int sum1=0;
for(i=0;i<4;i++){
sum1+=list[i][0]; //1st column has index as 0
}
System.out.println("Sum of the elements of the 1st Column "+sum1);
i.
nt sum2=0;
for(j=0;j<2;j++){
sum2+=list[1][j]; // 2nd row has index as 1
}
System.out.println("Sum of the elements of the 2nd Row "+sum2);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.