Results 1 to 8 of 8
  1. #1
    core2duo5252 is offline Member
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    Default Adding Values of 2 Arrays

    Kindly Help me Continue the code for this program. It should Display the sum of int myarray [][]= {{0,1,2},{1,0,3}};
    int myarray1 [][]= {{1,2,3},{6,5,4}}; like 0 of myarray and 1 of myarray1. Thanks. Please Help:(



    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    /**
    *
    * @author m303user
    */import java.io.*;
    public class jun_pyo {


    public static void main(String[] args) {
    int myarray [][]= {{0,1,2},{1,0,3}};
    int myarray1 [][]= {{1,2,3},{6,5,4}};


    System.out.println("1st array :");
    for(int row = 0; row < myarray.length; row++) {
    for(int column = 0; column <= myarray.length;column++) {
    System.out.print("" + myarray[row][column]);
    }
    System.out.println();
    }
    {
    System.out.println("2nd array :");
    for(int row = 0; row < myarray1.length;row++) {
    for(int column = 0; column <= myarray1.length;column++) {
    System.out.print(""+ myarray1[row][column]);


    }
    System.out.println(); for (int sum=0;sum<2;sum++)
    {
    System.out.println(" "+myarray1[row]);
    }
    }
    }
    }

    }

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    What do you mean by the sum of a 2D array?
    How did you get 0 for myarray and 1 for myarray1?

  3. #3
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

  4. #4
    core2duo5252 is offline Member
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    Default The output

    Ok so this should be the Output of it

    0 1 2 ---array1
    1 0 3---array2

    1 2 3----array1
    6 5 4---array2


    1 3 5====the sum of array 1
    7 5 7====the sum of array 2

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    So you want to add two arrays the way matrix addition is done?
    Better create an add method then that takes two arrays and returns an array.
    Your add method should check that the two supplied arrays can be added together in the first place before trying to do the addition. The rest is just a bit of math. Try it and post a refined version.

  6. #6
    core2duo5252 is offline Member
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    Default ...

    Im really at loss at this one sir.. Can you not refine my program and rewrite it.

  7. #7
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Not me, you. You do know how to declare a method right?
    Just declare the method as I suggested taking two arrays and returning an array. Then you declare the array that will hold then sum.
    After that you go over the two arrays adding elements at corresponding positions to their corresponding positions in the sum array.
    Also include a separate method that determines if two arrays can be added together or not.

  8. #8
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

    Default

    do you know how to get the size of array?
    like r035198x said, create a method with 2 input int[][] parameter and return int[][] array

    then use 2 for loop (1 outer, 1 inner) to sum up the corresponding filed of array
    assign the resultant value to any one of array, return the resultant array

Similar Threads

  1. Replies: 7
    Last Post: 09-27-2011, 12:29 AM
  2. Replies: 4
    Last Post: 04-01-2009, 03:03 AM
  3. Replies: 0
    Last Post: 05-22-2008, 11:53 AM
  4. Replies: 4
    Last Post: 04-15-2008, 02:04 PM
  5. BlackJack help please,, adding hand values
    By javakid9000 in forum New To Java
    Replies: 1
    Last Post: 11-18-2007, 05:26 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
  •