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: 3811089 • 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 2 3 -4 L9 10 11 12 More generally, define matrices A and B, such that, 11 12 a21 a22 a apl ap2 apa and 11 12 1r b21 b22 b2 baz bar Notice that A and B have the same number of columns and rows, respectively, i.e., A has dimensions p x and B has dimensions q x r. In this case, we can define the product of A and B, as the new matrix C J A. B, defined as the following matrix: C11 C12 Cir C21 c22 C2r Cpl Cp2 Cpr where aia ail Notice that the matrix Chas dimensions px 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 <stdlib.h>
#include <stdio.h>

void input_matrix(int** , int, int);
void print_matrix(int** , int, int);
int** multiply_matrices(int**, int,int, int**,int ,int);
int get_value_at(int**, int, int);
void read_dimensions(char*,int*,int*);
int** create_matrix(int,int);
void destroy_matrix(int**,int);
int main(){
   int r1,c1,r2,c2,r,c;
   int** arr1;
   int** arr2;
   int** arr;

   printf("Enter the dimension for first matrix :");
   read_dimensions("",&r1,&c1);
   printf("Enter the dimension for second matrix :");  
   read_dimensions("",&r2,&c2);

   if(c1!=r2){
       printf("Incompatable dimension ");
       return 1;
   }
   arr1 = create_matrix(r1,c1);
   arr2 = create_matrix(r2,c2);

   printf("Enter the values for first matrix : ");
   input_matrix(arr1,r1,c1);

   printf("Enter the values for second matrix : ");
   input_matrix(arr2,r2,c2);

   arr = multiply_matrices(arr1,r1,c1,arr2,r2,c2);
   r=r1;
   c=c2;
   printf("The resulting matrix is : ");
   print_matrix(arr,r,c);
  
   return 0;
}

void input_matrix(int** arr, int r, int c){
   int i,j;
   for(i=0;i<r; i++){
       for(j=0; j<c; j++){
           scanf("%d",arr[i][j]);
       }
   }
}
void print_matrix(int** arr, int r, int c){
   int i,j;
   for(i=0;i<r; i++){
       for(j=0; j<c; j++){
           printf(" %d ",arr[i][j]);
       }
       printf(" ");
   }
   printf(" ");
}

int** multiply_matrices(int** arr1, int r1,int c1, int** arr2,int r2,int c2){
   printf("test");
   int r=r1;
   int c=c2;
   int i;
   int j;
   int k;
   int** arr;
   arr = create_matrix(r,c);
   int sum =0;
   for(i=0;i<r; i++){
       for(j=0; j<c; j++){
           for(k=0;k<r;k++){
               sum += arr1[i][k]*arr2[k][j];
           }
           arr[r][c] = sum;
           sum=0;
       }
   }
   return arr;
}
int get_value_at(int** arr, int r, int c){

   return arr[r][c];
}
void read_dimensions(char* str,int* r,int* c){
   scanf("%d %d",r,c);
}
int** create_matrix(int r,int c){
   int **arr = (int **)malloc(r * sizeof(int *));
   int i;
   for (i=0; i<r; i++)
       arr[i] = (int *)malloc(c * sizeof(int));
   return arr;
}
void destroy_matrix(int** arr ,int r){
   int i;
   for(i=0; i<r; i++){
       free(arr[i]);
   }
}

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