행렬식 구하는 코드입니다. 도와주세요!~~~
코이
질문 제목 :
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;
}
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2694420 | C언어 질문할게요(유니코드,자료형,버퍼,캐스트연산자) | 은새 | 2025-05-11 |
2694370 | 내일까진데 함수호출 제발 도와주세요!!!!!!!!!11 | 들찬 | 2025-05-10 |
2694339 | putchar()의 괄호 안에 int c=10;로 전에 선언된 c를 넣으면 안되는 이유에서 제가 생각한 것이 그 이유가 되는지 확인하고 싶습니다. (3) | 미르 | 2025-05-10 |
2694316 | 이 코드 어디가 잘못되었는지 고수분들 ㅠㅠ (2) | 나빛 | 2025-05-10 |
2694285 | 언어 공부하는 과정 좀 추천해주세요! (1) | 아빠몬 | 2025-05-09 |
2694258 | 카운터.. 질문입니다. (4) | 하늘빛눈망울 | 2025-05-09 |
2694229 | 단순한 질문이요 (8) | 여름 | 2025-05-09 |
2694202 | 용돈을 가지고 할 수 있는 일을 여러가지로 출력하는 방법 좀 알려주세요! (2) | 미나 | 2025-05-09 |
2694145 | 화면깜빡임을 없애고 싶은데요... (1) | 어서와 | 2025-05-08 |
2694069 | unsigned 질문입니다. | 힘차 | 2025-05-07 |
2694012 | 전공 비전공자 개발자 (10) | 말글 | 2025-05-07 |
2693984 | 오버로딩이 무엇인가요? (2) | 헛매질 | 2025-05-07 |
2693956 | PlaySound재생이 안됩니다!(C에 음악넣기) | 지존 | 2025-05-06 |
2693928 | &와 *의 사용에 관한 명확한 이해 | 제나 | 2025-05-06 |
2693903 | 반복문 설명좀요 ㅠㅠ (2) | 란새 | 2025-05-06 |
2693869 | stdio.h 는 왜 쓰는건가요? (1) | 큰꽃들 | 2025-05-06 |
2693842 | 포인터 변수의 주소값끼리 더하는 것에 대해서 질문드립니다. (1) | 진솔 | 2025-05-05 |
2693811 | 소수 출력;;;; | 화이트캣 | 2025-05-05 |
2693788 | 이런 함수는 없나요? (3) | 앤드류 | 2025-05-05 |
2693758 | txt파일 불러와서 행렬로 저장 | 큰애 | 2025-05-05 |