Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Introduction: Write a C program that performs matrix multiplication. For this pr

ID: 3806392 • Letter: I

Question

Introduction: Write a C program that performs matrix multiplication. For this program, a matriz is a rectangular array of integer entries, like this 1 3 -4 5 6 7 8 9 10 11 12 More generally, define matrices A and B, such that, a11 a12 a la a21 22 a24 apl ap2 apa and b21 b 2... gl Notice that A and B have the same number of columns and rows respectively, i.e., A has dimensions p x q and B has dimensions q x r. In this case, we can define the product of A and B, as the new matrix C A. B, defined as the following matrix C11 C12 cir C21 C22 Cpl Cp2 where Notice that the matrix Chas dimensions p x r. If A and B have incompatible dimensions, i.e., when A and B do not have the same number of columns and rows, respectively, then you cannot compute the product A B. Here is Java code that performs matrix multiplication http://introcs.cs.princeton.edu/java/22library/Matrix.java.html See the Requirements section for further details

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>

void input_matrix(int**, int, int);
void multiply(int **mat1, int **mat2, int **mat, int p, int q, int r);
void print_matrix(int** mat, int n, int m);
void destroy_matrix(int**, int);
void read_dimensions(char *message, int *n, int *m);

int main()
{
int n1, m1, n2, m2;
read_dimensions("Enter dimension of first matrix as space seprated integers: ", &n1, &m1);
read_dimensions("Enter dimension of second matrix as space seprated integers: ", &n2, &m2);

if (m1 != n2)
{
printf("Can not multiply matrix as dimension do not match ");
return 1;
}

int i, j;
int **mat1;
mat1 = malloc(n1 * sizeof(int *));
if(mat1 == NULL)
{
printf("Can't allocate memory ");
return 1;
}
for(i = 0; i < n1; i++)
{
mat1[i] = malloc(m1 * sizeof(int));
if(mat1[i] == NULL)
{
printf("Can't allocate memory ");
destroy_matrix(mat1, i);
return 1;
}
}
  
printf("Enter value for first matrix ");
input_matrix(mat1, n1, m1);
int **mat2;
mat2 = malloc(n2 * sizeof(int *));
if(mat2== NULL)
{
printf("Can't allocate memory ");
destroy_matrix(mat1, n1);
return 1;
}
for(i = 0; i < n2; i++)
{
mat2[i] = malloc(m2 * sizeof(int));
if(mat2[i] == NULL)
{
printf("Can't allocate memory ");
destroy_matrix(mat1, n1);
destroy_matrix(mat2, i);
return 1;
}
}
  
printf("Enter value for second matrix ");
input_matrix(mat2, n2, m2);
  
int **mat;
mat = malloc(n1 * sizeof(int *));
if(mat== NULL)
{
printf("Can't allocate memory ");
destroy_matrix(mat1, n1);
destroy_matrix(mat2, n2);
return 1;
}
  
for(i = 0; i < n1; i++)
{
mat[i] = malloc(m2 * sizeof(int));
if(mat[i]== NULL)
{
printf("Can't allocate memory ");
  
destroy_matrix(mat1, n1);
destroy_matrix(mat2, n2);
destroy_matrix(mat, i);
return 1;
}
}
multiply(mat1, mat2, mat, n1, m1, m2);   

printf("The resulting matrix is: ");
//print_matrix(mat, n1, m2);
for(i = 0; i < n1; i++)
{
for(j = 0; j < m2; j++)
{
printf("%d ", *((int *)mat + 2*i*n1 + 2*j));
}
printf(" ");
}
return 0;
}

void read_dimensions(char *message, int *n, int *m)
{
printf("%s", message);
scanf("%d%d", n, m);
}

void input_matrix(int** mat, int n, int m)
{
int i,j;
for(i = 0; i < n; i++)
{
for(j = 0; j < m; j++)
{
scanf("%d", (mat + i*n + j));
}
}
}

void print_matrix(int** mat, int n, int m)
{
int i, j;
for(i = 0; i < n; i++)
{
for(j = 0; j < m; j++)
{
printf("%d ", *(mat + i*n + j));
}
printf(" ");
}
}

void destroy_matrix(int** mat, int n)
{
int j;
for(j = 0; j < n; j++)
{
free(mat[j]);
}
//free(mat);
}

void multiply(int** mat1, int** mat2, int** mat, int p, int q, int r)
{
int i, j, k;
for (i = 0; i < p; i++)
{
for (j = 0; j < r; j++)
{
*((int *)mat + (2*i)*p + (2*j)) = 0;
  
printf(" Col %d: ", j);
for (k = 0; k < q; k++)
{
*((int *)mat + p*2*i + 2*j) += (*((int *)mat1 + p*2*i + 2*k)) *(*((int *)mat2 + q*(2*k) + 2*j));
}
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote