Adblock breaks this site

Need Quick Help

Discussion in 'Programming General' started by mmki, Aug 23, 2009.

  1. mmki

    mmki Forum Addict
    Banned

    Joined:
    Jul 25, 2009
    Posts:
    251
    Referrals:
    0
    Sythe Gold:
    0
    Need Quick Help

    This is for an equation for the time it takes a pendlum to swing

    #include <iostream>
    #include <cmath>
    #define PI 3.14159
    #define G 9.8 //Gravity

    using namespace std;

    int main()
    {
    double l;
    double e;
    cout << "Enter Lengh of Rod\n";
    cin >> l;
    cout << "The time it takes from one swing is ";
    cout << 2*PI*((l/G)^(1/2));
    cin >> e;
    return 0;
    }





    i get error; invalid operands of types `double' and `int' to binary `operator^'
    :mad:
    HELP!!
    thanks
     
  2. Swan

    Swan When They Cry...
    Retired Global Moderator

    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner
    Need Quick Help

  3. super_

    super_ Member

    Joined:
    Dec 20, 2008
    Posts:
    91
    Referrals:
    0
    Sythe Gold:
    0
    Need Quick Help

    Code:
    /*
     * pendulum_time.c
     *
     * Calculates the time for a pendulum to complete one swing.
     *
     * 8/24/09 - super_
     */
    
    #include <stdio.h>
    #include <math.h>
    
    #define PI	3.14159
    #define G	9.8
    
    main() {
        double rod_length;
        double swing_time;
    
        printf("Enter length of rod: ");
        scanf("%lf", &rod_length);
    
        swing_time = 2 * PI * sqrt(rod_length / G);
    
        printf("Time it takes for one swing: %f\n", swing_time);
    
        return 0;
    }
    
    Improved.
     
  4. Swan

    Swan When They Cry...
    Retired Global Moderator

    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner
    Need Quick Help

    Improved again.
    While that may be legitimate C code for some compilers, others won't like the fact that you haven't specified a data type for the main method. As for the command line arguments, it's simply good practice to place them there.

    Also, I always thought that gravity was 9.81 rather than 9.8, but I suppose that won't matter too much.
     
  5. super_

    super_ Member

    Joined:
    Dec 20, 2008
    Posts:
    91
    Referrals:
    0
    Sythe Gold:
    0
    Need Quick Help

    It's legit for all compilers; implicit data type is int (k&r). Command line arguments aren't to be placed there; main() means 'function main with unknown arguments', not 'no arguments'. I leave it as 'unknown arguments' so that the real entry point can still push argc, argv, and envp on the stack but I don't need to access them. The stack is cleaned after main() returns anyway :)
     
  6. Swan

    Swan When They Cry...
    Retired Global Moderator

    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner
    Need Quick Help

    What I'm saying is that you aren't exactly complying to the standard most people use, which can confuse people.

    Also, many compilers will throw errors if you don't explicitly state the return type.
     
  7. hampe-92

    hampe-92 Forum Addict

    Joined:
    Jul 10, 2008
    Posts:
    328
    Referrals:
    0
    Sythe Gold:
    0
    Need Quick Help

    pretty much of topic but, what gravity is equal to differs depending on where on the earth you are (e.g. in Sweden we use g &#8776; 9.82)...
     
  8. mmki

    mmki Forum Addict
    Banned

    Joined:
    Jul 25, 2009
    Posts:
    251
    Referrals:
    0
    Sythe Gold:
    0
    Need Quick Help

    Thanks Tons Guys =)
     
  9. clint999

    clint999 Active Member

    Joined:
    Jun 4, 2008
    Posts:
    110
    Referrals:
    0
    Sythe Gold:
    0
    Need Quick Help

    It's legit for all compilers; implicit data type is int (k&r). Command line arguments aren't to be placed there; main() means 'function main with unknown arguments', not 'no arguments'. I leave it as 'unknown arguments' so that the real entry point can still push argc, argv, and envp on the stack but I don't need to access them. The stack is cleaned after main() returns anyway
     
  10. super_

    super_ Member

    Joined:
    Dec 20, 2008
    Posts:
    91
    Referrals:
    0
    Sythe Gold:
    0
    Need Quick Help

    what is your purpose?
     
< Something Wrong With My Program | Functions Help >


 
 
Adblock breaks this site