Results 1 to 13 of 13
  1. #1
    Javanoobs is offline Member
    Join Date
    Feb 2011
    Posts
    18
    Rep Power
    0

    Default How to write a program to calculate and display sum in two dimensional array?

    I have a college tutorial question that required me to write a program which need to calculate and display the sum of each column of a two dimensional array. For example, in the following array

    2 6 -1 5
    8 2 7 -5
    1 0 4 4

    the method shoudl display the following
    The sum of column 0 is 11
    The sum of column 1 is 8
    The sum of column 2 is 10
    The sum of column 3 is 4

    Assume that in the calling program the array A is declared as float A[MAXROW][MAXCOL]; where MAXROW and MAXCOL are global int constants.

    The method should have a two dimensional array as its only parameter and shold not return any values.

    I have roughly wrote something on a paper, but i jst dun get the idea how to link all the tings 2gether, can anyone pls help me of this?

    public class array
    public static final MAXROW{
    public static final MAXCOL{

    public void calculate Sum(){
    float A[MAXROW][MAXCOL];
    for (int i = 0; i<MAXROW.length; i++)
    {
    for (int j = 0; j<MAXCOL.length;j++)
    {
    (I don't have idea in tis part)
    }
    }

    i knw my code is poor and not completed, but im in the progress of learning, im using netbean 6.9.1, pls help me out ..thx

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

    Default

    Do it 'bottom up'; i.e. calculate the sum of one column; let j be the column for which you have to add all the numbers of a two dimensional array 'a'. The number of rows is known, i.e. a.length, so finding that total is just one loop:

    Java Code:
    int sum= 0;
    for (int row= 0; row < a.length; a++)
       sum+= a[row][j];
    All you have to do is apply the above code snippet to all columns of the array and add a few System.out.println( ... ) statements.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Javanoobs is offline Member
    Join Date
    Feb 2011
    Posts
    18
    Rep Power
    0

    Default

    may i knw y it ask me to declared the [maxrow][maxcol] and declared it as global int? if lik u say apply 1 loop, and how to deal wit tis maxrow and maxcol?

  4. #4
    Javanoobs is offline Member
    Join Date
    Feb 2011
    Posts
    18
    Rep Power
    0

    Default

    and also do i need to cr8 a new class for calculating the array? or do it in the main?

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

    Default

    Quote Originally Posted by Javanoobs View Post
    may i knw y it ask me to declared the [maxrow][maxcol] and declared it as global int? if lik u say apply 1 loop, and how to deal wit tis maxrow and maxcol?
    and also do i need to cr8 a new class for calculating the array? or do it in the main?
    If you want to ask me a question please use (more or less) proper English; I also reply in (more or less) proper English.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Im having a hard time understanding your questions, but I think the easiest way to do this would be to create a static method to produce the sum of a single array, then loop through the 2d array applying the static method and storing the values in a new array.

  7. #7
    Javanoobs is offline Member
    Join Date
    Feb 2011
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    If you want to ask me a question please use (more or less) proper English; I also reply in (more or less) proper English.

    kind regards,

    Jos
    my apologize...because I am having hard time to understand your explanation, Im stil a beginner in this, so hope you could explain this in details, you help will be fully appreciated...

  8. #8
    Javanoobs is offline Member
    Join Date
    Feb 2011
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    Im having a hard time understanding your questions, but I think the easiest way to do this would be to create a static method to produce the sum of a single array, then loop through the 2d array applying the static method and storing the values in a new array.
    ok...can you provide some coding example so I can get exactly what you said? your help will be kindly appreciated...:)

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    How would you find the sum of a single array? Each index in a 2d array is actually a single array

    Java Code:
    int[][] x = new int[4][];
    int[0] = { 1, 2, 3 };
    int[1] = { 4, 5, 2 };
    int[2] = { 6, 8, 9 };
    int[3] = { 2, 5, 9 , 10 };
    is a 2d array, so it simply comes down to finding the sum of a single array then applying that process to each element of a 2d array.

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

    Default

    Quote Originally Posted by Javanoobs View Post
    my apologize...because I am having hard time to understand your explanation, Im stil a beginner in this, so hope you could explain this in details, you help will be fully appreciated...
    As I wrote you should apply my code snippet to a single column j of your matrix. It would be easiest to turn it into a little method, like this:

    Java Code:
    private void calculateColumn(int[][] array, int j) {
       // my code snippet goes here
       // and a System.out.println( ... ) statement goed here
    }
    If you want to print the results of all columns you should apply the method above to each and every column of your matrix, like this:

    Java Code:
    public void calculate(int[][] array) {
       for (int col= 0; col < array[0].length; col++)
          calculateColumn(array, col);
    }
    That's all the code it takes ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    618
    Rep Power
    4

    Default

    generic, anonymously class, interface )
    Java Code:
    import java.util.ArrayList;
    import java.util.List;
    
    public class SumEx {
        public static void main(String[] arg) {
            Integer[][] mass = {
                    {2, 6, -1, 5},
                    {8, 2, 7, -5},
                    {1, 0, 4, 4},
            };
            System.out.println("Summ column 0 : " + summ(build(mass, 0)));
            System.out.println("Summ column 1 : " + summ(build(mass, 1)));
            System.out.println("Summ column 2 : " + summ(build(mass, 2)));
            System.out.println("Summ column 3 : " + summ(build(mass, 3)));
        }
    
        private static <T extends Number> List<T> build(T[][] mass, int index) {
            List<T> list = new ArrayList<T>();
            for (T[] t : mass) {
                list.add(t[index]);
            }
            return list;
        }
    
        private static <T extends Number> double summ(List<T> list) {
            return forEach(list, new Operation<Number>() {
                @Override
                public Number invoke(Number result, Number number) {
                    return result.doubleValue() + number.doubleValue();
                }
            });
        }
    
        interface Operation<A extends Number> {
            public A invoke(A result, A a);
        }
    
        private static <T extends Number> double forEach(List<T> list, Operation<Number> operation) {
            double result = 0D;
            for (T t : list) {
                result = operation.invoke(result, t).doubleValue();
            }
            return result;
        }
    }
    Skype: petrarsentev
    http://TrackStudio.com

  12. #12
    Javanoobs is offline Member
    Join Date
    Feb 2011
    Posts
    18
    Rep Power
    0

    Default

    thanks to josah and sunde...i know how it goes along now...thanks you guys help, and sorry for disturbing you both...peace =)

  13. #13
    Javanoobs is offline Member
    Join Date
    Feb 2011
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by Petr View Post
    generic, anonymously class, interface )
    Java Code:
    import java.util.ArrayList;
    import java.util.List;
    
    public class SumEx {
        public static void main(String[] arg) {
            Integer[][] mass = {
                    {2, 6, -1, 5},
                    {8, 2, 7, -5},
                    {1, 0, 4, 4},
            };
            System.out.println("Summ column 0 : " + summ(build(mass, 0)));
            System.out.println("Summ column 1 : " + summ(build(mass, 1)));
            System.out.println("Summ column 2 : " + summ(build(mass, 2)));
            System.out.println("Summ column 3 : " + summ(build(mass, 3)));
        }
    
        private static <T extends Number> List<T> build(T[][] mass, int index) {
            List<T> list = new ArrayList<T>();
            for (T[] t : mass) {
                list.add(t[index]);
            }
            return list;
        }
    
        private static <T extends Number> double summ(List<T> list) {
            return forEach(list, new Operation<Number>() {
                @Override
                public Number invoke(Number result, Number number) {
                    return result.doubleValue() + number.doubleValue();
                }
            });
        }
    
        interface Operation<A extends Number> {
            public A invoke(A result, A a);
        }
    
        private static <T extends Number> double forEach(List<T> list, Operation<Number> operation) {
            double result = 0D;
            for (T t : list) {
                result = operation.invoke(result, t).doubleValue();
            }
            return result;
        }
    }
    once again, thanks to you peter for your help, but do i need to do so much abstract coding? I mean this is just a simple array calculation program, just my opinion...hope to seek your advice...

Similar Threads

  1. Program in Java To calculate GCD of n numbers.?
    By ankitsinghal_89 in forum New To Java
    Replies: 4
    Last Post: 02-15-2011, 09:23 AM
  2. Replies: 1
    Last Post: 02-08-2011, 09:11 AM
  3. Replies: 1
    Last Post: 04-27-2010, 11:29 PM
  4. Multi-dimensional array program-help!
    By bobmasta5 in forum New To Java
    Replies: 13
    Last Post: 03-15-2009, 07:21 PM
  5. Why my program cannot calculate the decimal value?
    By pearllymary78 in forum New To Java
    Replies: 4
    Last Post: 06-23-2008, 12:52 AM

Posting Permissions

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