Created
October 17, 2016 01:48
-
-
Save sukso96100/83858515835324281474ff6474c910db to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
// 배열 내에 있는 모든 요소의 합을 반환하는 함수 | |
int sum1(int (*p)[4]); | |
int sum2(int p[4][2]); | |
int main(void){ | |
int i, j; // 반복문에 사용할 변수 | |
// 행열 A : 2행 4열 | |
int a[2][4] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8} }; | |
// 행열 B(A의 전치행열) : 4행 2열 | |
int b[4][2]; | |
// 2차원 배열에 대한 포인터 변수 | |
// a 와 호환되는 포인터 | |
int(*pa)[4] = a; | |
// b 와 호환되는 포인터 | |
int(*pb)[2] = b; | |
// 행열 A 출력(반복문 이용) | |
printf("Matrix A : \n"); | |
for (i = 0; i < 2; i++){ | |
for (j = 0; j < 4; j++){ | |
// printf("%d, ", pa[i][j]); | |
printf("%d, ", *(*(pa+i)+j)); | |
} | |
printf("\n"); | |
} | |
// 행열 A 에 대한 전치행열을 구해 행열 B로. | |
// (반복문 이용) | |
/* | |
for (i = 0; i < 2; i++){ | |
for (j = 0; j < 4; j++){ | |
b[j][i] = a[i][j]; | |
} | |
}*/ | |
// 행열 B 출력(반복문 이용) | |
printf("Matrix B \n"); | |
for (i = 0; i < 4; i++){ | |
for (j = 0; j < 2; j++){ | |
*(*(pb+i)+j) = *(*(pa+j)+i); | |
printf("%d, ", *(*(pb+i)+j)); | |
} | |
printf("\n"); | |
} | |
// a 요소의 합 출력 | |
printf("a 요소의 합 : %d\n", sum1(a)); | |
printf("b 요소의 합 : %d\n", sum1(b)); | |
return 0; | |
} | |
int sum1(int (*p)[4]){ | |
int i, j, sum=0; | |
for(i = 0; i < 2; i++){ | |
for(j = 0; j < 4; j++){ | |
sum += p[i][j]; | |
} | |
} | |
return sum; | |
} | |
int sum2(int p[4][2]){ | |
int i, j, sum=0; | |
for(i = 0; i < 4; i++){ | |
for(j = 0; j < 2; j++){ | |
sum += p[i][j]; | |
} | |
} | |
return sum; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment