Results 1 to 8 of 8
  1. #1
    dambundos is offline Member
    Join Date
    Oct 2011
    Posts
    3
    Rep Power
    0

    Default 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

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default 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:

    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;
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    dambundos is offline Member
    Join Date
    Oct 2011
    Posts
    3
    Rep Power
    0

    Default 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

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default Re: Java: summing up array elements and displaying

    Quote Originally Posted by dambundos View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    bossyboy is offline Member
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default 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.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default Re: Java: summing up array elements and displaying

    Quote Originally Posted by bossyboy View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    bossyboy is offline Member
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default 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

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default Re: Java: summing up array elements and displaying

    Quote Originally Posted by bossyboy View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. keep the first N elements of an array only
    By aneuryzma in forum New To Java
    Replies: 13
    Last Post: 03-27-2011, 05:03 PM
  2. Replies: 9
    Last Post: 02-14-2011, 05:22 AM
  3. sum of elements in array
    By myst in forum New To Java
    Replies: 7
    Last Post: 07-17-2010, 09:36 AM
  4. Displaying array elements as null except the last index
    By vasavi.singh in forum Advanced Java
    Replies: 2
    Last Post: 04-06-2009, 12:42 PM
  5. Help with array of elements
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 06:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •