Question: It is C language program and the code is given and needs to write and modify a function from matrix c file & main function

It is C language program and the code is given and needs to write and modify a function from matrix c file & main function that the program will print out the extreme values in the standard input stream.

---------------------------------------------

extreme.c file

#include

#include

#include \"matrix.h\"

int main () {

int num_rows, num_cols;

/* read the number of columns and the number of rows */

if (scanf(\"%d\",&num_cols) != 1) {

printf (\"ERROR: cannot read the number of columns \");

exit(1);

}

if (scanf(\"%d\",&num_rows) != 1) {

printf (\"ERROR: cannot read the number of rows \");

exit(1);

}

/* add code here */

}

-----------------------------------

matrix.c file

#include

#include

#include \"matrix.h\"

/* allocate and initialize to 0 a num_rows x num_cols matrix */

void matrix_init (matrix_type* mat_ptr, int num_rows, int num_cols) {

mat_ptr->num_rows = num_rows;

mat_ptr->num_cols = num_cols;

mat_ptr->data_ptr = (float*) calloc (num_rows*num_cols, sizeof(float));

#ifdef DEBUG

if (mat_ptr->data_ptr == 0) {

printf (\"error in matrix_init : calloc returned 0. \");

exit(1);

}

#endif

}

/* deallocate a matrix and set all entries to 0 */

void matrix_deinit (matrix_type* mat_ptr) {

mat_ptr->num_rows = 0;

mat_ptr->num_cols = 0;

free(mat_ptr->data_ptr);

mat_ptr->data_ptr = 0;

}

/* read a matrix from stdin */

void matrix_read (matrix_type* mat_ptr) {

int count = 0;

float next;

while (scanf(\"%g\",&next) == 1) {

if (count >= mat_ptr->num_rows*mat_ptr->num_cols) {

printf (\"ERROR: stdin contains too many numbers \");

exit(1);

}

mat_ptr->data_ptr[count] = next;

count += 1;

}

if (count num_rows*mat_ptr->num_cols) {

printf (\"ERROR: stdin contains too rew numbers \");

exit(1);

}

}

/* print a matrix to stdout */

void matrix_print (matrix_type* mat_ptr) {

int i;

matrix_row_type row;

printf (\"number of rows = %d \",mat_ptr->num_rows);

printf (\"number of columns = %d \",mat_ptr->num_cols);

for (i=0;inum_rows;i++) {

matrix_get_row (mat_ptr,&row,i);

matrix_row_print (&row);

}

}

/* get a row of a matrix (not a copy just a pointer into the data) */

void matrix_get_row (matrix_type* mat_ptr, matrix_row_type* row_ptr,

int row) {

#ifdef DEBUG

if ((row = mat_ptr->num_rows)) {

printf (\"error in matrix_get_row : row out of range \");

exit(1);

}

#endif

row_ptr->num_cols = mat_ptr->num_cols;

row_ptr->data_ptr = &(mat_ptr->data_ptr[row*mat_ptr->num_cols]);

}

/* print a row of a matrix to stdout */

void matrix_row_print (matrix_row_type* row_ptr) {

int i;

for (i=0;inum_cols;i++) {

printf (\"%g \",row_ptr->data_ptr[i]);

}

printf (\" \");

}

/* calculate the distance squared between two row vectors */

float matrix_row_dist_sq (matrix_row_type* row1_ptr,

matrix_row_type* row2_ptr) {

#ifdef DEBUG

if (row1_ptr->num_cols != row2_ptr->num_cols) {

printf (\"error in matrix_row_dist_sq: number of columns mismatch \");

}

#endif

/* add code here */

}

-----------------------------------------------------

matrix.h file

/* matrix.h */

/* make sure this header file has not been included already */

#ifndef __MATRIX

#define __MATRIX 1

typedef struct matrix_s {

int num_rows;

int num_cols;

float* data_ptr;

} matrix_type;

typedef struct matrix_row_s {

int num_cols;

float* data_ptr;

} matrix_row_type;

/* allocate and initialize to 0 a num_rows x num_cols matrix */

void matrix_init (matrix_type* mat_ptr, int num_rows, int num_cols);

/* deallocate a matrix and set all entries to 0 */

void matrix_deinit (matrix_type* mat_ptr);

/* read a matrix from stdin */

void matrix_read (matrix_type* mat_ptr);

/* print a matrix to stdout */

void matrix_print (matrix_type* mat_ptr);

/* get a row of a matrix (not a copy just a pointer into the data) */

void matrix_get_row (matrix_type* mat_ptr, matrix_row_type* row_ptr,

int row);

/* print a row of a matrix to stdout */

void matrix_row_print (matrix_row_type* row_ptr);

/* calculate the distance squared between two row vectors */

float matrix_row_dist_sq (matrix_row_type* row1_ptr,

matrix_row_type* row2_ptr);

#endif

-------------------------

Please modify main function and matrix row dist sqr function.

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 Programming Questions!