【发布时间】:2018-10-12 03:05:44
【问题描述】:
这是家庭作业的一部分,用户在其中输入五个整数,并通过几个函数得到总和、平均值、sqrt 和其他一些东西。代码如下:
#include <stdio.h>
#include <math.h>
// needs to be declared in order to work
int functionPrint(int sum, int root);
//sum function, takes all the ints from main and sums it up
int functionSum(int one, int two, int three, int four, int five) {
int sum = one + two + three + four + five;
// sends the sum to the print function
sum = functionPrint(sum, sum);
}
//sqrt function, will take all numbers and square root them
int functionSqrt(int one, int two, int three, int four, int five) {
int root = sqrt(three);
// sends the sqrt numbers to print
root = functionPrint(root, root);
}
int functionPrint(int sum, int root) {
printf("Sum: %d\n", sum);
printf("Square Root: %d\n", root);
}
//main function, all values to be worked are created here and sent to the other functions
int main() {
int sumMain, one, two, three, four, five;
printf("Enter five numbers separated by spaces: ");
scanf("%d%d%d%d%d", &one, &two, &three, &four, &five);
sumMain = functionSum(one, two, three, four, five);
}
目前,它应该只打印出 int 三的总和和 sqrt(当我解决这个问题时,我将包括其他 int)。 functionPrint() 从第 21 行开始,functionSqrt() 从第 15 行开始。但是,就像我说的,它只打印总和。我的猜测是存在一些必须被覆盖的变量,或者类似的东西。但话说回来,我不是专家。
任何帮助将不胜感激
【问题讨论】:
-
我想也许你需要了解一下 return 语句。
-
检查
scanf()调用是否返回5;如果没有,你有一个输入格式问题。你说它只打印总和;它应该打印相同的值并将其标记为“平方根”(这是用词不当,但这没关系)。你永远不会打电话给functionSqrt()AFAICS。忽略 5 个参数中的 4 个并不是一个特别好的主意。没有一个函数返回一个值,即使你说它们会返回。这很糟糕,特别是因为您尝试分配返回的值。这些函数都应该被声明和定义为返回void,并且应该删除返回值分配。 -
您还没有调用您的 functionSqrt 函数,您只是在名为 functionSum 的函数中添加值,然后将值作为按值调用传递到 print 函数中。
标签: c function pass-by-reference