一時変数を使わずに 2 つの数値を入れ替える

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

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

はじめに

2 つの数値を入れ替えるとは、それらの値を入れ替えることを意味します。つまり、最初の数値の初期値は、今では 2 番目の数値の値になり、その逆も同じです。この実験では、3 番目の変数を使用せずに 2 つの数値を入れ替える 2 つの方法を学びます。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL cpp(("C++")) -.-> cpp/BasicsGroup(["Basics"]) cpp(("C++")) -.-> cpp/FunctionsGroup(["Functions"]) cpp(("C++")) -.-> cpp/AdvancedConceptsGroup(["Advanced Concepts"]) cpp(("C++")) -.-> cpp/IOandFileHandlingGroup(["I/O and File Handling"]) cpp(("C++")) -.-> cpp/SyntaxandStyleGroup(["Syntax and Style"]) cpp/BasicsGroup -.-> cpp/variables("Variables") cpp/BasicsGroup -.-> cpp/operators("Operators") cpp/FunctionsGroup -.-> cpp/function_parameters("Function Parameters") cpp/AdvancedConceptsGroup -.-> cpp/references("References") cpp/IOandFileHandlingGroup -.-> cpp/output("Output") cpp/IOandFileHandlingGroup -.-> cpp/user_input("User Input") cpp/IOandFileHandlingGroup -.-> cpp/files("Files") cpp/SyntaxandStyleGroup -.-> cpp/code_formatting("Code Formatting") subgraph Lab Skills cpp/variables -.-> lab-96217{{"一時変数を使わずに 2 つの数値を入れ替える"}} cpp/operators -.-> lab-96217{{"一時変数を使わずに 2 つの数値を入れ替える"}} cpp/function_parameters -.-> lab-96217{{"一時変数を使わずに 2 つの数値を入れ替える"}} cpp/references -.-> lab-96217{{"一時変数を使わずに 2 つの数値を入れ替える"}} cpp/output -.-> lab-96217{{"一時変数を使わずに 2 つの数値を入れ替える"}} cpp/user_input -.-> lab-96217{{"一時変数を使わずに 2 つの数値を入れ替える"}} cpp/files -.-> lab-96217{{"一時変数を使わずに 2 つの数値を入れ替える"}} cpp/code_formatting -.-> lab-96217{{"一時変数を使わずに 2 つの数値を入れ替える"}} end

新しい C++ ファイルを作成する

ターミナルを開き、以下のコマンドでプロジェクトディレクトリに移動します。

cd ~/project

以下のコマンドを使用して、swap.cppという名前の新しい C++ ファイルを作成します。

touch swap.cpp

好きなコードエディタでswap.cppを開きます。

+ 演算子と - 演算子を使って 2 つの数値を入れ替えるコードを書く

このステップでは、 + 演算子と - 演算子を使って 2 つの数値を入れ替えるコードを書きます。このコードは、ユーザーから 2 つの数値を取得し、3 番目の変数を使用せずにそれらを入れ替える必要があります。

// swap.cpp
#include <iostream>
using namespace std;

void swap_numbers(int &x, int &y) {
    x = x + y;
    y = x - y;
    x = x - y;
}

int main() {
    int a, b;
    cout << "Enter the value of a: ";
    cin >> a;
    cout << "Enter the value of b: ";
    cin >> b;

    cout << "Before swapping: " << endl;
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;

    swap_numbers(a, b);

    cout << "After swapping: " << endl;
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;

    return 0;
}

このコードでは、2 つの整数型の引数を受け取り、入れ替え操作を行う swap_numbers 関数を作成しました。ユーザーから 2 つの数値 ab を取得し、それらの値を入れ替える swap_numbers 関数を呼び出します。最後に、 ab の入れ替えられた値を出力します。

プログラムを実行するには、以下のコマンドを使用します。

g++ swap.cpp -o swap &&./swap

* 演算子と / 演算子を使って 2 つの数値を入れ替えるコードを書く

このステップでは、 * 演算子と / 演算子を使って 2 つの数値を入れ替えるコードを書きます。このコードは、ユーザーから 2 つの数値を取得し、3 番目の変数を使用せずにそれらを入れ替える必要があります。

// swap.cpp
#include <iostream>
using namespace std;

void swap_numbers(int &x, int &y) {
    x = x * y;
    y = x / y;
    x = x / y;
}

int main() {
    int a, b;
    cout << "Enter the value of a: ";
    cin >> a;
    cout << "Enter the value of b: ";
    cin >> b;

    cout << "Before swapping: " << endl;
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;

    swap_numbers(a, b);

    cout << "After swapping: " << endl;
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;

    return 0;
 }

このコードでも、2 つの整数型の引数を受け取り、入れ替え操作を行う swap_numbers 関数を作成しました。ユーザーから 2 つの数値 ab を取得し、それらの値を入れ替える swap_numbers 関数を呼び出します。最後に、 ab の入れ替えられた値を出力します。この入れ替え方法では、加算と減算の代わりに乗算と除算の操作を使用します。

プログラムを実行するには、以下のコマンドを使用します。

g++ swap.cpp -o swap &&./swap

最終プログラム

ここに、加算・減算と乗算・除算の両方の方法で 2 つの数値を入れ替える swap.cpp プログラムの完全なコードがあります。

// swap.cpp
#include <iostream>
using namespace std;

void add_subtract_swap(int &x, int &y) {
    x = x + y;
    y = x - y;
    x = x - y;
}

void multiply_divide_swap(int &x, int &y) {
    x = x * y;
    y = x / y;
    x = x / y;
}

int main() {
    int a, b;
    cout << "Enter the value of a: ";
    cin >> a;
    cout << "Enter the value of b: ";
    cin >> b;

    cout << "Before swapping: " << endl;
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;

    add_subtract_swap(a, b);

    cout << "After swapping using + and - operators: " << endl;
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;

    multiply_divide_swap(a, b);

    cout << "After swapping using * and / operators: " << endl;
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;

    return 0;
}

コンパイルと実行

プログラムをコンパイルして実行するには、以下のコマンドを使用します。

g++ swap.cpp -o swap &&./swap

このプログラムは、ユーザーに 2 つの数値を求め、加算・減算と乗算・除算の両方の方法でそれらを入れ替えます。出力には、 ab の初期値、加算・減算を使って入れ替えた値、および乗算・除算を使って入れ替えた値が表示されます。

まとめ

入れ替えはプログラミングにおいて重要な操作であり、多くのアルゴリズムで必要とされることが頻繁です。この実験では、3 番目の変数を使用せずに 2 つの数値を入れ替える 2 つの方法を学びました。1 つは加算・減算を使用する方法で、もう 1 つは乗算・除算を使用する方法です。また、入れ替え操作を行う関数を作成する方法も学び、コードをよりモジュール化して読みやすくしました。