Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

struct _matrix { int rows; int cols; double * data; }; int nRows(matrix const *

ID: 3736011 • Letter: S

Question

struct _matrix {
int rows;
int cols;
double * data;
};

int nRows(matrix const * mtx, int * n) {
if (!mtx || !n) return -1;
*n = mtx->rows;
return 0;
}

int nCols(matrix const * mtx, int * n) {
if (!mtx || !n) return -1;
*n = mtx->cols;
return 0;
}

Exercise 6.8. Implement the following specification 1) /* Returns column col of mt® as a new tector. Returns NULL 2if mta is NULL or col is inconsistent with mta 's 3*dimensions. matrix getColumn (matrix mtx, int col) 7/ /* Returns row row of mt 1s a row uector. Returns NULLf * mta is NULL or rou is inconsistent with mt's 9dimensions. 10* 11matrix getRow (matrix mtx int row);

Explanation / Answer

Please find the implementation of these two methods below.

CODE

======================

struct matrix* getColumn(struct matrix* mat, int col) {
if(mat == NULL || col > mat->cols)
return NULL;
struct matrix* col_vector;
col_vector->rows = mat -> rows;
col_vector->cols = 1;
for (int i = 0; i < mat->rows; i++)
*(col_vector->data + i*mat->cols + col) = *(mat->data + i*mat->cols + col);
return col_vector;
}

struct matrix* getRow(struct matrix* mat, int row) {
if(mat == NULL || row > mat->rows)
return NULL;
struct matrix* row_vector;
row_vector->rows = 1;
row_vector->cols = mat->cols;
for (int i = 0; i < mat->cols; i++)
*(row_vector->data + row*mat->cols + i) = *(mat->data + row*mat->cols + i);
return row_vector;
}