← Back to Analisis Kode
Prev

Question 9 — Bug pada Merge

Analisis Kode · 20 points · Answer key: 8 17

Next

Question

#include <iostream>
using namespace std;
int A[5] = {1, 3, 5, 6, 18};
int B[5] = {2, 4, 7, 11, 16};
int C[10];
int main() {
    int i = 0, j = 0, k = 0;
    while (i < 5 || j < 5) {
        if (A[i] <= B[j]) {
            C[k] = A[i];
            i++; k++;
        } else {
            C[k] = B[j];
            j++; k++;
        }
    }
    if (i > j) {
        while (i < 5) {
            C[k] = A[i];
            i++; k++;
        }
    } else {
        while (j < 5) {
            C[k] = B[j];
            j++; k++;
        }
    }
    for (int l = 0; l < 10; l++) {
        cout << C[l] << " ";
    }
    cout << endl;
    return 0;
}

Program ini seharusnya menggabungkan dua array terurut menjadi satu array terurut. Namun terdapat bug sebanyak dua baris sehingga output keliru. Pada baris ke berapa sajakah bug tersebut berada?

Jawab: …………………………………………………… {tuliskan dua nomor baris terurut menaik}

Answers (7 members)

MemberAnswer
Yazid8, 17
Habibie18 & 23
Kian16, 17
Priscilla17 22
Athmar17, 23
Algazel-
Athar-