【发布时间】:2017-07-16 21:18:21
【问题描述】:
#include <stdio.h>
#include <math.h>
float math(int, int, int, int, int, float, float, float);
main() {
int a, b, c, d, e;
float sum, avg, sd;
printf("Enter Five Integers->");
scanf("%d%d%d%d%d", &a, &b, &c, &d, &e);
math(a, b, c, d, e, sum, avg, sd);
printf("Sum=%.2f\nAverage=%.2f\nStandard Deviation=%.2f", sum, avg, sd);
}
float math(int a, int b, int c, int d, int e, float sum, float avg, float sd) {
sum = a + b + c + d + e;
avg = (sum) / 5;
sd = pow(
((pow(a - avg, 2) + pow(b - avg, 2) + pow(c - avg, 2) + pow(d - avg, 2),
pow(e - avg, 2)) /
5),
0.5);
return sum, avg, sd;
}
我的程序总是返回答案 0.00。谁能解释我的代码有什么问题?
【问题讨论】:
-
请正确缩进您的代码。阅读逗号运算符的作用,您没有分配返回值;变量是按值传递的,因此在函数中更改它们根本没有帮助。
-
单步调试代码时调试器会告诉你什么?
-
您的
math函数不会返回您认为的结果(阅读the comma operator)。您可能需要find a good beginners book 并阅读更多关于函数以及参数和返回值如何工作的信息。 -
还有什么是代码应该“返回”反正。
-
感谢您的帮助。我的代码应该“返回”输入的五个整数的总和、平均值和标准差。
标签: c return function-call comma-operator