There are non-rectangular matrix. Can't make a copy of it. Can't find the bug in function copyMatrix()

Write in C, not C++

Create two matrices (original and its future copy).

Write in C, not C++

```
// structure for specifying rows of the matrix
struct Line{
int n; // number of elements in the row of the matrix
int *a; // array of elements
};
// structure to define the matrix
struct Matrix{
int lines; // number of rows of the matrix
Line *matr; // array of rows of the matrix
};
```

Create two matrices (original and its future copy).

```
int main()
{
Matrix matr = { 0, NULL }; // source matrix
Matrix matrT = { 0, NULL }; // copy source matrix
inputMatrix(&matr); // this function to fill the matrix, where we indicate all the dimensions of the matrix and the elements themselves. Everything works
copyMatrix(&matr, &matrT); // create a copy. but it is not created.
outputMatrix("Source matrix", matr); // print the original matrix on the screen all is displayed
outputMatrix("Transposed matrix", matrT); // print the copied matrix to the screen. Not output anything except ""Transposed matrix"
}
```

```
void copyMatrix(Matrix *arr1, Matrix *arr2) // create copy of matrix
{
arr2->matr = (Line *)malloc(array1->lines, sizeof(Line));
for (int i = 0; i < array1->lines; i++)
{
arr2->matr[i].a = (int *)malloc(array1->matr[i].n*sizeof(int));
for (int j = 0; j < arr1->matr[i].n; j++)
arr2->matr[i].a[j] = arr1->matr[i].a[j];
}
}
```

asked September 19th 19 at 12:20

1 answer

answered on

Solution

Forgot to specify dimensions for the second matrix. The items are copied, and the properties of n and lines - no.

Find more questions by tags CC++Data structuresMathematical matrix