Home > Back-end >  Please help have a look at where I went wrong
Please help have a look at where I went wrong

Time:09-18

Wrote a transposed matrix code, run without error, but do not know why the output of the transposed matrix is 0, the last line of all HTML code is as follows, causes and solutions, thank you very much!

#include
# define maxsize 20
Typedef struct
{
Int r;//line number
int c;//column number
Int d;//value
} tupnode;
Typedef struct
{
Int row;//the number of rows
Int col;//the number of columns
int num;//number of non-zero yuan
Tupnode data [maxsize + 1];
} the TSM;
Void createmat (TSM & amp; M)
{
Int I, k;
Tupnode t;
Printf (" please enter the number of rows of the matrix, the number of columns, non-zero element number: ");
Scanf_s (" % d, % d, % d ", & amp; M.r ow, & amp; M.c ol, & amp; M.n um, 3);
getchar();
If (m.n um & gt; Maxsize)
Printf (" ERROR ");
M. ata [0]. R=0;
For (I=1; I & lt;=m.n um; I++)
{
Printf (" please press the row order input the first line % d a non-zero element, columns, element value: ", I);
Scanf_s (" % d, % d, % d ", & amp; T.r, & amp; Tc, & amp; T.d);
M. ata [I]=t;
}
}
Void dispmat m (TSM)
{
Int I, j, k=1;
Tupnode * p=m. ata + 1;
for(i=1; I<=m.r ow; I++)
{
For (j=1; J & lt;=m.c ol; J++)
If (k & lt;=m.n um & amp; & P - & gt; R==I & amp; & P - & gt; C==j)
{
Printf (" % 3 d ", (p++) - & gt; D);//output p has pointed out the value of the element and point to the next element
k++;
}
The else
Printf (" % 3 d ", 0).
printf("\n");
}
}
Void trantat (TSM m, TSM & amp; T/t) for transposed matrix
{
Int p, c, q=1; Indicates the current element transposed matrix//q
T.r ow=m.c ol;
Tc ol=m.r ow;
T.n um=m.n um;
If (t.n um!=0)
For (c=1; C & lt; M.r ow. C + +)
For (p=1; p <=m.n um; P++)
If (m. ata [p].=c=c)
{
[q]. T.d ata r=m. ata [p]. C.
T.d ata [q]. C=m. ata [p]. R;
T.d ata [q] d=m. ata [p]. D;
Q++;
}
Dispmat (t);
}
Void main ()
{
The TSM a, b;
Printf (" create matrix a \ n ");
Createmat (a);
Dispmat (a);
Printf (" a transposed matrix for: \ n ");
Trantat (a, b);

}

CodePudding user response:

Where you where the output or transpose setting a breakpoint, look, is transposed wrong, wrong or output

CodePudding user response:

//scanf_s (" % d, % d, % d ", & amp; M.r ow, & amp; M.c ol, & amp; M.n um, 3);//don't need the parameters of the last 3 
Scanf_s (" % d, % d, % d ", & amp; M.r ow, & amp; M.c ol, & amp; M.n um);

Remove the three parameters, do not need,
In addition, m.n um need input? If the input number is not m.r ow * m.c ol that how to do?
M.n um is not m.r ow * m.c ol value?

CodePudding user response:

reference 2 building self-confidence boy reply:
//scanf_s (" % d, % d, % d ", & amp; M.r ow, & amp; M.c ol, & amp; M.n um, 3);//don't need the parameters of the last 3 
Scanf_s (" % d, % d, % d ", & amp; M.r ow, & amp; M.c ol, & amp; M.n um);

Remove the three parameters, do not need,
In addition, m.n um need input? If the input number is not m.r ow * m.c ol that how to do?
M.n um is not m.r ow * m.c ol value?

M.n um is the number of zero dollars, not the number of elements in the matrix

CodePudding user response:

reference 1/f, Simple, Soft reply:
place you in the output or transpose setting a breakpoint, look, is transposed wrong, or the output the wrong

Is transposed algorithm has a problem, but do not know should how to change
  • Related