logic error

This is a discussion on logic error within the C Programming forums, part of the General Programming Boards category; Code: I am using function to find the range of a projectile, but whenever I try to run the program ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    15

    logic error

    Code:
    I am using function to find  the range of a projectile, but whenever I try to run the program it will return zero 0.00. Can someone help me solve this logic problem. thanks in advance.
    
    #include <stdio.h>
    #include <math.h>
    
    double distance (double a, double v, double g);
    int square(int y);
    int main(void)
    {
            double v, d, h, datha, degrees, velocity;
            const int pi=3.14159265359;
            const double g= 9.8;
    
            printf(" enter the angle in degrees");
            scanf("%.2f", &degrees);
            datha = degrees * pi/180;
            printf("Enter the initial veloity\n");
            scanf("%.2f\n", &v);
            velocity = square(v);
            d= distance(datha, velocity,g);
            printf("the reange of the projectile was %.2f", d);
            return 0;
    }
    int square(int y){
            return pow(y,2);
    }
    double distance(double a, double v, double g ){
            return v *sin(2*a)/g;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,919
    Well for starters int's can't contain fractional parts:
    Code:
     const int pi=3.14159265359; //????
    Second why are you using a floating point function (pow) to return an int?

    Third you are misusing the scanf() function. You need to insure the format specifier matches the variable. Your's don't match, "%f" is the specifier for a float, not a double.

    Note I didn't look for any "logic" errors, the syntax errors may be your problem. You may want to insure your compiler is generating the maximum number of warnings and never ignore warnings.

    Jim
    Last edited by jimblumberg; 03-30-2013 at 12:55 PM.

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    15
    it work, I appreciated all your help and time thxxx :-P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Possible Logic Error
    By seanksg in forum C Programming
    Replies: 8
    Last Post: 03-14-2011, 07:42 PM
  2. Error in logic
    By Mentallic in forum C Programming
    Replies: 2
    Last Post: 03-28-2010, 09:02 AM
  3. logic Error
    By asmaa in forum C++ Programming
    Replies: 4
    Last Post: 02-13-2009, 10:58 PM
  4. Logic Error...
    By Blackroot in forum C++ Programming
    Replies: 6
    Last Post: 01-28-2006, 06:21 AM
  5. logic error
    By sscook69 in forum C++ Programming
    Replies: 5
    Last Post: 04-07-2002, 07:00 PM

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