Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Expert I need complete working code Output By eliminating the errors. This Below Code written in c language has Some errors. The Expert should eliminate

Expert I need complete working code Output By eliminating the errors. This Below Code written in c language has Some errors.

The Expert should eliminate all these errors and provide me the working code but without any errors .

I need To run this on gcc compiler.

#include #include #include // Value depend on System core #define CORE 3 // Maximum matrix size #define MAX 3 // Maximum threads is equal to total core of system pthread_t thread[CORE * 2]; int mat_A[MAX][MAX], mat_B[MAX][MAX], sum[MAX][MAX], sub[MAX][MAX] , mult[MAX][MAX]; // Addition of a Matrix void* addition(void* arg) { int i, j; int core = (int)arg; // Each thread computes 1/3th of matrix addition for (i = core * MAX / 3; i < (core + 1) * MAX / 3; i++) { for (j = 0; j < MAX; j++) { // Compute Sum Row wise sum[i][j] = mat_A[i][j] + mat_B[i][j]; } } } // Subtraction of a Matrix void* subtraction(void* arg) { int i, j; int core = (int)arg; // Each thread computes 1/3th of matrix subtraction for (i = core * MAX / 3; i < (core + 1) * MAX / 3; i++) { for (j = 0; j < MAX; j++) { // Compute Subtract row wise sub[i][j] = mat_A[i][j] - mat_B[i][j]; } } }

// Multiplication of a Matrix void* multiplication(void* arg) { int i, j ,sum ,k; int core = (int)arg; // Each thread computes 1/3th of matrix addition for (i = core * MAX / 3; i < (core + 1) * MAX / 3; i++) { for (j = 0; j < MAX; j++) { sum=0; for (k=0 ; k< MAX; k++ ) sum=sum + mat_A[i][k] * mat_B[k][j]; mult[i][j] = sum; } } }

// Driver Code int main() { int i, j, step = 0; // Generating random values in mat_A and mat_B for (i = 0; i < MAX; i++) { for (j = 0; j < MAX; j++) { mat_A[i][j] = rand() % 10; mat_B[i][j] = rand() % 10; } } // Displaying mat_A printf(" Matrix A: "); for (i = 0; i < MAX; i++) { for (j = 0; j < MAX; j++) { printf("%d ", mat_A[i][j]); } printf(" "); } // Displaying mat_B printf(" Matrix B: "); for (i = 0; i < MAX; i++) { for (j = 0; j < MAX; j++) { printf("%d ", mat_B[i][j]); } printf(" "); } // Creating threads equal // to core size and compute matrix row for (i = 0; i < CORE; i++) { pthread_create(&thread[i], NULL, &addition, (void*)step); pthread_create(&thread[i + CORE], NULL, &subtraction, (void*)step); pthread_create(&thread[i], NULL, &multiplication, (void*)step); step++; } // Waiting for join threads after compute for (i = 0; i < CORE * 2; i++) { pthread_join(thread[i], NULL); } // Display Addition of mat_A and mat_B printf(" Sum of Matrix A and B: "); for (i = 0; i < MAX; i++) { for (j = 0; j < MAX; j++) { printf("%d ", sum[i][j]); } printf(" "); } // Display Subtraction of mat_A and mat_B printf(" Subtraction of Matrix A and B: "); for (i = 0; i < MAX; i++) { for (j = 0; j < MAX; j++) { printf("%d ", sub[i][j]); } printf(" "); } // Display Multiplication of mat_A and mat_B printf(" Multiplication of Matrix A and B: "); for (i = 0; i < MAX; i++) { for (j = 0; j < MAX; j++) { printf("%d ", mult[i][j]); } printf(" "); } return 0; }

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

Step: 3

blur-text-image

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

Datacasting How To Stream Databases Over The Internet

Authors: Jessica Keyes

1st Edition

007034678X, 978-0070346789

More Books

Students also viewed these Databases questions

Question

In an Excel Pivot Table, how is a Fact/Measure Column repeated?

Answered: 1 week ago