Question: In this lab I am providing you with a code that performs the dot product on two matrices. I want you to edit this code

In this lab I am providing you with a code that performs the dot product on two
matrices.
I want you to edit this code so that we use multiprocessing (fork) where each
child calculates a rows of the resulting matrix.
Additionally, please get the process ID of each child process that we create.
If you would like to edit the size of the matrices youre welcome to do so, I
made them each 2x2 to start.
this is the code below please implement the answer related to the question I give you. and also show me the output
#include
#include
#include
void matrix_multiply(int rows_a, int cols_a, int rows_b, int cols_b, int **a, int
**b){
if (rows_a != cols_b){
printf("Dimensions mismatch.
");
return;
}
int **result =(int **)malloc(rows_a * sizeof(int *));
for (int i =0; i < rows_a; i++){
result[i]=(int *)malloc(cols_b * sizeof(int));
assert(result[i]!= NULL);
}
for (int i =0; i < rows_a; i++){
for (int j =0; j < cols_b; j++){
result[i][j]=0;
for (int k =0; k < cols_a; k++){
result[i][j]+= a[i][k]* b[k][j];
}
}
}
printf("Result Matrix:
");
for (int i =0; i < rows_a; i++){
for (int j =0; j < cols_b; j++){
printf("%d ", result[i][j]);
}
printf("
");
}
for (int i =0; i < rows_a; i++){
free(result[i]);
}
free(result);
}
int main(){
int rows_a =2;
int cols_a =2;
int rows_b =2;
int cols_b =2;
int **a =(int **)malloc(rows_a * sizeof(int *));
for(int i =0; i < rows_a; i++){
a[i]=(int*)malloc(sizeof(int)* cols_a);
assert(a[i]!= NULL);
}
for(int i =0; i < rows_a; i++){
for(int j =0; j < cols_b; j++){
a[i][j]= i +1+ j;
}
}
printf("Contents of the array:
");
for(int i =0; i < rows_a; i++){
for(int j =0; j < cols_b; j++){
printf("%d ", a[i][j]);
}
printf("
");
}
int **b =(int **)malloc(rows_b * sizeof(int *));
for(int i =0; i < rows_b; i++){
b[i]=(int*)malloc(sizeof(int)* cols_b);
assert(a[i]!= NULL);
}
for(int i =0; i < rows_b; i++){
for(int j =0; j < cols_b; j++){
b[i][j]= i +10+ j;
}
}
printf("Contents of the array:
");
for(int i =0; i < rows_b; i++){
for(int j =0; j < cols_b; j++){
printf("%d ", b[i][j]);
}
printf("
");
}
matrix_multiply(rows_a, cols_a, rows_b, cols_b, a, b);
for(int i =0; i < rows_b; i++){
free(a[i]);
}
for(int i =0; i < rows_b; i++){
free(b[i]);
}
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!