群众之间的重力吸引力

时间:2013-10-21 22:25:04

标签: c function pointers double

为什么我的程序会继续计算相同的力?我正确地使用了公式,但是我不确定为什么我会继续使用127达因的力。任何帮助将不胜感激

#include <stdio.h>
#include <math.h>
const double gravity_constant = 6.673;
void force_calculate(double a, double b, double c, double d);
void input(double *a, double *b, double *c);
void display(double a, double b, double c, double d);
int main(int argc, char * argv[])
{
    double mass_1 = 0;
    double mass_2 = 0;
    double distance = 0;
    double force = 0;

    input(&mass_1, &mass_2, &distance);
    force = force_calculate(mass_1, mass_2, distance, force);
    display(mass_1, mass_2, distance, force);

    return 0;
}

void force_calculate(double a, double b, double c, double d)
{
    d = (gravity_constant*a*b)/(c*c);
    return;
}
void input(double *a, double *b, double *c)
{
    printf("What is the first mass in grams?\n");
    scanf("%lf", a);
    printf("What is the second mass in grams?\n");
    scanf("%lf", b);
    printf("What is the distance between the two masses in centimeters\n");
    scanf("%lf", c);
}
void display(double a, double b, double c, double d)
{
    printf("%fg is the first mass\n", a);
    printf("%fg is the second mass\n", b);
    printf("%fcm is the distance between the two masses\n", c);
    printf("%f is the force in dynes between both masses\n", d);
    return;
}

5 个答案:

答案 0 :(得分:1)

第一种方法

通过引用传递force。将force的定义更改为

void force_calculate(double a, double b, double c, double *d)
{
    *d = (gravity_constant*a*b)/(c*c);
}

并像这样称呼它

force_calculate(mass_1, mass_2, distance, &force);

第二种方法:

您只需返回force_calculate

中的值即可
double force_calculate(double a, double b, double c, double d)
{
    d = (gravity_constant*a*b)/(c*c);
    return d;
}

答案 1 :(得分:1)

您应该返回该值以便可以分配它(确保它也是正确的类型)。此外,您可以在此处移除使用武力,因为它是计算的结果。

force = force_calculate(mass_1, mass_2, distance);
...
double force_calculate(double a, double b, double c)
{
 return (gravity_constant*a*b)/(c*c);
}

答案 2 :(得分:1)

问题是,force_calculate()功能的返回类型为void,您将其分配到force中的main()了 你的函数定义应该是

void force_calculate(double a, double b, double c, double *d)
{
    *d = (gravity_constant*a*b)/(c*c);
}

不要忘记更改其功能原型。函数调用应该是

force_calculate(mass_1, mass_2, distance, &force);  

答案 3 :(得分:1)

运行此程序会给我以下运行时错误。

d.c:15:11:错误:无法忽略空值,因为它应该是

我认为,这可以解决它。

#include <stdio.h>
#include <math.h>
const double gravity_constant = 6.673;
double force_calculate(double a, double b, double c);
void input(double *a, double *b, double *c);
void display(double a, double b, double c, double d);
int main(int argc, char * argv[])
{
    double mass_1 = 0;
    double mass_2 = 0;
    double distance = 0;
    double force = 0;

    input(&mass_1, &mass_2, &distance);
    force = force_calculate(mass_1, mass_2, distance);
    display(mass_1, mass_2, distance, force);

    return 0;
}

double force_calculate(double a, double b, double c)
{
    double force;
    force = (gravity_constant*a*b)/(c*c);
    return force;
}
void input(double *a, double *b, double *c)
{
    printf("What is the first mass in grams?\n");
    scanf("%lf", a);
    printf("What is the second mass in grams?\n");
    scanf("%lf", b);
    printf("What is the distance between the two masses in centimeters\n");
    scanf("%lf", c);
}
void display(double a, double b, double c, double d)
{
    printf("%fg is the first mass\n", a);
    printf("%fg is the second mass\n", b);
    printf("%fcm is the distance between the two masses\n", c);
    printf("%f is the force in dynes between both masses\n", d);
    return;
} 

答案 4 :(得分:0)

您定义force_calculate不返回任何内容(void),但在main方法中,您可以这样调用它:

force = force_calculate(mass_1, mass_2, distance, force);

即你正在返回双倍。

将force_calculate更改为此声明:

double force_calculate(double a, double b, double c);

和force_calculate到这个定义:

double force_calculate(double a, double b, double c)
{
    return (gravity_constant*a*b)/(c*c);
}