找出用户在c++中输入的3个最大数字的平均值
finding the average of the largest numbers input out of 3 by the user in c++
所以设计问题是:
输入3个数字并显示最大值。继续输入一组3个数字,直到用户想要退出为止。求所有最大数字的平均值。
因此,输入数字很好,但当我试图插入SENTINEL
值以停止循环时,我需要为所有3个数字输入它,但它不能为我提供过去输入的数字的正确平均值。
非常感谢您的帮助,谢谢您抽出时间!
#include <iostream>
using namespace std;
int main()
{
const int SENTINEL = -1;
int num1;
int num2 = 0;
int num3 = 0;
int largest;
int sum;
int count;
float average;
// initialize the count and sum
count = 0;
sum = 0;
while (num1 != SENTINEL)
{
// Prompt user for the first number or to quit
cout << "If you want to quit enter " << SENTINEL << " to stopn " << endl;
cout << "Enter first number ";
cin >> num1;
// Prompt the user for the second number
cout << "please enter second number ";
cin >> num2;
// Prompt the user for a third number
cout << "please enter the third number ";
cin >> num3;
// Compare numbers 1 and 2 for the largest number
if (num1 > num2)
{
largest = num1;
}
else
{
largest = num2;
}
// Compare largest to last number input
if (num3 > largest)
{
largest = num3;
}
// Display the largest number
cout << "largest number is: " << largest << endl;
// Increment the count
count++;
}
if (count > 0)
{
average = sum / count;
cout << "Average of the numbers is " << average;
}
return 0;
}
使用break;
指令是可能的,但通常被认为是不合法的,很难维护。
原因是你破坏了代码流,程序的读者(可能不是你在大程序中,也可能不是你共享的)希望循环的代码一直延续到它的末尾。如果你的函数有一个或多个break;
指令,可能很难理解函数的意图,因为你必须记住在哪些情况下循环已经结束。
解决问题的基本原则是在获得num1的值之后,在正确的位置开始循环。然后你的循环也必须以它结束。这样,你就会在用户输入num1后立即检查你的退出条件。
// initialize the count and sum
count = 0;
sum = 0;
// Prompt user for the first number or to quit
cout << "If you want to quit enter " << SENTINEL << " to stopn " << endl;
cout << "Enter first number ";
cin >> num1;
while (num1 != SENTINEL)
{
// Prompt the user for the second number
cout << "please enter second number ";
cin >> num2;
// Prompt the user for a third number
cout << "please enter the third number ";
cin >> num3;
// Compare numbers 1 and 2 for the largest number
if (num1 > num2)
{
largest = num1;
}
else
{
largest = num2;
}
// Compare largest to last number input
if (num3 > largest)
{
largest = num3;
}
// Display the largest number
cout << "largest number is: " << largest << endl;
// Increment the count
count++;
// Prompt user for the first number or to quit
cout << "If you want to quit enter " << SENTINEL << " to stopn " << endl;
cout << "Enter first number ";
cin >> num1;
}
if (count > 0)
{
average = sum / count;
cout << "Average of the numbers is " << average;
}
return 0;
注意:正如注释中提到的,我没有解决这里的代码错误(变量没有声明,总和没有更新,等等)。
在输入num1后立即有条件地中断循环。
cin >> num1;
if (SENTINEL == num1)
{
break;
}
之后
cin >> num1;
你可以检查,如果它的SENTIEL号码和打破循环:
if (num1 == SENTIEL)
break
这样它就不会执行另一个cin。
相关文章:
- 比较并显示使用最小值(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
- 代码以查找数组中的最大数字,但它仅将第一个数字显示为最大数字. 有人请告诉我为什么