# Thread: is this for loop correct?

1. ## is this for loop correct?

Code:
```{
double k;
double sumsqrd;
sumsqrd = 0;

for (k =0; k <= 5; k++); {

sumsqrd =pow(k,2);
}
{
sumsqrd +=numbers[k]
}
return sumsqrd;
}```
what im trying to do is , having 5 numbers, square each of them and then add their squares.

2. The sqrt() function finds the square root of a number, not the square of a number. Use pow(). Even then, why are you assigning this value to sumsqrd? You haven't defined numbers as an array - make sure you do that. And why do you have that second set of curly braces? Write out the logic to your program as a flow chart on a piece of paper, then try coding it again.

edit: And you shouldn't have semicolons after your for loops.

3. its part of a proptotype thats why the braces are there, i have edit the code to include pow(), the 2nd pair of braces is there so i dont get confused lol

4. Code:
```    double k;
double sum;
double sumsqrd;

sumsqrd = 0;
sum =  0;
for (k =0; k <= 5; k++);
{
sum =+numbers[k];
}

return sumsqrd=pow(sum,2);
}```
figured it out correct?

{
int total;
int k;

for(k=0;k<5;k++)
total+=(k*k);

}

in yor case the loop teminates after k is 5 look into that
and pow function returns of type double and and the return value should be stored in double variable. then why do you need and array there. and more over u havent declared an array there.

6. i havent declared the array here, but the array is declared in the program. this is part of a prototype that adds and squares the values in the array.

Code:
```for (k =0; k <= 5; k++);
{
sum =+numbers[k];
}```
Your for() loop is only incrementing k 6 times because you have that ; stuck on the end. But it looks like you want to execute what's in the following code block (which has the wrong syntax) 6 times instead.

8. yeah that semicolon is a typo, thanks for pointing it out.

9. Originally Posted by InvariantLoop
what im trying to do is , having 5 numbers, square each of them and then add their squares.
Code:
```#include <stdio.h>

int sum_of_squares ( int array[], int size )
{
int i, sum = 0;
for ( i = 0; i < size; ++i )
{
sum += array[i] * array[i];
}
return sum;
}

int main(void)
{
int array[] = {1,2,3,4,5};
int ssq = sum_of_squares ( array, sizeof array / sizeof array[0] );
printf ( "ssq = %d\n", ssq );
return 0;
}

/* my output
ssq = 55
*/```

10. thanks for the input Dave and for another way to doing things, i have gotten passed this issue. what i did is use a for loop to square the values and another nested for loop to add their squares.