[Prebuild] ITE1010 21' Midterm

한양대학교 정보시스템학과 2021학년도 2학기 C++프로그래밍 중간고사와 관련된 노트입니다.

1.

#include <iostream>
using namespace std;
int func(int a[], int k) {
    static int m = 0;
    a[k] = a[k] + k;
    if(m == 0)
        k = k + 1;
    m = (m + 1) % 2;

    return k;
}

int main(void) {
    int a[] = {3, 3, 2, 2, 1};
    int i = 0;
    while(i < 5)
        i = func(a,i);
    int j = 0;
    while(j < 5) {
        cout << a[j] << ", ";
        j++;
    }
    return 0;
}

1-1.

출력결과를 작성하시오.

정답

3, 5, 6, 8, 9,

해설

while 문 내부에서 func(a, 0)를 제일 처음에 호출하면, func(a,k)에서 k0이므로 a[0] = a[0] + 0이므로 a[0]3이다. m0 이므로 kk+1이 되어 값은 1이 되고, m = (m + 1) % 2m값을 다시 계산하면 1이다. 따라서 첫번째 호출에서는 1을 반환하며 while문의 순환자 i1로 변경한다.

This post is for subscribers only

Subscribe to jiwon.me

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe