Question: COMPLETE THIS PROGRAM IN C USING THE TEMPLATE BELOW: TEMPLATE: #include #include #include struct Matrix { size_t row; size_t col; double **data; }; typedef struct

COMPLETE THIS PROGRAM IN C USING THE TEMPLATE BELOW:

COMPLETE THIS PROGRAM IN C USING THE TEMPLATE BELOW: TEMPLATE: #include #include#include struct Matrix { size_t row; size_t col; double **data; }; typedefstruct Matrix TMatrix; /* * Creates and returns a matrix of size

TEMPLATE:

#include

#include

#include

struct Matrix {

size_t row;

size_t col;

double **data;

};

typedef struct Matrix TMatrix;

/*

* Creates and returns a matrix of size rows x cols

* - rows : (non-negative value) giving the number of rows

* - cols : (non-negative value) giving the number of columns

* If the allocation is not successful, the function should return NULL

* If the allocation is successful, the mat field of the ADT should

* point to an array of pointers (representing the rows) and each pointer

* in that array should point to an array of double representing the values

* in that row.

*/

TMatrix * newMatrix(size_t rows, size_t cols)

{

// TODO

return NULL;

}

/*

* This function is responsible for deallocating the dynamic memory

* currently used by this matrix. Namely, it should deallocate the rows

* and the array of row storage.

*/

void freeMatrix(TMatrix * m)

{

/*

Free the space for a matrix; the number of rows

may be needed if each row is allocated with a

separate malloc/calloc

*/

// TODO

if (m == NULL) /* remember to check m == NULL in every function */

return;

}

/*

* This function takes as input a matrix ADT and reads, from the standard

* input, a collection of row x col doubles representing the content of the

* matrix. Note that the content of the matrix should be given in row-major

* order. Namely, the matrix

* 1 2 3

* 4 5 6

* 7 8 9

* should be conveyed with a white-space separated sequence of 9 numbers:

* 1 2 3 4 5 6 7 8 9

* If the input is malformed, the reading should be interrupted and the

* function shall return the value 0 to report a failed read.

* If the input is well-formed, the reading should complete and the function

* shall return the value 1.

*/

int readMatrix(TMatrix * m)

{

/*

Read in from standard input the elements of a

rows X cols matrix, given row by row, and within

a row in increasing column order

*/

if (m == NULL)

return 0;

// TODO

return 1;

}

/*

* The transposeMatrix function takes as input a matrix ADT m and returns a

* new TMatrix ADT that holds the transpose of m. Transposition should run in

* O(n x m) (where n is the # of rows and m the # of columns).

* If memory allocation for the transpose failed, the routine should return

* a 0x0 matrix ADT.

* Transposition follows the usual mathematical definition of transposition.

*/

TMatrix * transposeMatrix(TMatrix * m)

{

/*

Allocate and return the address of a matrix that contains

the element of "a" in transposed order (with rows and columns

swapped)

*/

// TODO

if (m == NULL)

return NULL;

TMatrix *t = newMatrix(m->col, m->row);

return t;

}

/*

* The printMatrix function takes a matrix ADT as input and produces, on the

* standard output, a representation of the matrix in row-major format. For

* instance, the 3x3 identity matrix should print on 3 lines as:

* 1 0 0

* 0 1 0

* 0 0 1

*/

void printMatrix(TMatrix * m)

{

/*

Print elements of rows X cols matrix a to standard output,

one line per row

*/

if (m == NULL)

return;

for (int i = 0; i row; i++) {

for (int j = 0; j col; j++)

printf("%g ", m->data[i][j]);

printf(" ");

}

}

int main()

{

int m = 0, n = 0;

printf("Enter m and n (

scanf("%d %d", &m, &n);

while ((m > 0) && (n > 0)) {

TMatrix * a = newMatrix(m, n);

if (a != NULL) {

printf("Enter %dx%d matrix: ", m, n);

if (readMatrix(a)) {

printf("Input matrix: ");

printMatrix(a);

TMatrix *t = transposeMatrix(a);

if (t != NULL) {

printf("Transposed matrix: ");

printMatrix(t);

freeMatrix(t);

printf(" ");

}

} else {

printf("Error while reading the matrix. ");

}

freeMatrix(a);

}

printf("Enter m and n (

int res = scanf("%d %d", &m, &n);

if (res != 2)

m = 0;

}

return 0;

}

Exercise 1. Matrix Transposition (50 points) Matrices can be represented in C as two-dimensional arrays, i.e., arrays of arrays. For example, we can declare a 3 3 natrix of double-precision real values by double a [4] [3] However, this representation is not flexible if we want to have an abstract data type that can handle matrices of any size. A better approach is to define such an abstract data type as follows 1 struct Matrix f 2size t row; 3 size t col; double **data; 6 7 typedef struct Matrix TMatrix; Where row and col are numerical attributes representing the numbers of rows and columns of the matrix and mat is a double pointer to double to capture the actual matrix values. Namely, data is an array of pointers to arrays of doubles (each nested array representing a row of the matrix. For instance, a 4 3 matrix can be pictured as row: 4 col: 3 data Row 3 Row 2 Row 1 Row 0

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!