Question: Coding exercise ( Language: C ) You tutor students part - time as part of your work - study program. Currently, the students are facing

Coding exercise (Language: C)
You tutor students part-time as part of your work-study program. Currently, the students are facing threads and memory management for the first time and theyre having a hard time. To help them, you devised an exercise for them to practice with. One of the students submitted the following code, everything they added is encased to the base code supplied with /*Students Code*/ Comment.
#include
#include
#include
int threads;
double sol[100][100];
struct matrix{ int data[100][100]; int size1; int size2; };
void *runner(void *param){
/*Students Code*/
struct matrix *matrices =(struct matrix*)param;
int start_row =*((int*)param);
int end_row = start_row +1;
for (int i = start_row; i < end_row; i++){
for (int j =0; j < matrices[1].size2; j++){
sol[i][j]=0;
for (int k =0; k < matrices[0].size2; k++){
sol[i][j]+= matrices[0].data[i][k]* matrices[1].data[k][j];
}
}
}
/*Students Code*/
pthread_exit(0);
}
void matrixOperation(int scalar, struct matrix X, struct matrix Y){
/*Students Code*/
pthread_t tid[threads];
int rows_per_thread = X.size1/ threads;
int remaining_rows = X.size1% threads;
int thread_args[threads];
int start_row =0;
for (int i =0; i < threads; i++){
thread_args[i]= start_row;
int rows = rows_per_thread +(remaining_rows >0?1 : 0);
remaining_rows--;
start_row += rows;
pthread_create(&tid[i], NULL, runner, &thread_args[i]);
}
for (int i =0; i < threads; i++){
pthread_join(tid[i], NULL);
}
/*Students Code*/
// Prints out the solution matrix
printf("Result =
[");
for(int i =0; i < X.size1; i++){
printf("[");
for(int j =0; j < X.size2; j++){
(j+1== X.size2)? printf("%f", sol[i][j]) : printf("%f,", sol[i][j]);
}
(i+1== X.size1)? printf("]") : printf("],
");
}
printf("]
");
return;
}
/*Students Code*/
int main(){
struct matrix X, Y;
X.data[0][0]=1;
X.data[0][1]=2;
X.data[1][0]=3;
X.data[1][1]=4;
X.size1=2;
X.size2=2;
Y.data[0][0]=5;
Y.data[0][1]=6;
Y.data[1][0]=7;
Y.data[1][1]=8;
Y.size1=2;
Y.size2=2;
threads =1;
matrixOperation(3, X, Y);
threads =2;
Y.data[1][0]=12;
matrixOperation(2, X, Y);
Y.size2=4;
threads =2;
matrixOperation(2, X, Y);
return 0;
}
/*Students Code*/
Students Result:
Result =[[0.000000,0.000000],[0.000000,0.000000]]
Result =[[0.000000,0.000000],[0.000000,0.000000]]
Result =[[0.000000,0.000000],[0.000000,0.000000]]
Expected Result:
Result =[[18.000000,24.000000],[30.000000,36.000000]]
Result =[[12.000000,16.000000],[30.000000,24.000000]]
ILLEGAL OPERATION
There are multiple errors within the code, how would you go about resolving those issues and explaining the changes made such that the student can improve? Was the approach efficient? (Only use pThreads)

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 Databases Questions!