Computer >> 컴퓨터 >  >> 프로그래밍 >> C 프로그래밍

C 언어에서 2차원 배열이란? 개념부터 활용 예제까지 한눈에 보기

배열(Array)이란?

배열은 서로 관련 있는 데이터들을 하나의 공통된 이름으로 묶어 저장하는 자료 구조입니다. C 언어에서 배열을 사용하면 여러 개의 변수를 따로 선언할 필요 없이 효율적으로 데이터를 관리할 수 있습니다.

배열 선언 문법

배열을 선언하는 기본 문법은 다음과 같습니다.

datatype array_name [size];

배열의 종류

C 언어의 배열은 크게 세 가지 유형으로 분류됩니다.

  • 1차원 배열(One-dimensional array)
  • 2차원 배열(Two-dimensional array)
  • 다차원 배열(Multi-dimensional array)

배열의 초기화 방식

배열은 다음 두 가지 방법으로 초기화할 수 있습니다.

  • 컴파일 타임 초기화(Compile time initialization): 코드 작성 시 값을 미리 지정
  • 런타임 초기화(Runtime initialization): 프로그램 실행 중 사용자 입력으로 값 지정

2차원 배열(Two-dimensional Array)

2차원 배열은 표(table) 형태의 데이터를 저장하거나 행렬(matrix) 연산 등에 활용되는 배열입니다. 행(row)과 열(column)로 구성되어 있어 데이터를 직관적으로 다룰 수 있습니다.

선언 문법

2차원 배열을 선언하는 문법은 아래와 같습니다.

datatype array_name [rowsize] [columnsize];

예시: int a[5][5]; → 5행 5열 크기의 정수형 2차원 배열 선언

a[0][0]
10
a[0][1]
20
a[0][2]
30
a[1][0]
40
a[1][1]
50
a[1][2]
60
a[2][0]a[2][1]a[2][2]


예제 1: 컴파일 타임 초기화

아래는 컴파일 타임에 배열 값을 미리 지정하여 출력하는 C 프로그램입니다.

#include<stdio.h>
main ( ){
    int a[3][3] = {10,20,30,40,50,60,70,80,90};
    int i,j;
    printf ("elements of the array are");
    for ( i=0; i<3; i++){
        for (j=0;j<3; j++){
            printf("%d \t", a[i] [j]);
        }
        printf("\n");
    }
}

실행 결과

elements of the array are:
10 20 30
40 50 60
70 80 90

예제 2: 런타임 초기화

아래는 실행 중 사용자로부터 값을 입력받아 배열을 초기화한 뒤 출력하는 C 프로그램입니다.

#include<stdio.h>
main ( ){
    int a[3][3] ,i,j;
    printf ("enter elements of array");
    for ( i=0; i<3; i++){
        for (j=0;j<3; j++){
            scanf("%d", &a[i] [j]);
        }
    }
    printf("elements of the array are");
    for ( i=0; i<3; i++){
        for (j=0;j<3; j++){
            printf("%d\t", a[i] [j]);
        }
        printf("\n");
    }
}

실행 결과

Enter elements of array :
1 2 3 4 5 6 7 8 9
Elements of the array are
1 2 3
4 5 6
7 8 9

예제 3: 두 배열의 합과 곱 계산하기

아래는 런타임에 두 개의 2차원 배열 A와 B를 입력받아, 각 요소의 합(sum)과 곱(product)을 계산해 출력하는 C 프로그램입니다.

#include<stdio.h>
void main(){
    //런타임 방식으로 배열 선언//
    int A[2][3],B[2][3],i,j,sum[i][j],product[i][j];
    //for문을 이용해 배열 A와 B에 값 입력//
    printf("Enter elements into the array A: \n");
    for(i=0;i<2;i++){
        for(j=0;j<3;j++){
            printf("A[%d][%d] :",i,j);
            scanf("%d",&A[i][j]);
        }
        printf("\n");
    }
    for(i=0;i<2;i++){
        for(j=0;j<3;j++){
            printf("B[%d][%d] :",i,j);
            scanf("%d",&B[i][j]);
        }
        printf("\n");
    }
    //합계 계산 후 출력//
    printf("Sum array is : \n");
    for(i=0;i<2;i++){
        for(j=0;j<3;j++){
            sum[i][j]=A[i][j]+B[i][j];
            printf("%d\t",sum[i][j]);
        }
        printf("\n");
    }
    //곱셈 결과 계산 후 출력//
    printf("Product array is : \n");
    for(i=0;i<2;i++){
        for(j=0;j<3;j++){
            product[i][j]=A[i][j]*B[i][j];
            printf("%d\t",product[i][j]);
        }
        printf("\n");
    }
}

실행 결과

Enter elements into the array A:
A[0][0] :2
A[0][1] :3
A[0][2] :1
A[1][0] :2
A[1][1] :4
A[1][2] :5
B[0][0] :1
B[0][1] :2
B[0][2] :3
B[1][0] :5
B[1][1] :6
B[1][2] :7
Sum array is :
3 5 4
7 10 12
Product array is :
2 6 3
10 24 35