如何计算和显示C++中5个数字的平均值?
How to calculate and display the average of the 5 numbers in C++?
我正在尝试计算和显示 c++ 中 5 个数字的平均值,我正在挑战我自己不要使用:全局变量、标签或 go-to 语句、无限循环和中断语句以退出循环。我被卡住了,任何人都可以帮助我解决这个问题:我需要提示用户结束 5 个数字,然后计算并显示 5 个数字的平均值。谢谢。
这是我尝试过的代码:
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
// create a variable x.
int x;
// create a variable called average to get the 5 numbers
// calculation.
int average;
// Prompt the user to enter five numbers.
cout << "Please enter five numbers." << endl;
cout << average << endl;
// Calculate the five numbers.
average = x;
cout << "The average for the five numbers are:" << average << endl;
return 0;
}
使用数组的解决方案。
#include <stdio.h>
#include<iostream>
using namespace std;
int main()
{
int n, i;
vector<double> nums;
double sum = 0.0, average;
cout<<"Enter the numbers of elements: ";
cin>>n;
for(i = 0; i < n; ++i)
{
cout<<"Enter number: "<<i+1;
cin>>num[i];
sum += num[i];
}
average = sum / n;
cout<<"Average = "<<average
return 0;
}
您可以使用以下代码,我使用的变量与您使用的变量非常相似(这只是一个代码片段(:
int x;
int sum=0;
cout << "Please enter five numbers." << endl;
for(int i=0; i<5; i++){
cin>>x;
sum += x;
}
cout << sum/5.0 << endl;
编辑:
如果您根本不想使用循环,那么您可以使用称为递归的技术执行以下操作:
// Example program
#include <iostream>
#include <string>
int calc(int sum, int i){
if(i == 0){
return sum;
}else{
int x;
std::cin>>x;
i--;
sum+=x + calc(sum, i);
}
return sum;
}
int main()
{
int i=5;
int sum=0;
sum = calc(sum, i);
std::cout<<std::endl<<sum/5.0;
return 0;
}
这是我使用的编辑器的链接。
我希望这是有帮助的。
我已经解决了我自己的问题。正如我所说,我正在避免global variables, labels or go-to statements, infinite loops, and break statements to exit loops
.所以,这是我的答案:
这是我的代码:
#include <iostream>
using namespace std;
int main() {
// Creating variables.
float v, w, x, y, z, average;
// Prompting the user to enter first number.
cout << "Please enter first number: ";
// Getting the input from the user.
cin >> v;
// Prompting the user to enter second number.
cout << "Please enter second number ";
// Getting the input from the users.
cin >> w;
// Prompting the user to enter third number.
cout << "Please enter third number ";
// Getting the input from the user.
cin >> x;
// Prompting the user to enter fourth number.
cout << "Please enter fourth number ";
// Getting the input from the user.
cin >> y;
// Prompting the user to enter fifth number.
cout << "Please enter fifth number ";
// Getting the input from the user.
cin >> z;
// Calculating the average of those user input five numbers.
average = (v + w + x + y + z) / 5;
// Displaying the total average of the five numbers entered.
cout << "The average of the five numbers is:" << average << endl;
return 0;
}
下面是一些应该可以工作的代码:
#include <iostream>
#include <stdio.h>
using namespace std;
int main(int argc, char** argv) {
int main(int argc, char** argv) {
float x = 0;
int i;
int a;
int size;
cout << "How many numbers? ";
cin>> size;
for (i = 1; i <= size; i++) {
cout << "Please enter number #" << i << ": ";
cin >>a;
x += a;
}
cout << "The average for the five numbers are: " << x/(float)size << endl;
return 0;
}
当程序询问多少个数字时,您只需输入 5(它只是让您找到一个值的平均值(。希望这有帮助!
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 求出有多少个数字是完美平方,而sqrt()是L,R范围内的素数
- C++通过数字比较两个数字
- 将向量之间的数字放在另一个向量之间<vector>>如果两个数字的差值为 1
- C++ 函数,用于查找传入的 N 个数字的平均值、总和、最小值和最大值
- 如果我不知道每个列表中有多少个数字,我如何将给定数量的数字列表作为输入?
- 获取 2 个数字之间的差异百分比
- 在 txt 文件中显示前两个数字的程序
- 形成一个斐波那契三角形,使得每个数字是上面左对角线或右对角线上两个数字的总和
- 在C++中编程,将 3 个数字发送到一个函数,然后计算这 3 个数字的平均函数
- 两个数字的对称配对功能
- 将最多 50 位数字的 2 个数字相乘
- 查找中间两个数字的正则表达式的匹配项
- 使用C++具有两个数字的最短路径算法.(C++)
- 使用三个数字比较器进行排序
- 在 CPP 中交换 2 个数字
- 将两个数字添加为链表
- C++ 分段错误:11 错误,同时编码将两个数字相加的链接列表
- 在 C++ 中查找 2 个数字的 GCD
- 代码以查找数组中的最大数字,但它仅将第一个数字显示为最大数字. 有人请告诉我为什么