Results 1 to 10 of 10
  1. #1
    dalangley is offline Member
    Join Date
    Jan 2009
    Posts
    22
    Rep Power
    0

    Default Perfect Square Array Input Using For Loop

    Hello there, I have run into a bit of a wall/mental block on what I believe to be a very easy bit of code. Here is the code:

    // Creates a new IntArray, C, with a size of 20.
    IntArray C = new IntArray(20);

    for(int i = 1; i < 20;Math.pow(i,2))
    {
    C.setValue(i-1, i);
    }

    System.out.println("Array C =" + C);


    The question put forth to me for this particular section was to fill the Array, C, with the first 20 (size of the array) perfect squares using a for loop specifically. Now I am not sure if it is my syntax or logic because when I compile and run in Netbeans it seems as if it skips over the entire loop and outputs C with integer 1 placed in index 0.

    // Output:

    Array C =
    1 0 0 0 0
    0 0 0 0 0
    0 0 0 0 0
    0 0 0 0 0
    BUILD SUCCESSFUL (total time: 2 seconds)



    What I need is 1, 4, 9, 16.... up until 400, i.e. first 20 perfect squares. So how do I reconstruct the for loop? (By the way setValue is a method created in the driver class).

  2. #2
    dalangley is offline Member
    Join Date
    Jan 2009
    Posts
    22
    Rep Power
    0

    Default

    No one has any ideas?

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

    Default

    Java Code:
    for(int i = 1; i < 20;Math.pow(i,2)) {
    That's not how to code a for loop. It should start at 0 (since arrays are 0-based) and the 3rd portion of the loop should advance i. Please go to the Sun intro to Java tutorials and review this section.

    Also, you probably shouldn't bump your thread when it's only 25 minutes old. With patience you should get answers here.
    Last edited by Fubarable; 01-26-2009 at 11:27 PM.

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default for loop..

    I think the incrementor in the for statement is wrong.
    Java Code:
    for(int i = 1; i < 20;[B][COLOR="Red"]Math.pow(i,2)[/COLOR][/B])
    Te red section indicates how to increment the counter "i". Maybe you're looking for something like:

    Java Code:
    for(int i = 1,int j=1; i =< 400;j=Math.pow(i,2))
    This means that it loop up to 400 times and the incrementor is variable J which will increment 1, 4, 9, 16, etc

    You will have to replace J in:
    Java Code:
    C.setValue(i-1, i);
    But I'll leave that up to you.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    dalangley is offline Member
    Join Date
    Jan 2009
    Posts
    22
    Rep Power
    0

    Default

    I initially set i = 1 because i was using it in the update portion of the loop, that and squaring 0 wouldnt help me. CJSL helped me out on the loop, but the setValue statement is now not working correctly. I made the following changes to CJSL's version. I think it should be this way.

    for(int i = 1, int j=1; i <= 20;j = Math.pow(i,2))

    Where i is the index and j is the value in said index.

    My object code should work like this:

    C.setValue(i, j);

    But it says i and j are not variables. for some reason.

    Edit: i also has no update, so it would not increment as far as i can see. j is the only thing changing, still doesnt explain the fact that the Object.method wont work.

    for(int i = 1, int j=1; i <= 20;j = Math.pow(i,2))
    {
    C.setValue(i, j);
    }
    Last edited by dalangley; 01-26-2009 at 11:43 PM.

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default hhhmmm...

    Yes, that looks better... shame on me... duh... try the following:
    Java Code:
    for(int i = 1, int j=1; i <= 20;j = Math.pow(i,2),[B][COLOR="Blue"]i++[/COLOR][/B])
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  7. #7
    dalangley is offline Member
    Join Date
    Jan 2009
    Posts
    22
    Rep Power
    0

    Default

    for(int i=0;i <= 20;i++)
    {
    int j = (int) Math.pow(i + 1,2);
    C.setValue(i, j);
    }


    After a little bit of moving around, this setup works perfectly. It was a matter of placement. Problem Solved. Thanks for your help!

  8. #8
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    Java Code:
    for(int i=0;i <= 20;i++)
    {
    int j = (int) Math.pow(i + 1,2);
    C.setValue(i, j);
    }
    OK.. but it's going to look 21 times. Either make int "i =1" or "i < 20" to avoid that. If you make "i=1", then you'll have to change the math.pow to "Math.pow(i ,2);"

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  9. #9
    dalangley is offline Member
    Join Date
    Jan 2009
    Posts
    22
    Rep Power
    0

    Default

    I needed 1,4,9,16...400 excluding zero. Thats why there is an i+1.

  10. #10
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default if it's working...

    If it's working and hasn't blown the array out of the water, then I guess it's working, but a "for" loop starting at 0 and ending until 20 will loop 21 times (independent of what you put in the "for" brackets).
    Again, if it's working, and giving the correct results (which I doubt) then ... I guess it's working. Maybe I'm not understnading how it's supposed to work.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Creating a New Method for Square Root Loop
    By SapphireSpark in forum New To Java
    Replies: 14
    Last Post: 02-25-2009, 01:21 PM
  2. 8-Square puzzle loop
    By SapphireSpark in forum New To Java
    Replies: 7
    Last Post: 12-04-2008, 07:21 PM
  3. Reading input file into an array
    By littlefire in forum New To Java
    Replies: 6
    Last Post: 10-18-2008, 11:51 PM
  4. input placed in array
    By smilejava in forum New To Java
    Replies: 5
    Last Post: 11-12-2007, 07:29 AM
  5. input placed in array
    By smilejava in forum New To Java
    Replies: 1
    Last Post: 11-05-2007, 12:32 PM

Posting Permissions

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