C++ Snippets

Queue, Bubble Sort (without swapped & with swapped) & 2D Array — tap "Copy All" to copy all code in one click.

main.cpp (Queue + Bubble Sort without & with swapped)
// Queue
#include <iostream>
#include <queue>

using namespace std;
    int main() {
        queue<int> order_num;
        
        order_num.push(5);
        order_num.push(6);
        order_num.push(7);
        order_num.push(8);
        
        while(!order_num.empty()) {
            cout << order_num.front();
            cout << endl;
            
            order_num.pop();
            cout << endl;
            
            
        }
    }



// bubble sort without swapped

#include <iostream>
using namespace std;

int main() {
    int n = 0;
    int traversal = 0;

    cout << "Enter the size of the array: ";
    cin >> n;

    int array[n];

    for (int i = 0; i < n; i++) {
        cout << "Enter the number of index [" << i << "]: ";
        cin >> array[i];
    }

    // Bubble sorting
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - 1 - i; j++) {
            if (array[j] > array[j + 1]) {
                int temp = array[j];
                array[j] = array[j + 1];
                array[j + 1] = temp;
            }
            traversal++;
        }

        for (int i = 0; i < n; i++) {
            cout << array[i] << " ";
        }
        cout << endl;
    }

    cout << "Traversal: " << traversal;

    return 0;
}


// bubble sort with swapped
#include <iostream>
using namespace std;

int main() {
    int n = 0;
    int traversal = 0;

    cout << "Enter the size of the array: ";
    cin >> n;

    int array[n];

    for (int i = 0; i < n; i++) {
        cout << "Enter the number of index [" << i << "]: ";
        cin >> array[i];
    }

    // Bubble Sorting
    for (int i = 0; i < n - 1; i++) {
        bool swapped = false;

        for (int j = 0; j < n - 1 - i; j++) {
            if (array[j] > array[j + 1]) {
                int temp = array[j];
                array[j] = array[j + 1];
                array[j + 1] = temp;

                swapped = true;
            }

            traversal++;
        }

        for (int i = 0; i < n; i++) {
            cout << array[i] << " ";
        }
        cout << endl;

        if (!swapped) {
            break;
        }
    }

    cout << "Traversal: " << traversal;

    return 0;
}
2d_array.cpp (2D Array Input & Print)
#include <iostream>
using namespace std;

int main() {
    
    int n;
    cout << "Enter the size of the array: ";
    cin >> n;
    
    int arr[n][n];
    
    for (int i = 0; i < n; i++) {
        for (int j = 0;j < n; j++) {
            cout << "Enter a number for index [" << i << "][" << j << "]: ";
            cin >> arr[i][j];
        }
    }
    
    cout << endl;
    cout << endl;
    
    for (int i = 0; i < n; i++) {
        for (int j = 0;j < n; j++) {
            if (true) {
                cout << arr[i][j] << " ";
            } else {
                cout << "  ";
            }
        }
        cout << endl;
    }
}