满意答案
#define N 4
#define M 5
#include
#include
#include
typedef int ElemType;
typedef struct triplist
{
int row,col;
ElemType val;
}tl;
tl a[N*M];
void print_array(ElemType b[][M+1])
{
int i,j;
printf("\n");
for(i=1;i<=N;i++)
{
for(j=1;j<=M;j++)
printf("%5d",b[i][j]);
printf("\n");
}
}
void create_triplist(ElemType b[N][M+1],tl a[ ])
{
int i,j;
int t=1;
for(i=1;i<=N;i++)
for(j=1;j<=M;j++)
if(b[i][j])
{
a[t].row=i;
a[t].col=j;
a[t].val=b[i][j];
t++;
}
a[0].row=N;
a[0].col=M;
a[0].val=t-1;
for(i=1;i<=N*M;i++)
printf("%d\t%d\t%d\n",a[i].row,a[i].col,a[i].val);
}
int main()
{
ElemType b[N+1][M+1]; //这里我把b[N][M+1] 的N后边加一
int e,f;
srand((unsigned)time(NULL));
for(e=1;e<=N;e++)
for(f=1;f<=M;f++)
b[e][f]=rand()/10000;
print_array(b);
create_triplist(b,a);
return 0;
}
看看能不能运行,因为我运行出来的和这个结果都一样,只是有内存问题,加一之后就没有这个提示了
00分享举报