Thread: is this for loop correct?

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    494

    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.
    Last edited by InvariantLoop; 01-27-2005 at 07:02 PM.
    When no one helps you out. Call google();

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    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. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    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();

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    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?
    When no one helps you out. Call google();

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    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.

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    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();

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Your code:
    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.
    If you understand what you're doing, you're not learning anything.

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    yeah that semicolon is a typo, thanks for pointing it out.
    When no one helps you out. Call google();

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote 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
    */
    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.*

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    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();

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linux for GNU/Linux is not correct?
    By password636 in forum Linux Programming
    Replies: 8
    Last Post: 03-31-2009, 08:30 PM
  2. Is this correct : passing strings?
    By socket in forum C Programming
    Replies: 15
    Last Post: 11-25-2008, 02:03 PM
  3. Replies: 1
    Last Post: 05-26-2004, 12:58 AM
  4. Why I couldn't see the correct format of a file?
    By miketv in forum C Programming
    Replies: 2
    Last Post: 01-23-2002, 10:59 PM
  5. Check if input password is correct
    By kagemand in forum C++ Programming
    Replies: 2
    Last Post: 11-28-2001, 09:28 AM