C++ · Array & Sorting

Activity · คำถามในสไลด์

จะเกิดอะไรขึ้น ถ้ากำหนดค่าเกินขอบเขตของอาร์เรย์

int score[45]; แล้วสั่ง score[50] = 98;

คำตอบ พฤติกรรมไม่แน่นอน (undefined behavior)

ตรวจสอบความเข้าใจกันหน่อย

int score[8]; รับค่า 5 ตัว: 20 15 10 25 35 (ลงช่อง 0–4)

คำตอบ 1) 15   2) 55   3) ค่าไม่แน่นอน   4) 20
#include <iostream>
using namespace std;
int main() {
    int score[8];
    for (int i = 0; i <= 4; i++) {
        cout << "Input score " << i << " : ";
        cin >> score[i];
    }
    cout << "\n1. " << score[1] << "\n2. " << score[0] + score[4] << "\n3. " << score[2+3] << "\n4. " << score[2] + 10 << "\n";
    return 0;
}

วิเคราะห์โจทย์... บอก Output

n[5] = {-10, 10, 3, -9, 3} — ถ้าติดลบใช้ abs ถ้าไม่ติดลบใช้ pow(n[i], i) แล้วเก็บกลับด้านที่ m[4 - i]

คำตอบ 81 9 9 10 10
#include <iostream>
#include <cmath>
using namespace std;

int main() {
    int n[5] = {-10, 10, 3, -9, 3}, m[5];
    for (int i = 0; i < 5; i++) {
        if (n[i] < 0)
            m[4 - i] = abs(n[i]);
        else
            m[4 - i] = pow(n[i], i);
    }
    for (int i = 0; i < 5; i++)
        cout << m[i] << " ";
    return 0;
}

มาทดสอบกันหน่อย (Array 2 มิติ)

int a[][2] = {{5, 8}, {9}, {-1}};

คำตอบ a[0][1] = 8   a[2][1] = 0   a[1][0] = 9
#include <iostream>
using namespace std;
int main() {
    int a[][2] = {{5, 8}, {9}, {-1}};
    cout << "a[0][1] = " << a[0][1] << "\n";
    cout << "a[2][1] = " << a[2][1] << "\n";
    cout << "a[1][0] = " << a[1][0] << "\n";
    return 0;
}

อาร์เรย์นี้... มีขนาดเท่าใด

int x[2][5]; char names[3][4]; float nums[2][2];

คำตอบ x = 40   names = 12   nums = 16
#include <iostream>
using namespace std;
int main() {
    int x[2][5];
    char names[3][4];
    float nums[2][2];
    cout << "Size of x = " << sizeof(x) << "\n";
    cout << "Size of names = " << sizeof(names) << "\n";
    cout << "Size of nums = " << sizeof(nums) << "\n";
    return 0;
}

Array 1 มิติ

Array01_1

รับคะแนน (0-100) ของนักเรียน 10 คน เก็บในอาร์เรย์

#include<iostream>
using namespace std;
int main() {
    int score[10];
    for (int i = 0; i < 10; i++) {
        cout << "Input score " << i << " : ";
        cin >> score[i];
        if (score[i] < 0 || score[i] > 100) {cout << "Invalid input!!"; return 0;}
    }
    for (int i = 0; i < 10; i++) {
        cout << "score[" << i << "] : " << score[i] << '\n';
    }
    return 0;
}

Array01_2

แสดงคะแนนจากคนสุดท้ายย้อนกลับไปคนที่ 1

#include<iostream>
using namespace std;
int main() {
    int score[10];
    for (int i = 0; i < 10; i++) {
        cout << "Input score " << i << " : ";
        cin >> score[i];
        if (score[i] < 0 || score[i] > 100) {cout << "Invalid input!!"; return 0;}
    }
    for (int i = 9; i >= 0; i--) {
        cout << "score[" << i << "] : " << score[i] << '\n';
    }
    return 0;
}

Array01_3

หาผลรวมและค่าเฉลี่ยของคะแนน

#include<iostream>
using namespace std;
int main() {
    int score[10];
    int sum = 0;
    for (int i = 0; i < 10; i++) {
        cout << "Input score " << i << " : ";
        cin >> score[i];
        if (score[i] < 0 || score[i] > 100) {cout << "Invalid input!!"; return 0;}
        sum += score[i];
    }
    cout << "Sum = " << sum << '\n';
    cout << "Average = " << sum*1.0/10;
    return 0;
}

Array01_4

แสดงคะแนนย้อนกลับ และนับคนสอบผ่าน (>= 50) กับสอบตก

#include<iostream>
using namespace std;
int main() {
    int score[10];
    int pass = 0;
    int fail = 0;
    for (int i = 0; i < 10; i++) {
        cout << "Input score " << i << " : ";
        cin >> score[i];
        if (score[i] < 0 || score[i] > 100) {cout << "Invalid input!!"; return 0;}
    }
    for (int i = 9; i >= 0; i--) {
        cout << "score[" << i << "] : " << score[i] << '\n';
        if (score[i] >= 50) ++pass;
        else ++fail;
    }
    cout << "Pass = " << pass << '\n';
    cout << "Fail = " << fail;
    return 0;
}

Array02_1

หาค่ามากที่สุดของอาร์เรย์

#include<iostream>
using namespace std;
int main() {
    int a[5] = {-1, 6, 9, 2, -9};
    int max_ = a[0];
    for (int i = 1; i < 5; i++) {
        if (a[i] > max_) max_ = a[i];
    }
    cout << "Max = " << max_;
    return 0;
}

Array02_2

หาค่าน้อยที่สุดของอาร์เรย์

#include<iostream>
using namespace std;
int main() {
    int a[5] = {-1, 6, 9, 2, -9};
    int min_ = a[0];
    for (int i = 1; i < 5; i++) {
        if (a[i] < min_) min_ = a[i];
    }
    cout << "Min = " << min_;
    return 0;
}

Array02_3

หาค่าสัมบูรณ์ของอาร์เรย์ a เก็บไว้ในอาร์เรย์ b

#include<iostream>
using namespace std;
int main() {
    int a[5] = {-1, 6, 9, 2, -9};
    int b[5];
    for (int i = 0; i < 5; i++) {
        if (a[i] < 0) b[i] = -a[i];
        else b[i] = a[i];
    }
    for (int i = 0; i < 5; i++) {
        cout << "a[" << i << "] = " << a[i] << "\tb[" << i << "] = " << b[i] << '\n';
    }
    return 0;
}

Array03_1

รับค่าจำนวนเต็ม (0-20) 10 ตัว เก็บในอาร์เรย์ แล้วแสดงเป็นตาราง

#include<iostream>
using namespace std;
int main() {
    int a[10];
    for (int i = 0; i < 10; i++) {
        cout << "Input " << i << " : ";
        cin >> a[i];
        if (a[i] < 0 || a[i] > 20) {cout << "Invalid input!!"; return 0;}
    }
    cout << "Element\tvalue\n";
    for (int i = 0; i < 10; i++) {
        cout << i << "\t" << a[i] << '\n';
    }
    return 0;
}

Array03_2

แสดงตาราง พร้อม Histogram (ดาว * ตามค่า)

#include<iostream>
using namespace std;
int main() {
    int a[10];
    for (int i = 0; i < 10; i++) {
        cout << "Input " << i << " : ";
        cin >> a[i];
        if (a[i] < 0 || a[i] > 20) {cout << "Invalid input!!"; return 0;}
    }
    cout << "Element\tvalue\tHistogram\n";
    for (int i = 0; i < 10; i++) {
        cout << i << "\t" << a[i] << "\t";
        for (int j = 0; j < a[i]; j++) {
            cout << "*";
        }
        cout << '\n';
    }
    return 0;
}

Array 2 มิติ

Array04_1

รับคะแนน (0-100) เก็บในอาร์เรย์ 3 x 3 แล้วแสดงเป็นตาราง

#include<iostream>
using namespace std;
int main() {
    int arr[3][3];
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            cout << "Input arr[" << i << "][" << j << "]: ";
            cin >> arr[i][j];
            if (arr[i][j] < 0 || arr[i][j] > 100) {cout << "Invalid input!!"; return 0;}
        }
    }
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            cout << arr[i][j] << "\t";
        }
        cout << '\n';
    }
    return 0;
}

Array04_2

ยกกำลังสองแต่ละช่อง เก็บในอาร์เรย์อีกตัว แล้วแสดงเป็นตาราง 3 x 3

#include<iostream>
using namespace std;
int main() {
    int arr[3][3];
    int sq[3][3];
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            cout << "Input arr[" << i << "][" << j << "]: ";
            cin >> arr[i][j];
            if (arr[i][j] < 0 || arr[i][j] > 100) {cout << "Invalid input!!"; return 0;}
            sq[i][j] = arr[i][j]*arr[i][j];
        }
    }
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            cout << sq[i][j] << "\t";
        }
        cout << '\n';
    }
    return 0;
}

Array05_1

สร้างเมทริกซ์ 3 x 2 สองตัว รับค่า แล้วบวกเมทริกซ์

#include<iostream>
using namespace std;
int main() {
    int A[3][2];
    int B[3][2];
    int C[3][2];
    cout << "Matrix A\n";
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 2; j++) {
            cout << "A[" << i << "][" << j << "]: ";
            cin >> A[i][j];
        }
    }
    cout << "Matrix B\n";
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 2; j++) {
            cout << "B[" << i << "][" << j << "]: ";
            cin >> B[i][j];
        }
    }
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 2; j++) {
            C[i][j] = A[i][j]+B[i][j];
        }
    }
    cout << "A + B\n";
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 2; j++) {
            cout << C[i][j] << "\t";
        }
        cout << '\n';
    }
    return 0;
}

Array05_2

รับเมทริกซ์ 3 x 2 แล้วแสดงทรานสโพส (2 x 3)

#include<iostream>
using namespace std;
int main() {
    int A[3][2];
    int T[2][3];
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 2; j++) {
            cout << "A[" << i << "][" << j << "]: ";
            cin >> A[i][j];
            T[j][i] = A[i][j];      // แถว <-> หลัก
        }
    }
    cout << "Transpose\n";
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            cout << T[i][j] << "\t";
        }
        cout << '\n';
    }
    return 0;
}

Sorting

Selection Sort

หาตัวน้อยสุดในส่วนที่เหลือ แล้วสลับมาไว้หน้าสุด

#include<iostream>
using namespace std;
int main() {
    int n;
    int arr[100];
    cout << "Enter n (1-100): ";
    cin >> n;
    if (n < 1 || n > 100) return 0;
    for (int i = 0; i < n; i++) {
        cout << "Enter number " << i+1 << ": ";
        cin >> arr[i];
    }
    for (int i = 0; i < n - 1; i++) {
        int min_idx = i;
        for (int j = i + 1; j < n; j++) {
            if (arr[j] < arr[min_idx]) min_idx = j;
        }
        int temp = arr[min_idx];
        arr[min_idx] = arr[i];
        arr[i] = temp;
    }
    cout << "Sorted: ";
    for (int i = 0; i < n; i++) cout << arr[i] << " ";
    return 0;
}

Bubble Sort

เทียบคู่ที่ติดกัน ผิดลำดับก็สลับ ตัวใหญ่จะลอยไปท้ายสุด

#include<iostream>
using namespace std;
int main() {
    int n;
    int arr[100];
    cout << "Enter n (1-100): ";
    cin >> n;
    if (n < 1 || n > 100) return 0;
    for (int i = 0; i < n; i++) {
        cout << "Enter number " << i+1 << ": ";
        cin >> arr[i];
    }
    for (int i = 0; i < n - 1; i++) {
        bool swapped = false;
        for (int j = 0; j < n - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
                swapped = true;
            }
        }
        if (!swapped) break;
    }
    cout << "Sorted: ";
    for (int i = 0; i < n; i++) cout << arr[i] << " ";
    return 0;
}

Insertion Sort

หยิบทีละตัว (key) แล้วเสียบลงที่ที่ถูกต้อง เหมือนจัดไพ่

#include<iostream>
using namespace std;
int main() {
    int n;
    int arr[100];
    cout << "Enter n (1-100): ";
    cin >> n;
    if (n < 1 || n > 100) return 0;
    for (int i = 0; i < n; i++) {
        cout << "Enter number " << i+1 << ": ";
        cin >> arr[i];
    }
    for (int i = 1; i < n; i++) {
        int key = arr[i];
        int j = i - 1;
        while (j >= 0 && arr[j] > key) {
            arr[j + 1] = arr[j];
            j = j - 1;
        }
        arr[j + 1] = key;
    }
    cout << "Sorted: ";
    for (int i = 0; i < n; i++) cout << arr[i] << " ";
    return 0;
}