Can someone help me modify these programs? 1. Modify the following:<?xml:namespa
ID: 3549083 • Letter: C
Question
Can someone help me modify these programs?
1. Modify the following:<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
(a) The Average of all scores.
(b) The Average score for each row.
class
gradeExample
{
public static void main( String[] arg )
{
// declare and construct a 2D array
int[][] gradeTable =
{ {99, 42, 74, 83, 100},
{90, 91, 72, 88, 95},
{88, 61, 74, 89, 96},
{61, 89, 82, 98, 93},
{93, 73, 75, 78, 99},
{50, 65, 92, 87, 94},
{43, 98, 78, 56, 99} };
System.out.println("grade 0,0: " + gradeTable[0][0]);
System.out.println("grade 2,4: " + gradeTable[2][4]);
gradeTable[5][3] = 99 ;
int sum = gradeTable[0][1] + gradeTable[0][2] ;
System.out.println( "sum: " + sum );
}
}
2. Modify the following:
(a) Print out the total number of elements in the array.
(b) Print out the total number of elements in each row
class
UnevenExample2
{
public static void main( String[] arg )
{
// declare and construct a 2D array
int[][] uneven =
{ { 1, 9, 4 },
{ 0, 2},
{ 0, 1, 2, 3, 4 } };
System.out.println("Length is: " + uneven.length );
}
}
Explanation / Answer
// PROGRAM 1.
class gradeExample
{
public static void main( String[] arg )
{
// declare and construct a 2D array
int[][] gradeTable =
{ {99, 42, 74, 83, 100},
{90, 91, 72, 88, 95},
{88, 61, 74, 89, 96},
{61, 89, 82, 98, 93},
{93, 73, 75, 78, 99},
{50, 65, 92, 87, 94},
{43, 98, 78, 56, 99} };
System.out.println("grade 0,0: " + gradeTable[0][0]);
System.out.println("grade 2,4: " + gradeTable[2][4]);
gradeTable[5][3] = 99 ;
// int sum = gradeTable[0][1] + gradeTable[0][2] ;
int sum = 0;
int[] row_average = new int[7];
for(int i=0; i<gradeTable.length; i++)
{
row_average[i] = 0;
for(int j=0; j<gradeTable[i].length; j++)
{
sum = sum + gradeTable[i][j];
row_average[i] = row_average[i] + gradeTable[i][j];
}
}
System.out.println("sum: " + sum);
System.out.println("Average: " + (double)(sum)/35);
for(int i=0; i<7; i++)
System.out.println("Row " + (i+1) + " Average : " + (double)(row_average[i])/5);
}
}
// PROGRAM 2.
class UnevenExample2
{
public static void main( String[] arg )
{
// declare and construct a 2D array
int[][] uneven =
{ { 1, 9, 4 },
{ 0, 2},
{ 0, 1, 2, 3, 4 } };
int total_elements=0;
for(int i=0; i<uneven.length; i++)
{
total_elements+=uneven[i].length;
System.out.println("Row " + (i+1) + " Length is: " + uneven[i].length );
}
System.out.println("Total elements is: " + total_elements );
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.