# Perfect Square Array Input Using For Loop

• 01-26-2009, 11:34 PM
dalangley
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).
• 01-26-2009, 11:59 PM
dalangley
No one has any ideas?
• 01-27-2009, 12:25 AM
Fubarable
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.
• 01-27-2009, 12:25 AM
CJSLMAN
for loop..
I think the incrementor in the for statement is wrong.
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:

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:
Code:

`C.setValue(i-1, i);`
But I'll leave that up to you.

Luck,
CJSL
• 01-27-2009, 12:38 AM
dalangley
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);
}
• 01-27-2009, 12:59 AM
CJSLMAN
hhhmmm...
Yes, that looks better... shame on me... duh... try the following:
Code:

`for(int i = 1, int j=1; i <= 20;j = Math.pow(i,2),[B][COLOR="Blue"]i++[/COLOR][/B])`
Luck,
CJSL
• 01-27-2009, 01:14 AM
dalangley
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!
• 01-27-2009, 01:23 AM
CJSLMAN
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
• 01-27-2009, 01:32 AM
dalangley
I needed 1,4,9,16...400 excluding zero. Thats why there is an i+1.
• 01-27-2009, 02:33 AM
CJSLMAN
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