Matrix Multiplication
The Program:
For this assignment, you will multiply two matrices and print the result. If A is an m x n matrix, and B is an n x p matrix, then the product matrix C, which is A x B, is defined to be the m x p matrix whose entry in the ith row and the jth column is the sum of the products of corresponding entries of the ith row of A and the jth column of B. A standard algorithm is:
for(int i=0; i < m; i++)
    for(int j=0; j < p; j++) {
       C[i][j] = 0;
       for(int k=0; k < n; k++)
            C[i][j] += A[i][k] * B[k][j];

Input will come from a text file, and the name of the file must be read from the command line. The text file will take the following form. The first three lines of the file contain the values m, n, and p, each on a line by themselves. Following are two matrices, A followed by B with no blank lines, of the dimensions specified, one row per line. Each row entry is separated by a space.   Example: Given the following two matrices A and B:
A = |1 2|    B = |3|
The datafile will then have this format:
1 2

Header File:
For this assignment, you must use the provided header file p3.h
You are required to implement the three functions in this header file, and you may not create any additional functions in your program. You must include this header file in your program as follows:
#include "p3.h"
No other #include directives are permitted in your program file. You must not modify the header file in any way. The original version will be used to compile your program.
As usual, your program will first print a title line, consisting of the assignment number, your class account, and your name, all on one line. Then your program will print the two matrices to be multiplied, and finally print the resulting matrix. Each of the three matrices should be labeled. Example:

Program #, Class Account, Student Name
Matrix A contents:
    1    2
    3    4
    5    6

Matrix B contents:
    7    8    9   10
   11   12   13   14

Matrix A * B is:
   29   32   35   38
   65   72   79   86
101 112 123 134

The datafile read for this example is:
1 2
3 4
5 6
7 8 9 10
11 12 13 14

Additional Requirements:
• You may not use any brackets [ ] in your code.   Array elements must be referenced using pointer arithmetic.
• The matrices A, B, and C must be declared as single integer pointer variables. int *A, *B, *C;
• No error checking is required for this assignment. You may assume that any datafile used for testing will contain only integer values in the format specified in the assignment.
• Your source code file will be compiled on rohan.
• Your program file must consist of exactly four functions -- a main function, plus the three functions defined in the header file.

Solution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

id print_matrix(int * matrix, int row, int column){
    int *it;
    int i;
    int j;   
    it = matrix;
    j = 0;
    for (i = 0; i < row * column; i++) {            
       if (j == 0)
       printf("%d ", *it)   ;
       if (j == column) {
          j = 0;

void mult_matrices(int *A, int *B, int *C, int m, int n, int p){
    int i,j,k;
    int *pC;
    int *pB;
    int *pA;
    for( i=0; i < m; i++)
    for( j=0; j < p; j++) {
    // index is current row * column + current column
       pC = C + (i * p + j);
       *pC = 0;
       for( k=0; k < n; k++) {
            pA = A + (i * n + k);
            pB = B + (k * p + j);
            *pC += (*pA) * (*pB);         

This is only a preview of the solution. Please use the purchase button to see the entire solution


or free if you
register a new account!

Assisting Tutor

Related Homework Solutions

Get help from a qualified tutor
Live Chats