Java: summing up array elements and displaying
Hi,
I am writing 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}}
2 3 4 5
6 7 8
9 10
11
Total = 28 20 12 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 dispaly the elements in vertical column.
Here is my code
public class AssignmtOneQuiz1 {
public static void main(String args[]) {
int rows, cols;
int sum = 0;
int[ ][ ]MyArray={{2, 3, 4, 5 }, {6, 7,8}, {9, 10}, {11}};
System.out.println("Number of Row= " + value.length);
System.out.println("Number of Column= " + value.length);
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 + " ");
}
}
}
Please help. Thanks
Re: Java: summing up array elements and displaying
A 2 dimensional array actually is a one dimensional array of one dimensional arrays, i.e. MyArray[0] is a one dimensional array with 4 elements. If you want to calculate the sum of all elements, better add them row-wise:
Code:
public int sumRow(int[] row) {
int sum= 0;
for (int i= 0; i < row.length; i++)
sum+= row[i];
return sum;
}
public int sum(int[][] a) {
int sum= 0;
for (int i= 0; i < a.length; i++)
sum+= sumRow(a[i]);
return sum;
}
kind regards,
Jos
Re: Java: summing up array elements and displaying
Hi Jas,
Thanks for your info.
However, the code doesn't still do exactly what i wanted. First I want it to pick all corresponding elements eg all first elements and display, all second elements and display in a second column, all third elements and display in third column, etc.
Then the program sums up the elements PER COLUMN. So its not just summing up everything, but should display sum of every column.
Regards
Re: Java: summing up array elements and displaying
Quote:
Originally Posted by
dambundos
Then the program sums up the elements PER COLUMN. So its not just summing up everything, but should display sum of every column.
That can be done too of course but you have to keep track of a few more (essential) details: first you have to find the longest row in your matrix; next you have to iterate over the row arrays and sum their values, after having checked that the row contains at least so many columns. The code will be a bit uglier.
kind regards,
Jos
Re: Java: summing up array elements and displaying
Hi
Thanks once again
Now could you kindly show me how i could go about doing that for the first two rows in the matrix then I shall continue the rest? Kindly assist. Am stuck on this.
Re: Java: summing up array elements and displaying
Quote:
Originally Posted by
bossyboy
Hi
Thanks once again
Now could you kindly show me how i could go about doing that for the first two rows in the matrix then I shall continue the rest? Kindly assist. Am stuck on this.
If I do it for two rows, I might as well do it for all rows:
1) find the row with the maximum number of columns, this value be m
2) for all columns j in [0, m) do:
3) sum the elements in all rows for column j (if present)
You translate this pseudo code to Java.
kind regards,
Jos
Re: Java: summing up array elements and displaying
hi Jos
Thanks for the alogorithm u gave. I have used and it worsk fin and is able to add.
I wish to ask hwo can i now display the elements so that they appear in columns like below:
2 3 4 5
6 7 8
9 10
11
Do I have to do one by one manually like System.out.print("2"+"3"+"4"+5"); for all rows or is there a better way?
Regards
Re: Java: summing up array elements and displaying
Quote:
Originally Posted by
bossyboy
hi Jos
Thanks for the alogorithm u gave. I have used and it worsk fin and is able to add.
I wish to ask hwo can i now display the elements so that they appear in columns like below:[ ... ]
Do I have to do one by one manually like System.out.print("2"+"3"+"4"+5"); for all rows or is there a better way?
Do it one by one in a loop; read about the printf( ... ) method in the PrintWriter (and PrintStream) class.
kind regards,
Jos