#include <iostream.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <cstdlib>
#include <string.h>
#define maxsize 100
#define m 2
#define n 3
typedef int elemtype;
typedef struct{
int i,j;
elemtype e;
}Triple;
typedef struct{
Triple date[maxsize+1];
int mu,nu,tu;
}TSMatrix;
void change(TSMatrix &t,elemtype a[m][n]){
int k=1;
t.mu=m;
t.nu=n;
t.tu=0;
for(int e=0;e<m;e++){
for(int s=0;s<n;s++)
if(a[e][s]!=0)
{
t.date[k].i=e;
t.date[k].j=s;
t.date[k].e=a[e][s];
t.tu++;
k++;
}
}
}
void DipMat(TSMatrix t){
cout<<"行号"<<" "<<"列号"<<" "<<"值\n";
for(int k=0;k<t.tu;k++){
cout<<t.date[k].i+1<<" "<<t.date[k].j+1<<" "<<t.date[k].e<<"\n";
}
}
void DipMatjz(TSMatrix t){
int x=1,a[n][m],s,y;
for(y=0;y<t.mu;y++)
for(s=0;s<t.nu;s++)
if(t.date[x].i==y&&t.date[x].j==s)
a[y][s]=t.date[x].e;
else
a[y][s]=0;
for(y=0;y<t.mu;y++){
for(s=0;s<t.nu;s++)
cout<<a[y][s]<<" ";
cout<<"\n";
}
}
void z(TSMatrix m,TSMatrix &T){
int num[n+1],cpot[maxsize],col=1,x;
T.mu=m.nu;
T.nu=m.mu;
T.tu=m.tu;
if(T.tu){
for(col=1;col<=m.nu;++col)
num[col]=0;
for(x=1;T<=m.tu;T++)
++num[m.date[x].j];
cpot[1]=1;
for(col=2;col<=m.nu;++col)
cpot[col]=cpot[col-1]+num[col-1];
for(x=1;x<=t.tu;x++){
col=m.date[x].j;
y=copt[col];
T.date[y].i=m.date[x].j;
T.date[y].i=m.date[x].j;
T.date[y].e=m.date[x].e;
copt[col]++;
}
}
}
void main(){
cout<<"请输入"<<m<<"行"<<n<<"列矩阵:\n";
elemtype a[m][n];TSMatrix t,p;
for(int e=0;e<m;e++)
for(int s=0;s<n;s++)
cin>>a[e][s];
change(t,a);
DipMat(t);
z(t,p);
cout<<"转置后矩阵为:\n";
DipMatjz(p);
}
为什么z函数出错,变量欠缺;
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
1条回答 默认 最新
ShowMeAI 2022-12-11 23:06关注详细解答如下,望采纳
这段代码中的z函数缺少一个重要的变量:行数。可以在z函数的参数列表中添加一个行数变量,并在函数内部使用它。例如:void z(TSMatrix m, int row, TSMatrix &T){ // 在这里使用row变量 }在调用z函数时,您需要提供矩阵的行数,例如:
z(t, m, p);解决评论 打赏 举报无用 1