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;
}
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;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.