返回一个值作为输出参数

时间:2017-06-01 10:32:56

标签: c parameters output

我在C中有一个问题,我需要将二次方程的系数插入到函数中并返回解的数量和结果。

  

编写一个程序,接受一系列3个实数,即   二次方程的系数,程序将打印出来   等式和解决方案本身的一些解决方案。   指南

     
      
  • 必须使用其中一个函数处理函数   返回解决方案的数量作为返回值,并返回   解决方案本身通过输出参数。
  •   
  • 必须是3个数字   每次收到。输入将来自文件(将以EOF结尾)
  •   

与此同时我构建了函数而没有从文件中读取只是为了看到它对我有用,我构建了返回解决方案数量的函数,但我纠结于如何将结果作为输出参数返回 这是我现在的代码:

int main ()
{

    double a, b, c, root1,root2,rootnum;

    printf("Enter coefficients a, b and c: ");

    scanf("%lf %lf %lf",&a, &b, &c);

    rootnum=(rootnumber(a,b,c);

    printf("the number of roots for this equation is %d ",rootnum);
}


int rootnumber (double a,double b, double c)
{

    formula=b*b - 4*a*c;

    if (formula<0)

        return 0;

    if (formula==0)

        return 1;
    else
        return 2;
}

4 个答案:

答案 0 :(得分:1)

在C中,提供“输出参数”通常相当于提供一个指针参数。该函数取消引用该指针并写入结果。例如;

 int some_func(double x, double *y)
 {
     *y = 2*x;
     return 1;
 }

调用者通常必须提供将接收结果的地址(例如变量)。例如;

 int main()
 {
     double result;
     if (some_func(2.0, &result) == 1)
        printf("%lf\n", result);
     else
        printf("Uh oh!\n");
     return 0;
 }

我故意提供了一个示例,说明了“输出参数”是什么,但与您实际需要编写的代码没有关系。对于您的问题,您需要提供两个(即总共五个参数,您已经提供的三个参数,以及另外两个用于将值返回给调用者的指针)。

由于这是一项家庭作业,我不会解释你的功能需要通过输出参数返回的值。毕竟,这是练习的一部分,目的是让你通过解决这个问题来学习。

答案 1 :(得分:0)

除了调用中的任意括号和其他一些语法错误之外,到目前为止你所看到的都很好。要打印出根数,您需要在 // Assign attribute specific "data-scroll-speed" to elements upon loading, resizing and scrolling of the webpage page. "if/else" is depending on if #image-ul is fully above the bottom edge of the browser window. $(document).ready(function() { $(window).on('load resize scroll', function() { var WindowScrollTop = $(this).scrollTop(), Div_one_top = $('#image-ul').offset().top, Div_one_height = $('#image-ul').outerHeight(true), Window_height = $(this).outerHeight(true); if (WindowScrollTop + Window_height >= (Div_one_top + Div_one_height)) { $('#sloganenglish').attr('data-scroll-speed', '4'); $('.slogan-a-line').attr('data-scroll-speed', '5'); $('.slogan-a-line').css('color', 'yellow'); } else { $('#sloganenglish').attr('data-scroll-speed', '1'); $('.slogan-a-line').attr('data-scroll-speed', '1'); $('.slogan-a-line').css('color', 'red'); } }).scroll(); }); 语句中加上format specifier和一个参数:

printf

printf("the number of roots for this equation is %d\n", rootNum); 是int的格式说明符。

答案 2 :(得分:0)

这是您的工作代码:

#include <stdio.h>

int rootnumber (double a,double b, double c)
{
    double formula = (b*b) - (4*(a)*(c));
    if (formula > 0) {
        return 2;
    }
    else if (formula < 0) {
        return 0;
    }
    else {
        return 1;
    }
}

int main (void)
{
    double a, b, c;
    printf("Enter coefficients a, b and c: ");
    scanf("%lf %lf %lf",&a, &b, &c);
    printf("The number of roots for this equation is %d ", rootnumber(a,b,c));
    return 0;
}

答案 3 :(得分:0)

它需要一些健全性检查,现在正在进行:

#include<stdio.h>
int rootnumber(double a, double b, double c);
int main ()
{

    double a, b, c, root1,root2;
    int rootnum;

    printf("Enter coefficients a, b and c: ");

    scanf("%lf %lf %lf",&a, &b, &c);

    rootnum=rootnumber(a,b,c);

    printf("the number of roots for this equation is %d", rootnum);

    return 0;
}


int rootnumber(double a, double b, double c)
{

    int formula= (b*b) - (4*a*c);

    if (formula<0)

        return 0;

    if (formula==0)

        return 1;
    else
        return 2;
}
相关问题