For loop help, please ...

This is a discussion on For loop help, please ... within the C Programming forums, part of the General Programming Boards category; I'm writing a program to calculate the first 10 multiples of the absolute value of (x^2 - 20). Everything seems ...

  1. #1
    Unregistered
    Guest

    For loop help, please ...

    I'm writing a program to calculate the first 10 multiples of the absolute value of (x^2 - 20). Everything seems to be working fine except that I'm getting parse and compile errors on my for loop, and it's driving me insane tryig to figure out what isn't working.

    Any help is appreciated. Here's the code ...


    #include <stdio.h>

    float numsquared( float ) ;
    float abs_value( float ) ;

    int main()
    {

    float x, xsq, abval, multiple ;
    int counter = 1 ;

    printf( "Given x, I will calculate the first 10 multiples\n" ) ;
    printf( "of the absolute value of (x-squared - 20).\n" ) ;
    printf( "Please enter the value of x: " ) ;
    scanf( "%f", &x );
    xsq = numsquared( x ) ;
    abval = abs_value( xsq ) ;

    for (counter <= 10)
    {
    multiple = abval*counter ;
    counter++ ;
    printf( "%f\n", multiple ) ;
    }

    printf( "Thank you!\n\n" ) ;

    return 0 ;

    }

    float numsquared( float num )
    {

    float square ;

    square = num*num ;

    return square ;
    }

    float abs_value( float numsq )
    {
    float check = 0 ;
    float abs_val = 0 ;

    check = numsq-20 ;

    if (check >= 0)
    abs_val = check ;
    else
    abs_val = check*(-1) ;

    return abs_val;
    }

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    > for (counter <= 10)

    This isn't how you make a for loop

    try
    for(counter = 1; counter <= 10; counter++)

    Also - take the counter++ out of the middle of the loop unless you want your loop to increment twice every loop.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    A for loop is usally written like this
    Code:
    for(int i= 0; i < 20; i++)
    {
        dosomething();
    }
    What you have is more like a while loop

  4. #4
    Unregistered
    Guest
    I tried that, but I must have screwed up the syntax somewhere, because it works.

    Believe it or not, my textbook actually gives several examples of for loops constructed like that. I had C four or five years ago, and I thought it seemed funny, but since it's in the book, I figured it must work.

    Anyhow, I'm all good now. Thanks for the help!

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    > for(int i= 0; i < 20; i++)
    This is C++

    > for (counter <= 10)
    Examine the book very carefully, because this is valid ( watch the birdies ; )

    for ( ; counter <= 10 ; )

    You do the
    counter = 1 outside the loop, and
    counter++ inside the loop

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    My teacher told me the same thing.

    Code:
    int i;
    for(i= 0; i < 20; i++)
    {
        dosomething();
    }

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    Sure this is valid >for ( ; counter <= 10 ; )<
    But IMHO I think that it is alot cleaner to write while( counter <= 10) and use for loops for the way they where intended.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21