what im trying to do is , having 5 numbers, square each of them and then add their squares.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.Code:{ double k; double sumsqrd; sumsqrd = 0; for (k =0; k <= 5; k++); { sumsqrd =pow(k,2); } { sumsqrd +=numbers[k] } return sumsqrd; }
Last edited by InvariantLoop; 01-27-2005 at 07:02 PM.
When no one helps you out. Call google();
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.
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
When no one helps you out. Call google();
figured it out correct?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); }
When no one helps you out. Call google();
try out this code which could help you
{
int total;
int k;
for(k=0;k<5;k++)
total+=(k*k);
return total;
}
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.
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.
When no one helps you out. Call google();
Your code:
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.Code:for (k =0; k <= 5; k++); { sum =+numbers[k]; }
If you understand what you're doing, you're not learning anything.
yeah that semicolon is a typo, thanks for pointing it out.
When no one helps you out. Call google();
Originally Posted by InvariantLoopCode:#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 */
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
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.
When no one helps you out. Call google();