알고리즘/BaekJoon 단계별로 풀어보기

[2차원 배열] 2738번 행렬 덧셈(c++)

jylee3 2024. 11. 1. 15:36

https://www.acmicpc.net/problem/2738

 

소스 코드 (c++)

#include <iostream>
using namespace std;

int main() {
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

	int n, m;
	int arr[101][101];
	int result_arr[101][101] = {{0,},};
	cin >> n >> m;
	for (int i = 0; i < 2; i++) {
		for (int y = 0; y < n; y++) {
			for (int x = 0; x < m; x++)
			{
				cin >> arr[y][x];
				result_arr[y][x] += arr[y][x];
			}
		}
	}
	for (int y = 0; y < n; y++) {
		for (int x = 0; x < m; x++)
		{
			cout << result_arr[y][x] << " ";
		}
		cout << endl;
	}
}

 

알게된 점

// 1. 2차원 배열 초기화
int arr[3][3] = {0};

// 2. 2차원 배열 초기화
int arr[3][3] = {{0,}, };

// 3. 2차원 배열 초기화 - #include <cstring>
int arr[3][3];
memset(arr, 0, sizeof(arr));

알고는 있는 내용이나 약간 아리까리 했던 지점이 있었는데 다시한번 확인하게 되었다.