4 x 3인 행렬 A, B를 입력 받아 행렬합을 구하여 출력하는 프로그램을 작성하시오.
• A와 B의 행렬원소의 값을 입력 받는다.
• C에 행렬합을 저장하여 이를 출력한다
• 입력/처리/출력 부분을 각각 함수화 하라
– void readMatrix(int a[][3], int size)
– void matrixAdd(int a[][3], int b[][3], int c[][3], int size)
– void printMatrix(int a[][3], int size)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
void readMatrix(int a[][3], int size)
{
for (int i=0; i < size; i++)
for (int j=0; j < 3; j++)
scanf("%d", &a[i][j]);
}
void matrixAdd(int a[][3], int b[][3], int c[][3], int size)
{
for (int i=0; i < size; i++)
for (int j=0; j < 3; j++)
c[i][j] = a[i][j] + b[i][j];
}
void printMatrix(int a[][3], int size)
{
for (int i=0; i < size; i++) {
for (int j=0; j < 3; j++)
printf("%5d", a[i][j]);
printf("\n");
}
}
int main(void)
{
int A[4][3], B[4][3], C[4][3];
printf("(4x3) 행렬 A 입력:");
readMatrix(A,4);
printf("(4x3) 행렬 B 입력:");
readMatrix(B,4);
matrixAdd(A,B,C,4);
printf("행렬합:\n");
printMatrix(C, 4);
printf("\n");
return 0;
}
|
cs |