본문 바로가기
School Study/2021-2

[week 13] C++ 복습_8장 다차원 배열

by 전전긍긍 2021. 12. 9.

20211125_C++_공부기록

 

  • 2차원 배열의 선언
  • 2차원 배열의 초기화 ( 임의의 값으로 배열 초기화, 배열 출력, 입력값으로 초기화, 모든 요소의 합, 각 열의 합)
  • 2차원 배열을 함수에 전달

2차원 배열의 선언

 

 - 행렬이나 표를 저장하기 위해서 2차원 배열을 사용

 - 2차원 배열도 1차원 배열과 같이 선언과 동시에 초기화가 가능하다.

요소유형 배열이름[행수][열수];
int martix[5][5];

 

  • 임의의 값으로 배열 초기화
const int ROW_SIZE = 2;
const int COL_SIZE = 3;

int matrix[ROW_SIZE][COL_SIZE};

for (int row = 0; row < ROW_SIZE; row++)
	for (int col = 0; col < COL_SIZE; col++)
    	 matrix[row][col] = rand() % 100;

 

  • 배열 출력
for (int row = 0; row < ROW_SIZE; row++)
	for (int col = 0; col < COL_SIZE; col++)
    	cout << matrix[row][col] << " ";

 

  • 입력 값으로 배열 초기화
#include <iostream>
using namespace std;

int main()
{
	const int ROW_SIZE = 2;
	const int COL_SIZE = 3;
	int matrix[ROW_SIZE][COL_SIZE];

	cout << ROW_SIZE << "행 " << COL_SIZE << "열의 값을 입력하시오 : " << endl;

	//입력코드
	for (int row = 0; row < ROW_SIZE; row++)
		for (int col = 0; col < COL_SIZE; col++)
			cin >> matrix[row][col];

	//출력코드
	for (int row = 0; row < ROW_SIZE; row++)
	{
		for (int col = 0; col < COL_SIZE; col++)
		{
			cout << matrix[row][col] << " ";
		}
		cout << endl;
	}
	return 0;
}

 

  • 모든 요소의 합 구하기
//모든 요소의 합 구하기
	int total = 0;
	for (int row = 0; row < ROW_SIZE; row++)
	{
		for (int col = 0; col < COL_SIZE; col++)
		{
			total += matrix[row][col];
		}
	}
	cout << " total = " << total << endl;

 

  • 각 열(행)의 합 구하기
//각 열(행)의 합 구하기

//각 열의 합 구하기
	for (int col = 0; col < COL_SIZE; col++)
	{
		int total = 0;
		for (int row = 0; row < ROW_SIZE; row++)
		{
			total += matrix[row][col];
		}
		cout << col << "열의 합 : " << total << endl;
	}

//각 행의 합 구하기
	for (int row = 0; row < ROW_SIZE; row++)
	{
		int total = 0;
		for (int col = 0; col < COL_SIZE; col++)
		{
			total += matrix[row][col];
		}
		cout << row << "행의 합 : " << total << endl;
     }

2차원 배열을 함수에 전달

 

 - 2차원 배열을 함수로 전달할 때, C++에서는 함수 매개변수의 유형 선언에서 열(column)의 크기를 지정해야 한다.

#include <iostream>
using namespace std;

void printArray(int a[][4], int rowSize); //함수원형

int main()
{
	const int ROW_SIZE = 3;
	int numbers[ROW_SIZE][4] = { {1,4,3,6}, {3,4,5,2}, {9,3,4,8} };
	printArray(numbers, 3);

	return 0;
}

void printArray(int a[][4], int rowSize)
{
	for (int row = 0; row < rowSize; row++)
	{
		for (int col = 0; col < 4; col++)
		{
			cout << a[row][col] << " ";
		}
		cout << endl;
	}

}

 

  • 예제 : 시험 문제 채점
#include <iostream>
using namespace std;

int main()
{
	const int STU = 4;
	const int QUE = 10;

	int answer[STU][QUE] = 
	{ {1, 2, 3, 4, 4, 2, 1, 1, 3, 3}, {4, 2, 1, 3, 1, 2, 3, 2, 4, 1}, 
		{3, 2, 4, 2, 4, 2, 3, 1, 2, 4}, {1, 2, 1, 3, 2, 2, 3, 1, 3, 3} };

	int keys[] = { 1, 2 , 1, 3, 2, 2, 3, 1, 3, 3 };

	for (int stu = 0; stu < STU; stu++)
	{
		int correct = 0;
		for (int ans = 0; ans < QUE; ans++)
		{
			if (answer[stu][ans] == keys[ans])
				correct++;
		}

		cout << stu << "학생의 맞은 개수는 " << correct << endl;
	}
	return 0;
}

/*
0학생의 맞은 개수는 6
1학생의 맞은 개수는 5
2학생의 맞은 개수는 4
3학생의 맞은 개수는 10
*/

 

//2차원 배열을 함수에 전달
#include <iostream>
using namespace std;

const int QUE = 10; //열의 개수는 전역함수로
void grade(int[][QUE], int); //함수원형

int main()
{
	const int STU = 4;

	int answer[STU][QUE] = 
	{ {1, 2, 3, 4, 4, 2, 1, 1, 3, 3}, {4, 2, 1, 3, 1, 2, 3, 2, 4, 1}, 
		{3, 2, 4, 2, 4, 2, 3, 1, 2, 4}, {1, 2, 1, 3, 2, 2, 3, 1, 3, 3} };

	
	grade(answer, STU);
	return 0;

	
}

void grade(int list[][QUE], int size)
{
	int keys[] = { 1, 2 , 1, 3, 2, 2, 3, 1, 3, 3 };

	for (int stu = 0; stu < size; stu++)
	{
		int correct = 0;
		for (int ans = 0; ans < QUE; ans++)
		{
			if (list[stu][ans] == keys[ans])
				correct++;
		}

		cout << stu << "학생의 맞은 개수는 " << correct << endl;
	}
}
/*
0학생의 맞은 개수는 6
1학생의 맞은 개수는 5
2학생의 맞은 개수는 4
3학생의 맞은 개수는 10
*/

 


다차원 배열

 

const int CLASS = 2;
const int NUM_OF_STUDENTS = 4;
const int NUM_OF_QUESTIONS = 10; //2반*학생4명*10문제답


int answers[CLASS][NUM_OF_STUDENTS][NUM_OF_QUESTIONS] = 
   	{ {{1, 2, 3, 4, 4, 2, 1, 1, 3, 3}, //1반
    	  {4, 2, 1, 3, 1, 2, 3, 2, 4, 1},
    	  {3, 2, 4, 2, 4, 2, 3, 1, 2, 4},
    	  {1, 2, 1, 3, 2, 2, 3, 1, 3, 3}},

	  {{1, 2, 3, 4, 4, 2, 1, 1, 3, 3}, //2반
    	  {4, 2, 1, 3, 1, 2, 3, 2, 4, 1},
    	  {3, 2, 4, 2, 4, 2, 3, 1, 2, 4},
    	  {1, 2, 1, 3, 2, 2, 3, 1, 3, 3}}  };

cout << answers[0][1][0]; //1반 학생2의 1번째 문제 답,4
cout << answers[1][3][9]; //2반 학생4의  10번째 문제 답,3