Results 1 to 8 of 8
- 10-22-2011, 08:26 AM #1
Member
- Join Date
- Oct 2011
- Posts
- 3
- Rep Power
- 0
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
- 10-22-2011, 09:21 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,588
- Blog Entries
- 7
- Rep Power
- 17
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:
kind regards,Java 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; }
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-22-2011, 10:06 AM #3
Member
- Join Date
- Oct 2011
- Posts
- 3
- Rep Power
- 0
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
- 10-22-2011, 10:20 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,588
- Blog Entries
- 7
- Rep Power
- 17
Re: Java: summing up array elements and displaying
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-22-2011, 11:10 AM #5
Member
- Join Date
- Oct 2011
- Posts
- 4
- Rep Power
- 0
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.
- 10-22-2011, 11:55 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,588
- Blog Entries
- 7
- Rep Power
- 17
Re: Java: summing up array elements and displaying
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-22-2011, 01:25 PM #7
Member
- Join Date
- Oct 2011
- Posts
- 4
- Rep Power
- 0
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
- 10-22-2011, 02:34 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,588
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
keep the first N elements of an array only
By aneuryzma in forum New To JavaReplies: 13Last Post: 03-27-2011, 04:03 PM -
Manipulating array elements to zero using for-loops and if-statements in Java
By paulmmj in forum New To JavaReplies: 9Last Post: 02-14-2011, 04:22 AM -
sum of elements in array
By myst in forum New To JavaReplies: 7Last Post: 07-17-2010, 08:36 AM -
Displaying array elements as null except the last index
By vasavi.singh in forum Advanced JavaReplies: 2Last Post: 04-06-2009, 11:42 AM -
Help with array of elements
By zoe in forum New To JavaReplies: 1Last Post: 07-24-2007, 05:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks