Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Programming Languages 12th International Symposium Dbpl 2009 Lyon France August 2009 Proceedings Lncs 5708

Authors: Philippa Gardner ,Floris Geerts

2009th Edition

3642037925, 978-3642037924

More Books

Students also viewed these Databases questions

Question

. 3373. Calculate the pH of 0.001 N HCl solution

Answered: 1 week ago