# Thread: for loop again and array values, need help

1. ## for loop again and array values, need help

can someone help me out, im stuck. what im trying to do is square each value that the array holds, but my loop only squares the array index, not the value that it holds. any suggestions?

j[0] =4
j[1] =2

my loop only squares 0^2 =0 and 1^2 =1, instead of 4^2 and 2^2
Code:
```for (j=0;j<count;j++)
{
numbers[j]= pow(j,2);
}```

2. what you are doing in your code is sending down , j ( which is the index number) to the power function.

Code:
```for (j=0;j<count;j++)  //j is used to traverse the array
{
numbers[j]= pow(j,2); //over here you are squaring the index.
}```
Just change your code to this , and it will do what you want

Code:
```for (j=0;j<count;j++)
{
numbers[j]= pow(numbers[j],2);
}```

3. hey thanks for the help so close yet i couldnt see it

4. he he , no problem

5. i need some help on this again, even though the above code is correct and it squares the values, when i run the program it displays the values squared, i want the values to be displayed as they are in the file. for example
x[0] = 3
x[1]= 2

the program should output:
3
2

the sum of these values when squared is: 14.
this is what i have so far.
Code:
```for(j=0;j<count; j++)
{
numbers[j]= pow(numbers[j],2);
}

for(j=0; j<count;j++)
{
sumofsqrs+=numbers[j];
}

return sumofsqrs;```
the code does work correctly for squaring and then adding the squares but the values are displayed squared, the program should display the original values in the array not the square of the values. any ideas where the bug is?

6. Well, you changed the values of your array when squaring them. Your array contains only squared values now. You need to access the file again...
I hope I understand correctly what you mean.

7. Originally Posted by InvariantLoop
Code:
```for(j=0;j<count; j++)
{
numbers[j]= pow(numbers[j],2);
}

for(j=0; j<count;j++)
{
sumofsqrs+=numbers[j];
}

return sumofsqrs;```
the code does work correctly for squaring and then adding the squares but the values are displayed squared, the program should display the original values in the array not the square of the values. any ideas where the bug is?
Yes -- where you modify the array. (Worth another look?)

D'oh! Too many open tabs.

8. thanks again Dave i looked at your previous post again, it seems to work as i want it now.