I created the following code using C programmming but for some reason its not ru
ID: 3567603 • Letter: I
Question
I created the following code using C programmming but for some reason its not running. Can you show me whats wrong.
#include <stdio.h>
void matrix_multiplication(int* left_matrix, int* right_matrix, int* result_matrix, int rows, int cols, int shared);
int main(void)
{
int i,j,count=0;
int matrix1[2][2];
int matrix2[2][2];
int matrix3[2][2];
printf("Matrix1 is: ");
for(i=0;i<2;i++) ///
{ ///
for(j=0;j<2;j++) ///
{ ///
count=count+3; ////----> initializing the first matrix1
matrix1[i][j]=count; //
printf("%5d",matrix1[i][j]);//
} //
printf(" ");
}
printf(" ");
count=0;
printf("Matrix2 is: ");
for(i=0;i<2;i++) ///
{ ///
for(j=0;j<2;j++) ///
{ ///----> initializing the first matrix2
count=count+4; //
matrix2[i][j]=count; //
printf("%5d",matrix2[i][j]);//
}
printf(" ");
}
printf(" ");
matrix_multiplication(&matrix1, &matrix2,&matrix3[0][0], 0, 0, 2);//storing the multiplication for first row first column
matrix_multiplication(&matrix1, &matrix2,&matrix3[0][1], 0, 1, 2);//storing the multiplication for first row second column
matrix_multiplication(&matrix1, &matrix2,&matrix3[1][0], 1, 0, 2);//storing the multiplication for second row first column
matrix_multiplication(&matrix1, &matrix2,&matrix3[1][1], 1, 1, 2);//storing the multiplication for second row second column
printf("The multiplication of matrix1 and matrix2 is: ");
for(i=0;i<2;i++) ///
{ ///
for(j=0;j<2;j++) ///
{ ////
printf("%5d",matrix3[i][j]);///----> printing the result in matrix 3
} //
printf(" "); //
} //
return 0;
}
void matrix_multiplication(int* left_matrix, int* right_matrix, int* result_matrix, int rows, int cols, int shared)
{
int a,b,c,d;
if(rows==0 && cols==0)
{
c=*left_matrix; ///
d=*right_matrix; //
a=*left_matrix++; //
a=*left_matrix++; //
/////-->accessing to the rows and columns for the multiplication
b=*right_matrix++; //
b=*right_matrix++; //
b=*right_matrix++; ///
*result_matrix = (c)*(d)+a*b;//---->storing the first multiplication value in row 1 and column1
}
else if(rows==0 && cols==1)
{
c=*left_matrix++; ///
d=*right_matrix++; //
d=*right_matrix++; //
//-->accessing to the rows and columns for the multiplication
a=*left_matrix++; //
//
b=*right_matrix++; //
b=*right_matrix++; //
*result_matrix = (c)*(d)+a*b;//---->storing the first multiplication value in row 1 and column2
}
else if(rows==1 && cols==0)
{
c=*left_matrix++; ///
c=*left_matrix++; //
c=*left_matrix++; //
//
d=*right_matrix++; //////---->accessing to the rows and columns for the multiplication
//
a=*left_matrix++; //
//
b=*right_matrix++; //
b=*right_matrix++; //
*result_matrix = (c)*(d)+a*b;//---->storing the first multiplication value in row 2 and column1
}
else if(rows==1 && cols==1)
{
c=*left_matrix++; ///
c=*left_matrix++; //
c=*left_matrix++; //
///
d=*right_matrix++; ////---->accessing to the rows and columns for the multiplication
d=*right_matrix++; //
//
a=*left_matrix++; //
//
b=*right_matrix++; //
b=*right_matrix++;//
*result_matrix = (c)*(d)+a*b;//---->storing the first multiplication value in row 2 and column2
}
}
Explanation / Answer
replace &matrix1 with (int*)matrix1 in the argument when you are calling the function, I have added the code below check it
#include <stdio.h>
void matrix_multiplication(int* left_matrix, int* right_matrix, int* result_matrix, int rows, int cols, int shared);
int main(void)
{
int i,j,count=0;
int matrix1[2][2];
int matrix2[2][2];
int matrix3[2][2];
printf("Matrix1 is: ");
for(i=0;i<2;i++) ///
{ ///
for(j=0;j<2;j++) ///
{ ///
count=count+3; ////----> initializing the first matrix1
matrix1[i][j]=count; //
printf("%5d",matrix1[i][j]);//
} //
printf(" ");
}
printf(" ");
count=0;
printf("Matrix2 is: ");
for(i=0;i<2;i++) ///
{ ///
for(j=0;j<2;j++) ///
{ ///----> initializing the first matrix2
count=count+4; //
matrix2[i][j]=count; //
printf("%5d",matrix2[i][j]);//
}
printf(" ");
}
printf(" ");
matrix_multiplication((int*)matrix1, (int*)matrix2,&matrix3[0][0], 0, 0, 2);//storing the multiplication for first row first column
matrix_multiplication((int*)matrix1, (int*)matrix2,&matrix3[0][1], 0, 1, 2);//storing the multiplication for first row second column
matrix_multiplication((int*)matrix1, (int*)matrix2,&matrix3[1][0], 1, 0, 2);//storing the multiplication for second row first column
matrix_multiplication((int*)matrix1, (int*)matrix2,&matrix3[1][1], 1, 1, 2);//storing the multiplication for second row second column
printf("The multiplication of matrix1 and matrix2 is: ");
for(i=0;i<2;i++) ///
{ ///
for(j=0;j<2;j++) ///
{ ////
printf("%5d",matrix3[i][j]);///----> printing the result in matrix 3
} //
printf(" "); //
} //
return 0;
}
void matrix_multiplication(int* left_matrix, int* right_matrix, int* result_matrix, int rows, int cols, int shared)
{
int a,b,c,d;
if(rows==0 && cols==0)
{
c=*left_matrix; ///
d=*right_matrix; //
a=*left_matrix++; //
a=*left_matrix++; //
/////-->accessing to the rows and columns for the multiplication
b=*right_matrix++; //
b=*right_matrix++; //
b=*right_matrix++; ///
*result_matrix = (c)*(d)+a*b;//---->storing the first multiplication value in row 1 and column1
}
else if(rows==0 && cols==1)
{
c=*left_matrix++; ///
d=*right_matrix++; //
d=*right_matrix++; //
//-->accessing to the rows and columns for the multiplication
a=*left_matrix++; //
//
b=*right_matrix++; //
b=*right_matrix++; //
*result_matrix = (c)*(d)+a*b;//---->storing the first multiplication value in row 1 and column2
}
else if(rows==1 && cols==0)
{
c=*left_matrix++; ///
c=*left_matrix++; //
c=*left_matrix++; //
//
d=*right_matrix++; //////---->accessing to the rows and columns for the multiplication
//
a=*left_matrix++; //
//
b=*right_matrix++; //
b=*right_matrix++; //
*result_matrix = (c)*(d)+a*b;//---->storing the first multiplication value in row 2 and column1
}
else if(rows==1 && cols==1)
{
c=*left_matrix++; ///
c=*left_matrix++; //
c=*left_matrix++; //
///
d=*right_matrix++; ////---->accessing to the rows and columns for the multiplication
d=*right_matrix++; //
//
a=*left_matrix++; //
//
b=*right_matrix++; //
b=*right_matrix++;//
*result_matrix = (c)*(d)+a*b;//---->storing the first multiplication value in row 2 and column2
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.