Results 1 to 5 of 5
  1. #1
    cres965 is offline Member
    Join Date
    Mar 2017
    Posts
    3
    Rep Power
    0

    Default There is an error with the c++ in the code and I cannot figure out why.

    public class Grid
    {
    public static void main(String args[])
    {
    int [][] my2D= {{1,5,4,8,7,3},{2,4,3,5,7,6},{2,4,3,3,6,0},{9,8,9, 9,4,1}};
    int [][] newArray= new int [my2D.length][my2D[0].length];

    for(int r=0;r<my2D.length;r++)
    {
    for(int c=0; c<2D[0].length; c++)
    if(r%2==0 && r!=0)
    newArray[r][c]=my2D[r-1][c];
    else newArray[r][c]=my2D[r][c];
    }

    }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: There is an error with the c++ in the code and I cannot figure out why.

    That's not where the error is. Post the error message here -- it contains valuable information.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: There is an error with the c++ in the code and I cannot figure out why.

    There might be an issue in the 4th line from the bottom: the second parameter in the for-loop might be my2D[0].length rather than 2D[0].length. Are all the curly brackets set correctly?

  4. #4
    cres965 is offline Member
    Join Date
    Mar 2017
    Posts
    3
    Rep Power
    0

    Default Re: There is an error with the c++ in the code and I cannot figure out why.

    Yup, that was the mistake. Thanks.

  5. #5
    cres965 is offline Member
    Join Date
    Mar 2017
    Posts
    3
    Rep Power
    0

    Default Re: There is an error with the c++ in the code and I cannot figure out why.

    array required, but double found
    for(int c=0; c<2D[0].length; c++)

    That was the error message but benji pointed out it was the my2D[0].length rather than 2D[0].length which fixed it. Thanks.

Similar Threads

  1. Can't Figure Out What Memoization Code Is Doing
    By Rendar- in forum New To Java
    Replies: 2
    Last Post: 01-28-2014, 08:10 PM
  2. Replies: 5
    Last Post: 01-16-2014, 10:22 AM
  3. Replies: 5
    Last Post: 04-14-2012, 12:18 PM
  4. Replies: 5
    Last Post: 06-05-2011, 02:19 AM
  5. Replies: 3
    Last Post: 01-11-2010, 06:48 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
  •