• Log in or sign up for an account here: https://www.sythe.org/login

Need Quick Help

mmki

Forum Addict

Banned
Joined
Jul 25, 2009
Sythe Gold
0
Messages
251
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
 
Member of the Month Winner
Sythe's 10th Anniversary
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.
 
Member of the Month Winner
Sythe's 10th Anniversary
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.81

[b]int[/b] main([b]int argc, char* argv[][/b]) {
    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.

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.
 
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.

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 :)
 
Member of the Month Winner
Sythe's 10th Anniversary
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 :)
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.
 
Also, I always thought that gravity was 9.81 rather than 9.8, but I suppose that won't matter too much.
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)...
 
Thanks Tons Guys =)
 
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
 
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

what is your purpose?
 
Runescape Gamblingrunescape gambling
Back
Top