Results 1 to 5 of 5
  1. #1
    ShinTheWolf is offline Member
    Join Date
    Jun 2014
    Posts
    11
    Rep Power
    0

    Default Having issues doing Arrays

    Good evening. I am having a small issue with my homework and I have no idea if I am tackling this assignment correctly. I'm still trying to figure out how to understand arrays and I think i have the general gist of it but even then I don't really think so. This is my assignment and what I need to do.

    - Create a 10 x 10 two dimensional array of int data type
    - Load each index location in the array with the sum of it's row + column
    - Create and algorithm that will add all the values in the diagonal indexes
    from 0,0 to 9,9 and print the total
    - Create and algorithm that will add all the values in the diagonal indexes
    from 0,9 to 9,0 and print the total
    - Print the addition of both diagonals
    How:
    - From the main method create the rows and columns variables with their values
    - From the main method call a method named arrays passing rows and columns
    - From the arrays method do the rest of the program

    This is what I currently have. I know I have created the array but everytime I run the program it gives me a 10x10 matrix with just "zeros". I dont know what I am doing wrong and where to even begin with adding the values

    Java Code:
    public class KendalJPgm1
    {   
    
        public static void main(String args[])
        {
            int[][] matrix = new int[10][10];
            rows = 0; cols = 0;
      
            
            for(int rows = 0; rows < matrix.length; rows++)
            {
                for (int cols = 0; cols < matrix[rows].length; cols++)
                    System.out.printf(matrix[rows][cols] + "\n");
                System.out.println();
            }
        }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Having issues doing Arrays

    everytime I run the program it gives me a 10x10 matrix with just "zeros"
    To have a value in a variable besides the default value, the variable must be assigned a value.
    Where does the code assign any values to the elements of the array?
    You need to do the second step in you assignment.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    ShinTheWolf is offline Member
    Join Date
    Jun 2014
    Posts
    11
    Rep Power
    0

    Default Re: Having issues doing Arrays

    that is where I am lost is the second step. Do i just list it like so

    [0][9]
    [1][8]
    [2][7]... and so on?

  4. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    878
    Rep Power
    11

    Default Re: Having issues doing Arrays

    u can assign like this...use loops to do this below
    matrix[0][0] = 1;
    matrix[0][1] = 2;
    matrix[1][0] = 4;
    matrix[1][1] = 5;
    Ramya:cool:

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Having issues doing Arrays

    The second step:
    Load each index location in the array with the sum of it's row + column
    seems very clear to me. Can you explain what problems you are having with it?
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 4
    Last Post: 06-28-2014, 06:00 PM
  2. Copying Single Arrays to 2-D Arrays
    By jmscarlet9 in forum New To Java
    Replies: 7
    Last Post: 04-03-2012, 12:17 AM
  3. Casting Enum Type arrays to object type arrays
    By nmvictor in forum Advanced Java
    Replies: 4
    Last Post: 02-17-2012, 01:49 PM
  4. arrays and multidimensional arrays
    By belfast09 in forum New To Java
    Replies: 5
    Last Post: 06-14-2011, 02:28 PM
  5. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 02:40 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
  •