Need Quick Help

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

Need Quick Help
  1. Unread #1 - Aug 23, 2009 at 8:16 AM
  2. mmki
    Joined:
    Jul 25, 2009
    Posts:
    251
    Referrals:
    0
    Sythe Gold:
    0

    mmki Forum Addict
    Banned

    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
     
  3. Unread #2 - Aug 23, 2009 at 5:48 PM
  4. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    Need Quick Help

  5. Unread #3 - Aug 24, 2009 at 3:04 PM
  6. super_
    Joined:
    Dec 20, 2008
    Posts:
    91
    Referrals:
    0
    Sythe Gold:
    0

    super_ Member

    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.
     
  7. Unread #4 - Aug 24, 2009 at 5:22 PM
  8. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    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.
     
  9. Unread #5 - Aug 24, 2009 at 6:45 PM
  10. super_
    Joined:
    Dec 20, 2008
    Posts:
    91
    Referrals:
    0
    Sythe Gold:
    0

    super_ Member

    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 :)
     
  11. Unread #6 - Aug 25, 2009 at 2:19 AM
  12. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    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.
     
  13. Unread #7 - Aug 25, 2009 at 12:02 PM
  14. hampe-92
    Joined:
    Jul 10, 2008
    Posts:
    328
    Referrals:
    0
    Sythe Gold:
    0

    hampe-92 Forum Addict

    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)...
     
  15. Unread #8 - Sep 6, 2009 at 3:18 AM
  16. mmki
    Joined:
    Jul 25, 2009
    Posts:
    251
    Referrals:
    0
    Sythe Gold:
    0

    mmki Forum Addict
    Banned

    Need Quick Help

    Thanks Tons Guys =)
     
  17. Unread #9 - Oct 21, 2009 at 6:10 AM
  18. clint999
    Joined:
    Jun 4, 2008
    Posts:
    110
    Referrals:
    0
    Sythe Gold:
    0

    clint999 Active Member

    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
     
  19. Unread #10 - Oct 21, 2009 at 7:57 PM
  20. super_
    Joined:
    Dec 20, 2008
    Posts:
    91
    Referrals:
    0
    Sythe Gold:
    0

    super_ Member

    Need Quick Help

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

Users viewing this thread
1 guest


 
 
Adblock breaks this site