如何计算和显示C++中5个数字的平均值?

How to calculate and display the average of the 5 numbers in C++?

本文关键字:5个 数字 平均值 C++ 显示 何计算 计算      更新时间:2023-10-16

我正在尝试计算和显示 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(它只是让您找到一个值的平均值(。希望这有帮助!