Question: This is C++ program of calculating matrix . i computed each number in the output matrix with a separate thread . Can you try to
This is C++ program of calculating matrix .
i computed each number in the output matrix with a separate thread .
Can you try to correct the thread lines without deleting them ..
#include
#define Nthreads 10
void* func(void* ) {
// detach the current thread // from the calling thread pthread_detach(pthread_self()); pthread_t thread1,thread2; char *message1 = "thread 1"; char *message2 = "thread 2"; int iret1, iret2; printf("Inside the thread "); // exit the current thread pthread_exit(NULL); }
void fun() { pthread_t ptid; pthread_create(&ptid, NULL, &func, NULL); iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1); iret2 = pthread_create( &thread1, NULL, print_message_function, (void*) message2); pthread_join( thread1, NULL); pthread_join( thread2, NULL); pthread_t thread_id(Nthreads); int i, j; for (i=0; i < Nthreads; i++) { pthread_create( &thread_id[i], NULL, thread_function, NULL ); } for (j=0; j < Nthreads; j++) pthread_join ( thread_id[j], NULL); printf("final counter value: %d ", counter); printf("thread 1 returns: %d " ,iret1); printf("thread 2 returns: %d ", iret2); exit(0); // Declare variables int c, d, p, q, m, n, k, tot = 0; int fst[10][10], sec[10][10], mul[10][10], transpose[10][10];
printf(" Please insert the number of rows and columns for first matrix "); scanf("%d%d", &m, &n); // Read in the elements of the first matrix printf(" Insert your matrix elements : "); for (c = 0; c < m; c++) for (d = 0; d < n; d++) scanf("%d", &fst[c][d]); // Read in the number of rows and columns for the second matrix printf(" Please insert the number of rows and columns for second matrix "); scanf(" %d %d", &p, &q); // Check if the matrices can be multiplied if (n != p) printf(" Your given matrices cannot be multiplied with each other. "); else { // Read in the elements of the second matrix printf(" Insert your elements for second matrix "); for (c = 0; c < p; c++) for (d = 0; d < q; d++) scanf("%d", &sec[c][d]); // Multiply the matrices for (c = 0; c < m; c++) { for (d = 0; d < q; d++) { for (k = 0; k < p; k++) { tot = tot + fst[c][k] * sec[k][d]; } mul[c][d] = tot; tot = 0; } } // Print the result of the matrix multiplication printf(" The result of matrix multiplication or product of the matrices is: "); for (c = 0; c < m; c++) { for (d = 0; d < q; d++) printf("%d \t", mul[c][d]); printf(" "); } // Compute the transpose of the product matrix int r = q, c = m; for (int i = 0; i < r; ++i) for (int j = 0; j < c; ++j) { transpose[j][i] = mul[i][j]; } // Print the transpose of the product matrix printf("The transpose of the product matrix is: "); for (int i = 0; i < c; ++i) { for (int j = 0; j < r; ++j) printf("%d \t", transpose[i][j]); printf(" "); } } pthread_exit(NULL); }
int main(void) { fun(); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
