Results 1 to 10 of 10
- 01-26-2009, 10:34 PM #1
Member
- Join Date
- Jan 2009
- Posts
- 22
- Rep Power
- 0
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, 10:59 PM #2
Member
- Join Date
- Jan 2009
- Posts
- 22
- Rep Power
- 0
No one has any ideas?
- Java Code:
for(int i = 1; i < 20;Math.pow(i,2)) {
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.
- 01-26-2009, 11:25 PM #4
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])
Java Code:for(int i = 1,int j=1; i =< 400;j=Math.pow(i,2))
You will have to replace J in:
Java Code:C.setValue(i-1, i);
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 01-26-2009, 11:38 PM #5
Member
- Join Date
- Jan 2009
- Posts
- 22
- Rep Power
- 0
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.
- 01-26-2009, 11:59 PM #6
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])
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 01-27-2009, 12:14 AM #7
Member
- Join Date
- Jan 2009
- Posts
- 22
- Rep Power
- 0
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, 12:23 AM #8Java Code:
for(int i=0;i <= 20;i++) { int j = (int) Math.pow(i + 1,2); C.setValue(i, j); }
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 01-27-2009, 12:32 AM #9
Member
- Join Date
- Jan 2009
- Posts
- 22
- Rep Power
- 0
I needed 1,4,9,16...400 excluding zero. Thats why there is an i+1.
- 01-27-2009, 01:33 AM #10
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,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Similar Threads
-
Creating a New Method for Square Root Loop
By SapphireSpark in forum New To JavaReplies: 14Last Post: 02-25-2009, 01:21 PM -
8-Square puzzle loop
By SapphireSpark in forum New To JavaReplies: 7Last Post: 12-04-2008, 07:21 PM -
Reading input file into an array
By littlefire in forum New To JavaReplies: 6Last Post: 10-18-2008, 11:51 PM -
input placed in array
By smilejava in forum New To JavaReplies: 5Last Post: 11-12-2007, 07:29 AM -
input placed in array
By smilejava in forum New To JavaReplies: 1Last Post: 11-05-2007, 12:32 PM
Bookmarks