개요
2차원 배열을 입력하고 행과 열의 합을 각각 구하여 출력하여라. |
목표
[0][0] = 1 [0][1] = 2 [0][2] = 3 [1][0] = 4 [1][1] = 5 [1][2] = 6 [2][0] = 7 [2][1] = 8 [2][2] = 9 [3][0] = 10 [3][1] = 11 [3][2] = 12 1 2 3 = 6 4 5 6 = 15 7 8 9 = 24 10 11 12 = 33 22 26 30 |
Code
#include<stdio.h>
int main(void)
{
int arr1[4][3] = { 0 };
int sumRow[4] = { 0 };
int sumCol[3] = { 0 };
//2D Arrary 입력
for (int row = 0; row < 4; row++)
{
for (int col = 0; col < 3; col++)
{
printf("[%d][%d] = ", row, col);
scanf_s("%d", &arr1[row][col]);
}
}
for (int row = 0; row < 4; row++)
{
for (int col = 0; col < 3; col++)
{
//Array 출력
printf("%3d\t",arr1[row][col]);
//row 합계
sumRow[row] += arr1[row][col];
}
//row 합계 출력
printf("= %3d", sumRow[row]);
printf("\n");
}
//col 합계 계산
for (int col = 0; col < 3; col++)
{
for (int row = 0; row < 4; row++)
{
sumCol[col] += arr1[row][col];
}
//col 합계 출력
printf("%3d\t", sumCol[col]);
}
return 0;
}
Output
'C\C++' 카테고리의 다른 글
Pointer & Array Ex01 (0) | 2022.02.17 |
---|---|
Array & Pointer 예제 (0) | 2022.02.16 |
string ex01 (0) | 2022.02.14 |
do_while 연습 (0) | 2022.02.12 |
For문 중첩 (0) | 2022.02.10 |