C++

[C++ 3-3] 배열 기반 반복문

KimKyao 2024. 10. 7. 21:39
#include <iostream>

using namespace std;


int main()
{
    //배결 기반 반복문
    int a[10] = { 1, 3, 5, 7, 9 };

    for (int i = 0; i < 5; i++) {
        cout << a[i];
    } // 13579
    cout << "\n";

    for (int i : a) { // 콜론 다음 배열을 넣어준다.
        cout << i;
    } // 1357900000
    cout << "\n";

    //중첩 루프 : 2차원 배열
    int temp[4][5] =
    {
        {1,2,3,4,5},
        {11,22,33,44,55},
        {111,222,333,444,555},
        {1111,2222,3333,4444,5555},
    };
    cout << temp[0][0] << "\n";

    // 4개의 원소를 갖고, 각각의 원소는 5개씩 원소를 갖는다

    for (int row = 0; row < 4; row++) {
        for (int col = 0; col < 5; col++) {
            cout << temp[row][col] << " ";
        }
        cout << "\n";
    }

    return 0;
}