如何使用不同的功能计算三个数字的总和,平均值和产物



我需要创建一个接受3个数字的程序并找到总和,平均值和产品。我只需要使用 main() get_abc() Compute() and and display() functions。我做对了,但我没有得到有关数学操作的正确输出。

#include<conio.h>
#include<iostream.h>
float get_A(float A)
{
    cout<<"Enter First Number: ";
    cin>>A;
    return(A);
}
float get_B(float B)
{
    cout<<"Enter Second Number: ";
    cin>>B;
    return(B);
}
float get_C(float C)
{
    cout<<"Enter Third Number: ";
    cin>>C;
    return(C);
}
 float compute_sum(float A,float B,float C)
{
 float sum;
 sum = A + B + C;
 return(sum);
}
float compute_ave(float A,float B,float C)
{
    float ave;
    ave = (A + B + C) / 3;
    return (ave);
}
float compute_prod(float A,float B,float C)
{
    float prod;
    prod = A * B * C;
    return(prod);
}
void display(float sum,float ave,float prod)
{
    cout<<"The sum of three numbers is "<<sum<<".n";
    cout<<"The average of three numbers is "<<ave<<".n";
    cout<<"The product of three numbers is "<<prod<<".";
}
float main()
{
    float A,B,C;
    float sum;
    float ave;
    float pro;
    clrscr();
    get_A(A);
    get_B(B);
    get_C(C);
    sum = compute_sum(A,B,C);
    ave = compute_ave(A,B,C);
    pro = compute_prod(A,B,C);
    display(sum,ave,pro);
    getch();
    return(0);
}

这是输出。

Enter First Number: 1
Enter Second Number: 2
Enter Third Number: 3
The sum of three numbers is 0.
The average of three numbers is 0.
The product of three numbers is 0.

我真的需要帮助。我的教授给我这个问题而不教导如何编码,所以我只提出基础知识,我真的放弃了,最终在这里。如果您愿意,您可以更改,添加或替换代码(用基本代码),我将不胜感激。

更改以下内容:

get_A(A);
get_B(B);
get_C(C);

A = get_A(A);
B = get_B(B);
C = get_C(C);

以便您使用函数的返回值

此外,main()应返回int,而不是float

此外,在声明变量时初始化您的变量,以免在此功能中使用"未经初始化"警告。

相关内容

最新更新