행렬식 구하는 코드입니다. 도와주세요!~~~
코이
질문 제목 :
1차원 배열을 이용해서 행렬을 구현하고,
소행렬과 여인수를 이용해서 행렬식을 구하는 문제입니다.
재귀적으로 구현하라고 해서 나름대로 구현을 했는데, 답이 계속 틀리게 나오네요.
도와주세요ㅠㅠㅠㅠㅠㅠㅠ
#include stdio.h
#include stdlib.h
#define mtype double // 행렬의 크기는 그대로 사용하되, 행과 열은 실제보다 1 작은 값을 사용한다.
mtype* getnewmatrix(int size); // 표준입력으로 행렬 얻기
void printmatrix(mtype *mat, int size); // 행렬 표준출력하기
mtype cofactor(mtype *mat, int size, int i, int j); // i행 j열에 대한 여인수
mtype determinant(mtype *mat, int size); // 행렬식
mtype* minormat(mtype *majormat, int size, int row, int col); // i행 j열에 대한 소행렬 구하기
void main()
{
int size;
mtype *mymat;
// 새로운 행렬 생성
printf(** 몇 차 행렬? : );
scanf(%d, &size);
printf(** %d x %d 행렬을 입력 , size, size);
printf((%d개의 값을 빈칸(또는 엔터)으로 구분하여 입력.)\n, size*size);
mymat = getnewmatrix(size);
printmatrix(mymat, size);
//행렬식
printf(\ndet(mymat) is %5.2f\n, determinant(mymat, size));
getchar();
}
mtype* getnewmatrix(int size)
{
int i;
mtype* mat = (mtype *)malloc(sizeof(mtype) * size * size);
for(i = 0; i size * size; i++)
{
scanf(%lf, &mat[i]);
}
fflush(stdin);
return mat;
}
void printmatrix(mtype *mat, int size)
{
int i;
for(i = 0; i size*size; i++)
{
printf(%+6.2f , mat[i]);
if(i % size == size - 1)
printf(\n);
}
}
mtype* minormat(mtype *majormat, int size, int row, int col)
{
int i,j;
int mindex=0;
int msize=size-1;
mtype *minormat = (mtype *)malloc(sizeof(mtype) * msize * msize);
if(size==1)
return majormat;
else
{
for(i=0;isize;i++)
{
for(j=0;jsize;j++)
{
if(i!=row && j!=col)
{
minormat[mindex]=majormat[i*size+j];
mindex++;
}
}
}
}
return minormat;
}
mtype determinant(mtype *mat, int size)
{
mtype det = 0;
int i;
if(size==1)
return mat[0];
else
{
for(i=0;isize;i++)
det+=mat[i]*cofactor(mat,size-1,0,i);
}
return det;
}
mtype cofactor(mtype *mat, int size, int i, int j)
{
mtype cf;
int a,k;
if(i+j%2==0)
a=1;
else
a=-1;
if(size==1)
return mat[0];
else
{
for(k=0;ksize;k++)
cf+=a*determinant(minormat(mat, size, 0, k), size-1);
}
return cf;
}
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2685477 | 소수점이하 출력하는거요 | 널위해 | 2025-02-18 |
2685449 | printf배우고있는데요 궁금한게있어요 (8) | 꽃큰 | 2025-02-18 |
2685393 | c언어 질문이요.... | 붕붕 | 2025-02-18 |
2685365 | 윈도우7에서 visual studio c++6.0 (1) | 빛길 | 2025-02-17 |
2685317 | segmentation fault가 나요 | 가람 | 2025-02-17 |
2685292 | 다중스택질문 | 올해1살 | 2025-02-17 |
2685231 | C언어와 닷넷에 대해서 질문!! (2) | 설아 | 2025-02-16 |
2685206 | VisualBasic 과 DEV++ 의 장단점과 만든 파일 호환 가능하나요? | 에드워드 | 2025-02-16 |
2685154 | 배열 크기조절 | 해찬솔 | 2025-02-15 |
2685124 | 수정이거 제가 뭐가 잘못한거죠 ? | 아이돌 | 2025-02-15 |
2685096 | 포인터의 고수분들 모두 보세요!! 제발 ㅠ_ㅠ 헷갈려헷갈려..갈려헷.. (7) | 치킨마루 | 2025-02-15 |
2685045 | 전처리기 질문 | 치에미 | 2025-02-14 |
2685016 | 오류 좀 확인해 주시면 감사하겠습니다 | 초코맛사탕 | 2025-02-14 |
2684917 | 알고리즘을 이용한 행렬의 전치// 문제다운 문제네요. | 뿡뿡 | 2025-02-13 |
2684868 | 양방향 연결리스트에서 실행도중 죽는 이유좀 찾아주세요. (2) | 예다움 | 2025-02-13 |
2684844 | 시계프로그램인데 도저히 모르겠어요ㅠ (1) | 비내리던날 | 2025-02-12 |
2684812 | 레포트좀도와주세요ㅠㅠ (2) | 갈매빛 | 2025-02-12 |
2684780 | 채팅창 흉내내보려고하는데요 ㅜ.ㅜ (1) | 바름 | 2025-02-12 |
2684729 | 내일 시험인데 메모리 그리는것좀 도와주세요 ㅠ (1) | 상처주지마 | 2025-02-11 |
2684701 | 버퍼 관련 질문 3가지 (이전거랑 달라요) | 한국녀 | 2025-02-11 |