Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Is That right?!

  1. #1
    ahmedzica is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default Is That right?!

    Given a problem that says Use a for statement to initialize each element for the sum of its indices . Assume that a and b are declared as control variables

    and Here's my code

    public static void main(String[] args) {
    // TODO code application logic here
    int[][] indicesSum=new int[10][10];

    for (int x=0;x<indicesSum.length; x++){
    for (int y=0;y<indicesSum.length; y++) {

    indicesSum[x][y]=x+y;
    System.out.println(indicesSum[x][y]);

    }
    }

    }

    is that right?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Is That right?!

    Your second for loop should not go to y < indicesSum.length but rather y < indicesSum[x].length. This is in particularly important if the number of rows does not equal the number of columns.

  3. #3
    ahmedzica is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default Re: Is That right?!

    Oh i see thanks but the whole programs is right and it couldn't be done for a uni-dimensional array,right?

  4. #4
    ahmedzica is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default Re: Is That right?!

    ????

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Is That right?!

    Quote Originally Posted by ahmedzica View Post
    Oh i see thanks but the whole programs is right
    Run it and test the array that is returned to see if it behaves as expected.

    ...and it couldn't be done for a uni-dimensional array,right?
    If you are supposed to add two indices, then a uni-dimensional array wouldn't work.
    ahmedzica likes this.

Posting Permissions

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