전치행렬 값이 이상해요..ㅠㅠ; 뭐가 문제인지를.. 해결해주세요 젭알..
들꿈
질문 제목 : 전치 행렬 값이 이상함..질문처럼 값이 이상하게 나오네요..질문 내용 : #include stdio.h
#include stdlib.h
#define max_terms 101
#define max_row 50
#define max_col 50
typedef struct{
int row;
int col;
int value;
}term;
term a[max_terms], b[max_terms], d[max_terms];
int cnt;
int compare(int a_col, int b_col)
{
if(a_col b_col)
return -1;
if(a_col == b_col)
return -0;
if(a_col b_col)
return 1;
}
void storesum(term d[], int *totald, int row , int column, int *sum)
{
if(*sum)
{
if (*totald max_terms)
{
d[++*totald],row = row;
d[*totald].col =column;
d[*totald].value = *sum;
*sum=0;
}
else{
printf(행렬의 최대치 %d을 넘었습니다.\n,max_terms);
exit(1);
}
}
}
void fast_tranaspose(term a[], term b[])
{
int row_terms[max_col], starting_pos[max_col];
int i, j, num_cols = a[0].col, num_terms = a[0].value;
b[0].row = num_cols;
b[0].col = a[0].row;
b[0].value = num_terms;
if(num_terms 0)
{
for(i = 0; inum_cols; i++)
row_terms[i]=0;
for(i = 1; i=num_cols; i++)
row_terms[a[i].col]++;
starting_pos[0] = 1;
for(i = 1 ; inum_cols;i++)
starting_pos[i] = starting_pos[i-1] + row_terms[i-1];
for(i = 1; i=num_terms; i++)
{
j = starting_pos[a[i].col]++;
b[j].row = a[i].col;
b[j].col = a[i].row;
b[j].value = a[i].value;
}
}
}
int set_matrix_element(int max_row, int max_col, int value_number, term t[])
{
int i, j, value;
int cnt = 0;
printf(원소를 입력하시오.(행 열 값)\nex2(행) 1(열) 3(값)\n);
t[0].row = max_row;
t[0].col = max_col;
while(cntvalue_number)
{
printf(%d번째 입력입니다.\n입력 하시오. : , cnt+1);
scanf(%d %d %d, &i, &j, &value);
if(i max_row - 1 || j max_col -1)
{
printf(최대 범위를 넘었습니다. 다시 입력하시오.\n);
continue;
}
t[cnt+1].col = j;
t[cnt+1].row = i;
t[cnt+1].value = value;
cnt++;
}
t[0].value = cnt;
return cnt;
}
void mmult(term a[], term b[], term d[])
{
int i, j, column;
int totalb = b[0].value, totald = 0;
int rows_a = a[0].row, cols_a = a[0].col, totala = a[0].value;
int cols_b = b[0].col, row_begin = 1, row = a[1].row, sum = 0;
term new_b[max_terms];
if(cols_a != b[0].row)
{
printf(incompatible matrices\n);
exit(1);
}
fast_tranaspose(b, new_b);
a[totala + 1].row = rows_a;
new_b[totalb+1].row = cols_b;
new_b[totalb+1].col = 0;
new_b[totalb+1].value = 0;
for(i = 1; i=totala;)
{
column = new_b[1].row;
for(j = 1; j=totalb+1;)
{
if(a[i].row != row)
{
storesum(d, &totald, row, column, &sum);
i = row_begin;
for(;new_b[j].row == column; j++)
;
column = new_b[j].row;
}
else if(new_b[j].row != column)
{
storesum(d, &totald, row, column, &sum);
i = row_begin;
column = new_b[j].row;
}
else switch(compare(a[i].col, new_b[j].col))
{
case -1 :
i++;
break;
case 0 :
sum += (a[i++].value * new_b[j++].value);
break;
case 1 :
j++;
}
}
for(;a[i].row == row ; i++)
;
row_begin = i; row = a[i].row;
}
d[0].row = rows_a;
d[0].col = cols_b; d[0].value = totald;
}void main(void)
{
int max_row, max_col, value_number, max_row1,max_col1, value_number1;
int cnt1, cnt2;
int i;
printf(희소행렬 전치 프로그램\n);
printf(a행렬의 행렬과 원소개수를 입력하시오.(행렬의 수 열의 수 원소의 개수) \nex3(행) 2(열) 4(원소수)\n);
printf(입력하시오 : );
scanf(%d %d %d, &max_row, &max_col, &valuamp;value_number);
cnt1= set_matrix_element(max_row, max_col, value_number, a);
//fast_tranaspose(a,b);
printf(b 행렬과 원소개수를 입력하시오. (행의 수 열의 수 원소 개수)\n ex3(행) 2(열) 4(원소수) \n);
printf(입력하시오 : );
scanf(%d %d %d, &max_row1, &max_col1, &value_number1);
cnt2= set_matrix_element(max_row1, max_col1, value_number1, b);
mmult (a,b,d);
/*출력부 */
printf(a행렬\n);
printf(row\tcol\tvalue\n);
for(i=0; i=cnt1;i++)
printf(%3d\t%3d\t%5d\n,a[i].row, a[i].col, a[i].value);
printf(b행렬\n);
printf(row\tcol\tvalue\n);
for(i=0;i=cnt2;i++)
printf(%3d\t%3d\t%5d\n, b[i].row,b[i].col,b[i].value);
printf(곱셈결과\n);
printf(row\tcol\tvalute\n);
for(i=0; d[i].value !=null ; i++)
printf(%3d\t%3d\t%5d\n,d[i].row,d[i].col, d[i].value);
}
밑에처럼 나와야 하는데..입력도 밑에처럼 해서 해야하는데 답이이상하게나와요 ㅠㅠ
| 번호 | 제 목 | 글쓴이 | 날짜 |
|---|---|---|---|
| 2752085 | 문자열을 랜덤하게 출력해서 대체,삽입,삭제 횟수를 각가 표시하고 그걸 표 형태로 출력 | 슬아s | 2026-10-20 |
| 2752056 | 포인터 && 배열 질문요~ (2) | 해찬나래 | 2026-10-20 |
| 2752031 | x^n 구하는 코드 짜봤는데 값이 틀려요 ㅠㅠ | 세실 | 2026-10-20 |
| 2752000 | 나 정말 미치겠어요..ㅠㅜ (4) | 다이 | 2026-10-20 |
| 2751978 | 문자열 질문이요.. | 초시계 | 2026-10-19 |
| 2751945 | 숫자 맞추는 프로그램이요 ㅠㅠ (1) | 동은 | 2026-10-19 |
| 2751917 | C언어 이중 연결 관련 질문합니다. | 유우 | 2026-10-19 |
| 2751888 | return의 정확한 의미좀 알려주세요 (3) | 올해1살 | 2026-10-19 |
| 2751857 | 포인터 배열 (2) | 터큰 | 2026-10-18 |
| 2751831 | 연산자? 질문입니다 (2) | 진주 | 2026-10-18 |
| 2751800 | C언어 기초질문입니다.. (3) | 핫블랙 | 2026-10-18 |
| 2751739 | 파일 출력을 구조체로 받을 때, 파일의 끝을 판별하는 방법을 알려주세요. (2) | 얼 | 2026-10-17 |
| 2751715 | unresolved external symbol 에러... | 이룩 | 2026-10-17 |
| 2751686 | 변수 설정에 관한 질문입니다. | 꽃달 | 2026-10-17 |
| 2751655 | 자료구조 연습문제 | 한무릎 | 2026-10-17 |
| 2751631 | gets 함수 질문이요. (4) | 해늘 | 2026-10-16 |
| 2751553 | 퀵정렬 소스인데 주석좀 달아주세요 (1) | 싴흐한세여니 | 2026-10-16 |
| 2751528 | 저기 이거하는법좀 질문.. (2) | 가람슬기 | 2026-10-15 |
| 2751445 | 도저히 풀 수가 없네요.... (2) | 신당 | 2026-10-15 |
| 2751412 | 어제부터 계속 해도.........안되는.... (1) | 족장 | 2026-10-14 |
