[C/C++] c++ 2차원 배열 동적 할당

Posted in 단체/ㄴ대학 전공 수업 by

#include <iostream>

using namespace std;

int main(){
    int i=0, j=0;
    int students, exams;

    cin>>students;
    cin>>exams;

    int **std = new int* [students];
    for(i=0; i<students; i++)
        std[i] = new int[exams];

    for(i=0; i<students; i++)
        for(j=0; j<exams; j++)
            std[i][j] = j;

    for(i=0; i<students; i++){
        for(j=0; j<exams; j++)
            cout<<std[i][j];
        cout<<endl;
    }

    if(std){
        for(i=0; i<students; i++)
            delete[] std[i];
        delete[] std;
    }

    return 0;
}

결과값 :

5 3                   // 입력 값 : 5 3
012                  // 출력값들
012
012
012
012
Press any key to continue