본문 바로가기
Language/C++

[C++_제 8장] 다차원 배열

by 전전긍긍 2021. 12. 9.

본 포스팅은 C++로 시작하는 객체지향 프로그래밍 책을 바탕으로 작성하였습니다.

 

C++로 시작하는 객체지향 프로그래밍

『C++로 시작하는 객체지향 프로그래밍』은 구문보다는 문제 해결에 중점을 두는 문제 구동 방식을 사용한 프로그래밍에 대해 가르치고 있다. 여러 가지 상황에서 문제를 야기한 개념을 사용함

book.naver.com


key point

 

8.2 2차원 배열 선언

 - 2차원 배열의 요소는 행과 열 인덱스를 사용하여 접근하다.

elementType arrayName[ROW_SIZE][COLUMN_SIZE;]

 

8.3 2차원 배열 처리

 

 - 2차원 배열을 처리하기 위해서 중첩 for문이 종종 사용된다.

//다음을 가정한다.
const int ROW_SIZE = 2; // 행 크기
const int COLUMN_SIZE = 2; // 열 크기
int martix[ROW_SIZE][COLUMN_SIZE];

 

8.3.1 ( 입력 값으로 배열 초기화 )

cout << "Enter " << ROW_SIZE << " rows and " << COLUMN_SIZE << " columns: " << endl;

	for (int i = 0; i < ROW_SIZE; i++)
		for (int j = 0; j < COLUMN_SIZE; j++)
			cin >> matrix[i][j];

 

8.3.2 ( 임의의 값으로 배열 초기화 )

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

 

8.3.3 ( 배열 출력 )

for (int i = 0; i < ROW_SIZE; i++)
	for (int j = 0; j < COLUMN_SIZE; j++)
		cout << matrix[i][j] << " ";

 

8.3.4 ( 모든 요소의 합 구하기 )

int total = 0;
for (int row = 0; row < ROW_SIZE; row++)
	for (int col = 0; col < COLUMN_SIZE; col++)
		total += matrix[row][col];

 

8.3.5 ( 각 열의 합 구하기 )

for (int col = 0; col < COLUMN_SIZE; col++)
{
	int total = 0;
	for (int row = 0; row < ROW_SIZE; row++)
		total = matrix[row][col];
	cout << "Sum for column " << col << " is " << total << endl;
}

 

8.3.6 ( 합이 가장 큰 행 찾기 )

int maxRow = 0;
	int maxRowIndex = 0;

	for (int col = 0; col < COLUMN_SIZE; col++)
	{
		cout << "입력하세요 0행 " << col << "열 ";
		cin >> matrix[0][col];
		maxRow += matrix[0][col];
	}

	for (int row = 1; row < ROW_SIZE; row++)
	{
		int currentTotal = 0;
		for (int col = 0; col < COLUMN_SIZE; col++)
		{
			cout << "입력하세요 " <<  row << "행 " << col << "열 ";
			cin >> matrix[row][col];
			currentTotal += matrix[row][col];
		}

		if (maxRow < currentTotal)
		{
			maxRow = currentTotal;
			maxRowIndex = row;
		}
	}

	cout << "Row " << maxRowIndex << " has the maximum sum of " << maxRow << endl;
	return 0;

 

8.3.7 ( 요소 임의로 섞기 )

srand(time(0));

for (int row = 0; row < ROW_SIZE; row++)
{
	for (int col = 0; col < COLUMN_SIZE; col++)
	{
		int i1 = rand() % ROW_SIZE;
		int j1 = rand() % COLUMN_SIZE;

		double temp = matrix[row][col];
		matrix[row][col] = matrix[i1][j1];
		matrix[i1][j1] = temp;
	}

}

 

8.4 2차원 배열을 함수에 전달

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

#include <iostream>
using namespace std;

const int COL_SIZE = 4; //열크기를 전역변수로 선언

int sum(const int a[][COL_SIZE], int rowSize) //모든 요소들의 합을 구하는 함수
{
	int total = 0;
	for (int row = 0; row < rowSize; row++)
	{
		for (int col = 0; col < COL_SIZE; col++)
			total += a[row][col];
	}
	return total;
}

int main()
{
	const int ROW_SIZE = 3;
	int m[ROW_SIZE][COL_SIZE];
	cout << "Enter " << ROW_SIZE << " rows and " << COL_SIZE << " columns: " << endl;

	for (int i = 0; i < ROW_SIZE; i++)
	{
		for (int j = 0; j < COL_SIZE; j++)
			cin >> m[i][j];
	}

	cout << "\nSum of all elements is " << sum(m, ROW_SIZE) << endl;

	return 0;
}
/*
Enter 3 rows and 4 columns:
1 2 3 4
5 6 7 8
9 10 11 12

Sum of all elements is 78

*/