목차
■ 이해점검 풀이
■ 프로그램 문제 풀이
■ 프로그램 문제 풀이
본문내용
두 이차원 배열의 더하기와 빼기를 수행하는 함수를 만들어, 다음 배열의 연산 결과를 알아보시오.
#include
#define rows 2
#define cols 3
typedef double matrixa[rows][cols];
typedef double matrixb[rows][cols];
typedef double resultc[rows][cols];
void plus(resultc r, matrixa a, matrixb b);
void minus(resultc r, matrixa a, matrixb b);
void display(resultc r, int m);
int main(void)
{
matrixa a = {{4.2, 4.3, 3.8}, {3.7, 1.5, 0.7}};
matrixb b = {{5.2, 2.1, 1.8}, {3.2, 1.4, 2.9}};
resultc r = {0};
display(a, rows);
display(b, rows);
printf("다음은 위 두 행렬의 더하기 결과입니다.\n\n");
plus(r, a, b);
display(r, rows);
printf("다음은 위 두 행렬의 빼기 결과입니다.\n\n");
minus(r, a, b);
display(r, rows);
return 0;
}
void plus(resultc r, matrixa a, matrixb b) {
int i, j, k;
for(i=0; i < rows ; i++) {
for(j=0; j < cols ; j++) {
//r[i][j] = a[i][j] + b[k][j];
*(r[i]+j) = *(a[i]+j) + *(b[i]+j);
}
}
}
void minus(resultc r, matrixa a, matrixb b) {
int i, j, k;
for(i=0 ; i< rows ; i++) {
for(j=0 ; j < cols ; j++) {
//r[i][j] = a[i][j] + b[k][j];
*(r[i]+j) = *(a[i]+j) *(b[i]+j);
}
}
}
void display(resultc r, int m) {
int i, j;
for(i=0; i < m; i++) {
for(j=0; j < cols; j++) {
printf("%8.2f",r[i][j]);
}
printf("\n");
}
printf("\n");
}
5. 프로그램 예제의 두 행렬의 곱 프로그램을 수정하여, 다음 곱을 수행하도록 프로그램을 작성하시오.
#include
#define rows 2
#define cols 3
typedef double matrixa[rows][cols];
typedef double matrixb[cols][rows];
typedef double resultc[rows][rows];
void multiply(resultc r, matrixa a, matrixb b);
void display(resultc r, int m);
int main(void)
{
matrixa a = {{4.2, 4.3, 3.8}, {3.7, 1.5, 0.7}};
matrixb b = {{5.2, 2.1}, {3.2, 1.4}, {1.5, 3.6}};
resultc r = {0};
int i, j;
for(i=0 ; i < rows; i++) {
for(j=0 ; j < cols; j++) {
printf("%8.2f", a[i][j]);
}
printf("\n");
}
printf("\n");
display(b, cols);
printf("다음은 위 두 행렬의 곱 결과입니다.\n\n");
multiply(r, a, b);
display(r, rows);
return 0;
}
void multiply(resultc r, matrixa a, matrixb b) {
int i, j, k;
for(i=0; i < rows ; i++) {
for(j=0; j < rows ; j++) {
for(k=0; k < cols; k++) {
//r[i][j] += a[i][k] * b[k][j];
*(r[i] + j) += *(a[i] + k)* *(b[k] + j);
}
}
}
}
void display(resultc r , int m) {
int i, j;
for(i=0; i
for(j=0; j
printf("%8.2f", r[i][j]);
}
printf("\n");
}
printf("\n");
}
#include
#define rows 2
#define cols 3
typedef double matrixa[rows][cols];
typedef double matrixb[rows][cols];
typedef double resultc[rows][cols];
void plus(resultc r, matrixa a, matrixb b);
void minus(resultc r, matrixa a, matrixb b);
void display(resultc r, int m);
int main(void)
{
matrixa a = {{4.2, 4.3, 3.8}, {3.7, 1.5, 0.7}};
matrixb b = {{5.2, 2.1, 1.8}, {3.2, 1.4, 2.9}};
resultc r = {0};
display(a, rows);
display(b, rows);
printf("다음은 위 두 행렬의 더하기 결과입니다.\n\n");
plus(r, a, b);
display(r, rows);
printf("다음은 위 두 행렬의 빼기 결과입니다.\n\n");
minus(r, a, b);
display(r, rows);
return 0;
}
void plus(resultc r, matrixa a, matrixb b) {
int i, j, k;
for(i=0; i < rows ; i++) {
for(j=0; j < cols ; j++) {
//r[i][j] = a[i][j] + b[k][j];
*(r[i]+j) = *(a[i]+j) + *(b[i]+j);
}
}
}
void minus(resultc r, matrixa a, matrixb b) {
int i, j, k;
for(i=0 ; i< rows ; i++) {
for(j=0 ; j < cols ; j++) {
//r[i][j] = a[i][j] + b[k][j];
*(r[i]+j) = *(a[i]+j) *(b[i]+j);
}
}
}
void display(resultc r, int m) {
int i, j;
for(i=0; i < m; i++) {
for(j=0; j < cols; j++) {
printf("%8.2f",r[i][j]);
}
printf("\n");
}
printf("\n");
}
5. 프로그램 예제의 두 행렬의 곱 프로그램을 수정하여, 다음 곱을 수행하도록 프로그램을 작성하시오.
#include
#define rows 2
#define cols 3
typedef double matrixa[rows][cols];
typedef double matrixb[cols][rows];
typedef double resultc[rows][rows];
void multiply(resultc r, matrixa a, matrixb b);
void display(resultc r, int m);
int main(void)
{
matrixa a = {{4.2, 4.3, 3.8}, {3.7, 1.5, 0.7}};
matrixb b = {{5.2, 2.1}, {3.2, 1.4}, {1.5, 3.6}};
resultc r = {0};
int i, j;
for(i=0 ; i < rows; i++) {
for(j=0 ; j < cols; j++) {
printf("%8.2f", a[i][j]);
}
printf("\n");
}
printf("\n");
display(b, cols);
printf("다음은 위 두 행렬의 곱 결과입니다.\n\n");
multiply(r, a, b);
display(r, rows);
return 0;
}
void multiply(resultc r, matrixa a, matrixb b) {
int i, j, k;
for(i=0; i < rows ; i++) {
for(j=0; j < rows ; j++) {
for(k=0; k < cols; k++) {
//r[i][j] += a[i][k] * b[k][j];
*(r[i] + j) += *(a[i] + k)* *(b[k] + j);
}
}
}
}
void display(resultc r , int m) {
int i, j;
for(i=0; i
}
printf("\n");
}
printf("\n");
}
키워드
추천자료
C언어를 이용해 입력받은 두정수의 합과 차를 구하는 프로그램
C언어를 이용해 년과월을 입력받아 그달의 말일을 출력하는 프로그램
C언어를 이용해 배열의 값을 내림차순 정렬하는 프로그램
C언어를 이용해 가위바위보를 하는 프로그램
c언어를 이용해 함수로 구현한 계산기 프로그램(switch문)
C언어를 이용한 피보나츠 수열 프로그램
C언어를 이용한 팩토리얼 함수(n!) 프로그램
C언어를 이용해 두 정수를 입력 받아 앞의 정수가 크면 두 수를 더하고, 뒤의 정수가 크거나 ...
C언어를 이용해 배열의 값을 N씩 증가시키는 프로그램
C언어를 이용해 두 문자의 크기를 비교하는 프로그램
C언어를 이용해 10진수를 입력시 2진수로 변환하는 프로그램
c언어를 이용한 출석부 관리 프로그램
C언어를 이용한 최소공배수와 최대공약수를 동시에 구하는 프로그램
C언어를 이용한 암호화 프로그램
소개글