ユーザー入力の N 個の数の平均を求める

C++C++Beginner
今すぐ練習

💡 このチュートリアルは英語版からAIによって翻訳されています。原文を確認するには、 ここをクリックしてください

はじめに

この実験では、C++ で配列を使わずにユーザーが入力した n 個の数の平均を計算する方法を学びます。コードを1行ずつ見て、それがどのように機能するかを理解します。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL cpp(("C++")) -.-> cpp/BasicsGroup(["Basics"]) cpp(("C++")) -.-> cpp/ControlFlowGroup(["Control Flow"]) cpp(("C++")) -.-> cpp/IOandFileHandlingGroup(["I/O and File Handling"]) cpp(("C++")) -.-> cpp/StandardLibraryGroup(["Standard Library"]) cpp(("C++")) -.-> cpp/SyntaxandStyleGroup(["Syntax and Style"]) 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;

その後、ユーザーに1つずつ各数を入力してもらいます。ユーザー入力を取得し、入力された数を合計に加えるためにforループを使用します。

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ループを使ってユーザーからの入力を読み取り、入力された数の合計と平均を計算しました。最後に、結果をユーザーに出力しました。