Closed. This question needs to be more
focused。它当前不接受答案。
想改善这个问题吗?更新问题,使其仅关注
editing this post的一个问题。
3个月前关闭。
Improve this question
当您从键盘输入三个数字时,将计算并显示它们的平均值,并显示输入值本身以及从输入值中减去平均值的结果(带+号,最多三位小数)。程序来做。
规范:
在键盘上输入并显示结果。
主面。输入的值是浮点数。 在函数ave3()中计算平均值。这个
函数从主要方面接受三个数据作为参数,
计算它们的平均值,并将结果返回
值。 从输入数据中减去平均值的操作
在功能sub3()中执行。该功能接收
有关来自主要方面和平均值的三个数据的信息
先前计算为参数的值,然后减去平均值
从这三个数据中的每一个。此函数没有返回值,并且
主端输出在函数中更改的三个值
他们是。 源代码
#include <stdio.h>
double ave3 (double, double, double);
double subave3 (double *, double *, double *);
int main ()
{
double a, b, c, ave;
printf ("Please enter three values: \ n");
scanf ("% lf% lf% lf", & a, & b, & c);
ave = ave3 (& a, & b, & c); / * Function call * /
printf ("Average:% .3f \ n", ave);
printf ("Original data:% + .3f% + .3f% + .3f \ n", a, b, c);
subave3 (double * x, double * y, double * z);
printf ("Data after average deduction:% + .3f% + .3f% + .3f \ n", a, b, c);
return 0;
}
/ * A function that calculates the average. It also subtracts the average value from the data. * /
double subave3 (double x, double y, double z)
{
x-= ave; / * Subtract the average from each data * /
y-= ave;
z-= ave;
a = x;
b = y;
c = z;
return a, b, c;
}
double ave3 (double x, double y, double z) {
ave = (* x + * y + * z) / 3.0;
return ave;
}
错误
prog01.c: In function ‘main’:
prog01.c: 13: 14: error: incompatible type for argument 1 of ‘ave3’
13 | ave = ave3 (& a, & b, & c); / * Function call * /
| ^ ~
| |
| double *
prog01.c: 3:13: note: expected ‘double’ but argument is of type ‘double *’
3 | double ave3 (double, double, double);
| ^ ~~~~~
prog01.c: 13: 18: error: incompatible type for argument 2 of ‘ave3’
13 | ave = ave3 (& a, & b, & c); / * Function call * /
| ^ ~
| |
| double *
prog01.c: 3:21: note: expected ‘double’ but argument is of type ‘double *’
3 | double ave3 (double, double, double);
| ^ ~~~~~
prog01.c: 13: 22: error: incompatible type for argument 3 of ‘ave3’
13 | ave = ave3 (& a, & b, & c); / * Function call * /
| ^ ~
| |
| double *
prog01.c: 3:29: note: expected ‘double’ but argument is of type ‘double *’
3 | double ave3 (double, double, double);
| ^ ~~~~~
prog01.c: 18:11: error: expected expression before ‘double’
18 | subave3 (double * x, double * y, double * z);
| ^ ~~~~~
prog01.c: 18: 3: error: too few arguments to function ‘subave3’
18 | subave3 (double * x, double * y, double * z);
| ^ ~~~~~~
prog01.c: 4: 8: note: declared here
4 | double subave3 (double *, double *, double *);
| ^ ~~~~~~
prog01.c: At top level:
prog01.c: 26: 8: error: conflicting types for ‘subave3’
26 | double subave3 (double x, double y, double z)
| ^ ~~~~~~
prog01.c: 4: 8: note: previous declaration of ‘subave3’ was here
4 | double subave3 (double *, double *, double *);
| ^ ~~~~~~
prog01.c: In function ‘subave3’:
prog01.c: 28: 8: error: ‘ave’ undeclared (first use in this function); did you mean ‘ave3’?
28 | x-= ave; / * Subtract the average from each data * /
| ^ ~~
| ave3
prog01.c: 28: 8: note: each undeclared identifier is reported only once for each function it appears in
prog01.c: 32: 3: error: ‘a’ undeclared (first use in this function)
32 | a = x;
| ^
prog01.c: 33: 3: error: ‘b’ undeclared (first use in this function)
33 | b = y;
| ^
prog01.c: 34: 3: error: ‘c’ undeclared (first use in this function)
34 | c = z;
| ^
prog01.c: In function ‘ave3’:
prog01.c: 40: 3: error: ‘ave’ undeclared (first use in this function); did you mean ‘ave3’?
40 | ave = (* x * y * z) / 3.0;
| ^ ~~
| ave3
prog01.c: 40:10: error: invalid type argument of unary ‘*’ (have ‘double’)
40 | ave = (* x * y * z) / 3.0;
| ^ ~
prog01.c: 40: 15: error: invalid type argument of unary ‘*’ (have ‘double’)
40 | ave = (* x * y * z) / 3.0;
| ^ ~
prog01.c: 40: 20: error: invalid type argument of unary ‘*’ (have ‘double’)
40 | ave = (* x * y * z) / 3.0;
| ^ ~
函数ave
需要三个double
参数,例如a
,b
或c
。但是,通过获取他们的地址,以下传递了double *
参数:
ave = ave3 (& a, & b, & c);
只需将其更改为:
ave = ave3 (a, b, c);
逐一查看参数,并将您要传递的内容与函数期望的类型进行比较,并确保它们兼容。
另外,以下操作将无效:
subave3 (double * x, double * y, double * z);
这是一个函数调用,而不是声明。将其更改为:
subave3 (a, b, c);
这可能不是您想要的,但是会编译。
在
subave3
中,即使未在函数中声明
a
,
b
和
c
,它也尝试引用。如果要返回这些值,则需要传递它们的地址并通过指针存储这些值,因为在
return
语句中不能返回多个值。
在
ave3
中,由于
a
,
b
和
c
不是指针,因此以下操作无效:
ave = (* x + * y + * z) / 3.0;
另外,未声明
ave
。更改为:
double ave = (x + y + z) / 3.0;
可能存在其他问题,并且在任何情况下都可能需要重新考虑。只需记住,将
&
应用于类型
t
的变量将得到类型为
t *
的指针,而将
*
应用于类型
t *
的表达式将得到类型为
t
的值。