Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Time it took Matthew 10 minutes Files to submit mat_multiply.c mat_multiply.h a Makefile, named Makefile, that will compile main.c, mat_multiply.c, and mat_multiply.h into an executable

Time it took Matthew

10 minutes

Files to submit

mat_multiply.c

mat_multiply.h

a Makefile, named Makefile, that will compile main.c, mat_multiply.c, and mat_multiply.h into an executable named mat_multiply.out

Problem

Write a program that multiplies two matrices together and then displays the results. If you dont know how to do matrix multiplication I recommend looking over this explanation to see how it is done. You can use this online calculator to help you check your results and see if they are correct.

To help you practice more with problem decomposition, you will NOT be coming up with your solution from scratch. Instead, you must implement the methods that you find in the starter code, which is at the END of the prompt. Each function will tell you what code you need to write.

This code will be called from main.c, which has been provided in the starter code. Look at it to see how all the functions are connected together. You can NOT modify main.c.

You must also write a makefile that will compile together main.c, mat_multiply.c, and mat_multiply.h into an executable named mat_multiply.out.

Specifications

You MUST dynamically allocate the space for your matrices

Any solutions that contain statically allocated arrays will be given zeros

The values in the matrices will always be integers

There is no maximum size for the matrices input

The matrices will not always be square (have the same number of rows and columns)

The matrices will be entered one line at a time

Assumptions

Input will always be valid.

Valid Input

The user will enter input in the following order

The number of rows in the matrices

The number of columns in the matrices

The values of matrix A

The values of matrix B

Restrictions

No global variables may be used

Hints

This would be a great problem to use input redirection on when testing on your own computer. The syntax is ./mat_add.exe < input.txt assuming your executable was called mat_add.exe and the file with your inputs was called input.txt. When creating input.txt you just type into the file exactly like what you would type if the program was running.

Examples

In the examples below user input has been italicized. You don't have to do any italicizing in your program. It is just there to help you differentiate between what is input and what is output.

Example 1

Enter the dimensions of matrix A: 2 2

Enter Matrix A

1 2

3 4

Enter the dimensions of matrix B: 2 2

Enter Matrix B

10 20

30 40

A * B =

70 100

150 220

Example 2

Enter the dimensions of matrix A: 3 4

Enter Matrix A

2 5 7 9

3 8 5 17

2 3 1 -3

Enter the dimensions of matrix B: 4 1

Enter Matrix B

1 2 3 4

A * B =

69

102

-1

Main.c

#include

#include

#include "mat_multiply.h"

void check_matrix_is_null(int** matrix, const char* matrix_name){

/*

print whether matrix has been set to NULL or not

@matrix: the matrix to check if it has been set to NULL

@matrix_name: the name of the matrix

*/

if(matrix == NULL){

printf("%s set to NULL. ", matrix_name);

}else{

printf("%s was NOT set to NULL! ", matrix_name);

}

}

int main() {

int** matrix_a;

int num_rows_a, num_cols_a;

int** matrix_b;

int num_rows_b, num_cols_b;

int** matrix_c;

int num_rows_c, num_cols_c;

get_matrix_dimensions_from_user("Enter the dimensions of matrix A: ", &num_rows_a, &num_cols_a);

matrix_a = make_empty_matrix(num_rows_a, num_cols_a);

printf("Enter Matrix A ");

fill_matrix_from_user_input(matrix_a, num_rows_a, num_cols_a);

get_matrix_dimensions_from_user("Enter the dimensions of matrix B: ", &num_rows_b, &num_cols_b);

matrix_b = make_empty_matrix(num_rows_b, num_cols_b);

printf("Enter Matrix B ");

fill_matrix_from_user_input(matrix_b, num_rows_b, num_cols_b);

matrix_c = matrix_multiply(matrix_a, num_rows_a, num_cols_a,

matrix_b, num_rows_b, num_cols_b,

&num_rows_c, &num_cols_c);

printf("A * B = ");

print_matrix(matrix_c, num_rows_c, num_cols_c);

delete_matrix(&matrix_a, num_rows_a, num_cols_a);

delete_matrix(&matrix_b, num_rows_b, num_cols_b);

delete_matrix(&matrix_c, num_rows_c, num_cols_c);

check_matrix_is_null(matrix_a, "matrix_a");

check_matrix_is_null(matrix_b, "matrix_b");

check_matrix_is_null(matrix_c, "matrix_c");

return 0;

}

mat_multiply.h

#ifndef MAT_MULTIPLY_H

#define MAT_MULTIPLY_H

void get_matrix_dimensions_from_user(char* prompt, int* num_rows, int* num_cols);

int** make_empty_matrix(int num_rows, int num_cols);

void fill_matrix_from_user_input(int** matrix, int num_rows, int num_cols);

int** matrix_multiply(int** matrix_a, int num_rows_a, int num_cols_a,

int** matrix_b, int num_rows_b, int num_cols_b,

int* out_num_rows_c, int* out_num_cols_c);

void print_matrix(int** matrix, int num_rows, int num_cols);

void delete_matrix(int*** matrix, int num_rows, int num_cols);

#endif

mat_multiply.c

#include

#include

#include "mat_multiply.h"

void get_matrix_dimensions_from_user(char* prompt, int* num_rows, int* num_cols){

/*

Display the given prompt to the user, then read two integers from the

user storing the result in num_rows and num_cols

@prompt: The prompt to show to the user

@num_rows: the address of where to store the first number entered by the user

@num_cols: the address of where to store the second number entered by the user

@returns: nothing

@modifies: num_rows, num_cols

*/

}

int** make_empty_matrix(int row_dim, int col_dim) {

/*

Dynamically create a row_dim X col_dim matrix

A matrix here is an array of array of integers

You do not need to initialize the values of the numbers in

the matrix but you can if you want

@row_dim: the number of rows the matrix should have

@col_dum: the number of columns the matrix should have

@returns: a row_dim X col_dim matrix of integers

@modifies: nothing

*/

}

void fill_matrix_from_user_input(int** matrix, int row_dim, int col_dim) {

/*

Fill in the row_dim X col_dim matrix with values entered by the user

The user will enter in row_dim rows that each have col_dim elements in it.

For example if row_dim = 3 and col_dim = 4 the user could enter something like

11 25 43 45

10 45 63 17

-8 25 -9 24

These values should be stored into matrix

@matrix: the matrix to be filled in

@row_dim: the number of rows matrix has

@col_dum: the number of columns matrix has

@returns: nothing

@modifies: matrix

*/

}

int** matrix_multiply(int** matrix_a, int num_rows_a, int num_cols_a,

int** matrix_b, int num_rows_b, int num_cols_b,

int* out_num_rows_c, int* out_num_cols_c) {

/*

Multiply matrix_a and matrix_b together, returning the resulting matrix

Set out_num_rows_c to be the number of rows in the resulting matrix

Set out_num_cols_c to be the number of cols in the resulting matrix

@matrix_a: the A in C = A X B

@num_rows_a: the number of rows in matrix a

@num_cols_a: the number of columns in matrix a

@matrix_b: the B in C = A X B

@num_rows_b: the number of rows in matrix b

@num_cols_b: the number of columns in matrix b

@out_num_rows_c: where to store the number of rows of the resulting matrix

@out_num_cols_c: where to store the number of columns of the resulting matrix

@returns: matrix_a X matrix_b

@modifies: out_num_rows_c, out_num_cols_c

*/

}

void print_matrix(int** matrix, int row_dim, int col_dim) {

/*

Print matrix to the screen

@matrix: the matrix to be printed

@row_dim: the number of rows in matrix

@col_dim: the number of columns in matrix

@returns: nothing

@modifies: nothing

*/

}

void delete_matrix(int*** matrix, int row_dim, int col_dim) {

/*

Delete matrix AND set it to NULL.

@matrix: the address of the matrix to be deleted

@row_dim: the number of rows in matrix

@col_dim: the number of columns in matrix

@returns: nothing

@modifies: matrix

*/

}

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

Step: 3

blur-text-image

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

Oracle RMAN For Absolute Beginners

Authors: Darl Kuhn

1st Edition

1484207637, 9781484207635

Students also viewed these Databases questions