Dear all,
I wish to write a Java application program and an applet program that find the sum of all the integers in the array shown below and print them out together with the sum as below:
int[ ][ ]MyArray={{2, 3, 4, 5 }, {6, 7,8}, {9, 10}, {11}}
expected output:
2 3 4 5
6 7 8
9 10
11
Total = 28 20 12 5
(eg col1 6+2+9+11=28, col2: 3+7+10=20, col3: 8+4=12, col4: 5)
That is, the program arranges all first elements in one column, all second elements in another vertical column, etc and displays as output as shown above.. Then it sums them up the corresponding elements and displays them as Total = 24 17 10 4 as above. When I try, am getting wrong values for the sum and am unable to display the elements in vertical column.
Here is my code for the application program part:
public class AssignmtOneQuiz1 {
public static void main(String args[]) {
int sum = 0;
int[ ][ ]MyArray={{2, 3, 4, 5 }, {6, 7,8}, {9, 10}, {11}};
System.out.print("Total = ");
for(int i=0; i<MyArray.length; i++)
{
for(int j=0; j<MyArray[i].length; j++)
sum += MyArray[i][j];
System.out.print(sum + " ");
}
}
Butt it's not giving the required results. lease assist.
Thanks
}