找出用户在c++中输入的3个最大数字的平均值

finding the average of the largest numbers input out of 3 by the user in c++

本文关键字:3个 数字 平均值 输入 用户 c++      更新时间:2023-10-16

所以设计问题是:

输入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。