如何使用不同的功能计算三个数字的总和,平均值和产物
How to compute sum, average and product of three numbers using different functions
我需要创建一个接受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
。
此外,在声明变量时初始化您的变量,以免在此功能中使用"未经初始化"警告。
相关文章:
- 模板-模板参数推导:三个不同的编译器三种不同的行为
- 在 2D 向量中使用第三个 [ ] 有什么意义?
- 如何通过按下第三个窗口中的按钮,将QString从一个窗口获取到另一个窗口
- 如何知道n!是否可以表示为三个连续数字的乘法?
- 使用三个数字比较器进行排序
- 有没有更简单的方法可以从用户那里获取三个数字并按升序打印它们?
- 数组中最大的三个数字
- 跳过每三个数字
- 三个数字之间的 C++ 相加两个更高的数字,没有循环和数组
- 如何使用不同的功能计算三个数字的总和,平均值和产物
- 创建一个程序,要求用户输入两个数字,并有三个可能的答案.代码未编译.数字(作为int)
- 为什么linear_congruenty_engine :: seed(sseq)丢弃了由种子序列生成的三个数字
- 根据列中的第三个数字对向量进行排序
- 三个数字彩票猜谜游戏C++
- 从一组数字中找出最接近的三个数字值
- 需要帮助获取数组中最小的三个数字
- 输入三个数字,并在c++中确定最高和最低的数字
- 三个不同的数字
- 为什么我在尝试输出UTF-8字符时会得到三个不同的数字
- 至少三个数字