【问题标题】:C - Root is not printing the right value, is not changing from sumC - Root 没有打印正确的值,没有从 sum 改变
【发布时间】: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


【解决方案1】:

添加#include &lt;stdlib.h&gt; 并将scanf 更改为:

int result = scanf("%d%d%d%d%d", &one, &two, &three, &four, &five);
if (result != 5)
{
    fprintf(stderr, "Error, failed to read five numbers.\n");
    exit(EXIT_FAILURE);
}

这将测试输入问题。尽早测试并报告问题总是好的,而不是让程序在以后以奇怪的方式继续失败。

functionSum 更改为返回一个值。 functionSumint functionSum(…) 声明,这意味着它应该返回一个值。要真正返回一个值,您需要在其中添加 return 语句:

int functionSum(int one, int two, int three, int four, int five)
{
    return one + two + three + four + five;
}

main程序中,在functionSum之后调用functionPrint

sumMain = functionSum(one, two, three, four, five);
functionPrint(sumMain, 0);

这应该负责计算总和并打印它。了解这些变化应该有助于您在平方根方面取得进步。

【讨论】:

    【解决方案2】:
    1. 在 main() 中

      scanf("%d%d%d%d%d", &one, &two, &three, &four, &five);
      

    scanf() 将无法按您的意愿工作,形成字符串(“%d%d%d%d%d”)中没有特定的分隔符,您将如何划分 5 个输入?

    1. 在 main() 中

      sumMain = functionSum(one, two, three, four, five);
      

    sumMain不会得到你想要的sum值,因为functionSum()不会“返回”任何值,请自行google“C语言”+“返回”。

    1. 根据您在步骤 2 中的搜索结果修改了 functionSum() 和 functionSqrt()。您可能不需要 functionPrint()。

    【讨论】:

    • 您关于scanf 的说法是错误的。它将完全按预期工作,因为每个 %d 都会跳过输入中的空格。
    【解决方案3】:

    根据所需输出对您的程序进行修改:

    #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;
        float sqrt = functionSqrt(one,two,three,four,five);  // Called the Sqrt function and receives the square root of the third number
        // sends the sum to the print function
        sum = functionPrint(sum, sqrt);
    }
    
    //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);
        return root;
        // 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);
    }
    

    【讨论】:

    • functionSum 仍然没有返回任何内容或使其返回 void 并删除分配。
    猜你喜欢
    • 2015-08-07
    • 1970-01-01
    • 1970-01-01
    • 2020-02-01
    • 2019-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-04
    相关资源
    最近更新 更多