主题:[讨论]稀疏矩阵转置
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 1000
typedef struct
{
int row, col;
int e;
}Triple;
typedef struct
{
Triple data[MAXSIZE+1];
int m,n,len;
}TSMatrix;
void FastTransposeTSMatrix(TSMatrix A,TSMatrix *B);
void main()
{
int i,j,k;
TSMatrix A,B;
A.m = 6;
A.n = 7;
A.len = 8;
int a[6][7] = {
{0,2,9,0,0,0,0},
{0,0,0,0,0,0,0},
{3,0,0,0,0,4,0},
{0,0,5,0,0,0,0},
{0,8,0,0,0,0,0},
{1,0,0,7,0,0,0}};
printf("A矩阵是:\n");
for(i = 0;i < 6;i++)
{
for(j = 0;j < 7;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("\n");
printf("A矩阵对应的三元组是:\n");
int b = 0;
for(i = 1;i <= 6;i++)
{
for(j = 1;j <= 7;j++)
{
if(a[i-1][j-1] != 0)
{
A.data[b].row = i;
A.data[b].col = j;
A.data[b].e = a[i-1][j-1];
printf("%d %d %d",A.data[b].row,A.data[b].col,A.data[b].e);
printf("\n");
}
}
b++;
}
A.m = 6;
A.n = 7;
A.len = b - 1;
FastTransposeTSMatrix(A,&B);
for(k = 1;k <= B.len;k++)
{
printf("%d %d %d",B.data[k].row,B.data[k].col,B.data[k].e);
printf("\n");
}
}
void FastTransposeTSMatrix(TSMatrix A,TSMatrix *B)
{
int col,p,q;
int t = 0;
int num[MAXSIZE],position[MAXSIZE];
B->len=A.len;B->n=A.m;B->m=A.n;
if(B->len)
{
for(col = 1;col <= A.n;col++)
num[col]=0;
for(t = 1;t <= A.len;t++)
num[A.data[t].col]++;
position[1]=1;
for(col = 2;col <= A.n;col++)
position[col]=position[col-1]+num[col-1];
for(p = 1;p <= A.len;p++)
{
col=A.data[p].col;q=position[col];
B->data[q].row=A.data[p].col;
B->data[q].col=A.data[p].row;
B->data[q].e=A.data[p].e;
position[col]++;
}
}
}
我打印不出来转置后B的3元组,麻烦大家帮忙看下哪里错了
谢谢了
#include <stdlib.h>
#define MAXSIZE 1000
typedef struct
{
int row, col;
int e;
}Triple;
typedef struct
{
Triple data[MAXSIZE+1];
int m,n,len;
}TSMatrix;
void FastTransposeTSMatrix(TSMatrix A,TSMatrix *B);
void main()
{
int i,j,k;
TSMatrix A,B;
A.m = 6;
A.n = 7;
A.len = 8;
int a[6][7] = {
{0,2,9,0,0,0,0},
{0,0,0,0,0,0,0},
{3,0,0,0,0,4,0},
{0,0,5,0,0,0,0},
{0,8,0,0,0,0,0},
{1,0,0,7,0,0,0}};
printf("A矩阵是:\n");
for(i = 0;i < 6;i++)
{
for(j = 0;j < 7;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("\n");
printf("A矩阵对应的三元组是:\n");
int b = 0;
for(i = 1;i <= 6;i++)
{
for(j = 1;j <= 7;j++)
{
if(a[i-1][j-1] != 0)
{
A.data[b].row = i;
A.data[b].col = j;
A.data[b].e = a[i-1][j-1];
printf("%d %d %d",A.data[b].row,A.data[b].col,A.data[b].e);
printf("\n");
}
}
b++;
}
A.m = 6;
A.n = 7;
A.len = b - 1;
FastTransposeTSMatrix(A,&B);
for(k = 1;k <= B.len;k++)
{
printf("%d %d %d",B.data[k].row,B.data[k].col,B.data[k].e);
printf("\n");
}
}
void FastTransposeTSMatrix(TSMatrix A,TSMatrix *B)
{
int col,p,q;
int t = 0;
int num[MAXSIZE],position[MAXSIZE];
B->len=A.len;B->n=A.m;B->m=A.n;
if(B->len)
{
for(col = 1;col <= A.n;col++)
num[col]=0;
for(t = 1;t <= A.len;t++)
num[A.data[t].col]++;
position[1]=1;
for(col = 2;col <= A.n;col++)
position[col]=position[col-1]+num[col-1];
for(p = 1;p <= A.len;p++)
{
col=A.data[p].col;q=position[col];
B->data[q].row=A.data[p].col;
B->data[q].col=A.data[p].row;
B->data[q].e=A.data[p].e;
position[col]++;
}
}
}
我打印不出来转置后B的3元组,麻烦大家帮忙看下哪里错了
谢谢了