计算用户输入的 N 个数字的平均值

C++C++Beginner
立即练习

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

介绍

在本实验中,我们将学习如何在 C++ 中计算用户输入的 n 个数字的平均值,而不使用数组。我们将逐行分析代码,并理解其工作原理。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL cpp(("C++")) -.-> cpp/IOandFileHandlingGroup(["I/O and File Handling"]) cpp(("C++")) -.-> cpp/StandardLibraryGroup(["Standard Library"]) cpp(("C++")) -.-> cpp/SyntaxandStyleGroup(["Syntax and Style"]) cpp(("C++")) -.-> cpp/BasicsGroup(["Basics"]) cpp(("C++")) -.-> cpp/ControlFlowGroup(["Control Flow"]) cpp/BasicsGroup -.-> cpp/variables("Variables") cpp/BasicsGroup -.-> cpp/operators("Operators") cpp/ControlFlowGroup -.-> cpp/for_loop("For Loop") cpp/IOandFileHandlingGroup -.-> cpp/output("Output") cpp/IOandFileHandlingGroup -.-> cpp/user_input("User Input") cpp/StandardLibraryGroup -.-> cpp/math("Math") cpp/SyntaxandStyleGroup -.-> cpp/code_formatting("Code Formatting") subgraph Lab Skills cpp/variables -.-> lab-96156{{"计算用户输入的 N 个数字的平均值"}} cpp/operators -.-> lab-96156{{"计算用户输入的 N 个数字的平均值"}} cpp/for_loop -.-> lab-96156{{"计算用户输入的 N 个数字的平均值"}} cpp/output -.-> lab-96156{{"计算用户输入的 N 个数字的平均值"}} cpp/user_input -.-> lab-96156{{"计算用户输入的 N 个数字的平均值"}} cpp/math -.-> lab-96156{{"计算用户输入的 N 个数字的平均值"}} cpp/code_formatting -.-> lab-96156{{"计算用户输入的 N 个数字的平均值"}} end

包含库和使用命名空间

我们将在 ~/project 目录下使用以下命令创建一个名为 main.cpp 的新文件:

touch ~/project/main.cpp

在第一步中,我们将包含必要的库并使用标准命名空间。

#include <iostream>

using namespace std;

欢迎信息和变量声明

在下一步中,我们将向用户打印欢迎信息并声明程序中使用的变量。

int main()
{
  cout << "\n\nWelcome to the Average Calculator!\n\n";

  int n, i, temp;
  double sum = 0, average = 0;
}

我们声明了以下变量:

  • n 是用户输入的整数数量。
  • i 是循环变量。
  • temp 是一个临时变量,用于读取用户输入。
  • sum 保存用户输入的所有值的总和。
  • average 计算为总和除以 n

读取用户输入

在下一步中,我们将读取用户的输入。我们会提示用户输入他们想要计算平均值的整数数量。

cout << "Enter the number of integers: ";
cin >> n;

然后,我们会要求用户逐个输入每个数字。我们将使用一个 for 循环来获取用户输入,并将输入的数字累加到 sum 中。

for (i = 1; i <= n; i++)
{
  cout << "Enter number " << i << ": ";
  cin >> temp;

  sum += temp;
}

计算平均值

现在,我们可以使用总和和整数数量来计算输入数字的平均值。

average = sum / n;

向用户输出平均值

最后一步是将最终结果输出给用户。

cout << "\n\nThe Sum of the " << n << " numbers entered by the user is: " << sum << endl;
cout << "\nThe Average of the " << n << " numbers entered by the user is : " << average << "\n\n";

完整代码:

#include <iostream>

using namespace std;

int main()
{
    cout << "\n\nWelcome to the Average Calculator!\n\n";

    int n, i, temp;
    double sum = 0, average = 0;

    cout << "Enter the number of integers: ";
    cin >> n;

    for (i = 1; i <= n; i++)
    {
        cout << "Enter number " << i << ": ";
        cin >> temp;

        sum += temp;
    }

    average = sum / n;

    cout << "\n\nThe Sum of the " << n << " numbers entered by the user is: " << sum << endl;
    cout << "\nThe Average of the " << n << " numbers entered by the user is : " << average << "\n\n";

    return 0;
}

要在终端上编译并运行代码,请执行以下命令:

g++ main.cpp -o main
./main

总结

在本实验中,我们学习了如何在不使用数组的情况下计算用户输入的 n 个数字的平均值。我们使用 for 循环读取用户输入,并计算输入数字的总和与平均值。最后,我们将结果输出给用户。