Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions

Question

Propose a machine learning algorithm for a school

Answered: 1 week ago