Home > Back-end >  How do you write this on matrix program?
How do you write this on matrix program?

Time:03-09

CodePudding user response:

 
A void MatrixAdd (int * and an int * b, int * r, int len)
{
for (int i=0; I & lt; Len. + + I)
{
R=a + b [I] [I] [I];
}
}

A void MatrixSub (int * and an int * b, int * r, int len)
{
for (int i=0; I & lt; Len. + + I)
{
R=[I] a [I] - b [I];
}
}

Void MatrixDisplay (int * m, int row, int col)
{
for (int i=0; I & lt; The row. + + I)
{
For (int j=0; J & lt; Col. + + j)
{
Printf (" % d \ t ", m + j] [I * col);
}

printf("\r\n");
}

printf("\r\n");
}

Void MatrixTest (void)
{
Srand (GetTickCount ());

Int row=0;
Int col=0;

Printf (" keyboard input matrix rows, the number of columns (m, n format) : \ r \ n ");
Scanf_s (" % d % d ", & amp; The row, & amp; Col);

Int len=row * col;

Int * a=new int [len];
Int * b=new int [len];
for (int i=0; I & lt; Len. + + I)
{
A [I]=rand () % 100;
B [I]=rand () % 100;
}

Printf (" matrix a=\ r \ n ");
MatrixDisplay (a, the row, col);

Printf (" matrix b=\ r \ n ");
MatrixDisplay (b, the row, col);

Int * r=new int [len];

Printf (" matrix a + b=\ r \ n ");
MatrixAdd (a, b, r, len);
MatrixDisplay (r, the row, col);

Printf (" matrix a - b=\ r \ n ");
MatrixSub (a, b, r, len);
MatrixDisplay (r, the row, col);

The delete [] a;
The delete [] b;
The delete [] r;
}
  • Related