Below is the c program that I wrote that multiplies two matrixs and puts it in a
ID: 3589743 • Letter: B
Question
Below is the c program that I wrote that multiplies two matrixs and puts it in a new matrix. Can someone help me to impliment this program with Pthread please?
Since this is not a very huge program to impliment it with pthread, we can use only 8 - 10 pthread. Thank you in advance.
#include<stdio.h>
#include<time.h>
#include<unistd.h>
#include<stdlib.h>
#define A 800
#define B 800
#define C 800
int main()
{
//int row1 = 300, col1 = 300, row2 = 300, col2 = 300;
int i = 0, j = 0, k = 0;
int matxa[A][B], matxb[B][A], matxc[A][C];
clock_t t1,t2;
/*
printf("Enter the number of rows and columns for the first matrix: ");
printf("Number of rows: ");
scanf("%d", &row1);
printf("Number of columns: ");
scanf("%d", &col1);
srand(time(NULL));
printf("Enter the number of rows and columns for the second matrix: ");
printf("Number of rows: ");
scanf("%d", &row2);
printf("Number of columns: ");
scanf("%d", &col2);
if(row1 != col2 || col2!= row1)
{
printf("error!, the rows and column doesn't match");
return 0;
}
printf("First Array: ");
*/
srand(time(NULL));
t1 = clock();
for(i = 0; i<A;i++)
{
for(j = 0; j<B; j++)
{
matxa[i][j] = rand() % 100;
printf("%d ", matxa[i][j]);
}
printf(" ");
}
printf("Second Array: ");
for(i = 0; i<B;i++)
{
for(j = 0; j<C; j++)
{
matxb[i][j] = rand() % 10;
printf("%d ", matxb[i][j]);
}
printf(" ");
}
for(i = 0; i < A; i++)
{
for(j = 0; j < B; j++)
{
matxc[i][j] = 0;
}
}
for(i = 0; i < A; i++)
{
for(j = 0; j < B; j++)
{
for(k = 0; k < B; k++)
{
matxc[i][j] += matxa[i][k] * matxb[k][j];
}
}
}
printf("The multiplication of the given Matrixes is: ");
for(i = 0; i < A; i++)
{
for(j = 0; j < B; j++)
{
printf("%d ", matxc[i][j]);
}
printf(" ");
}
t2 = clock();
printf("Time passses: %f ",(t2-t1)/(float)(CLOCKS_PER_SEC));
return 0;
}
Explanation / Answer
# include <stdio.h>
# include <pthread.h>
#define A 800
#define B 800
#define C 800
int row1,col1,row2,col2;
void *thread_Multiply_Matrix(void *);
int matxa[A][B], matxb[B][A], matxc[A][C];
int main()
{
pthread_t tid;
int i,j,k;
printf("Enter the number of rows and columns for the first matrix: ");
printf("Number of rows: ");
scanf("%d", &row1);
printf("Number of columns: ");
scanf("%d", &col1);
printf("Enter the number of rows and columns for the second matrix: ");
printf("Number of rows: ");
scanf("%d", &row2);
printf("Number of columns: ");
scanf("%d", &col2);
if(row1 != col2 || col2!= row1)
{
printf("error!, the rows and column doesn't match");
return 0;
}
else
{
for(i=0;i<row1;i=i+2)
{
for(j=0;j<col2;j=j+2)
{
matxc[i][j]=0;
}
}
pthread_create(&tid,NULL,thread_Multiply_Matrix,NULL);
for(i=0;i<row1;i=i+2)
{
for(j=0;j<col2;j++)
{
for(k=0;k<col1;k++)
{
matxc[i][j]+=matxa[i][k] * matxb[k][j];
}
}
}
pthread_join(tid,NULL);
}
printf(" Matrix 1 ");
for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
{
printf("%d ",matxa[i][j]);
}
printf(" ");
}
printf(" Matrix 2 ");
for(i=0;i<row2;i++)
{
for(j=0;j<col2;j++)
{
printf("%d ",matxb[i][j]);
}
printf(" ");
}
printf(" Multipication of Matrix ... ");
for(i=0;i<row1;i++)
{
for(j=0;j<col2;j++)
{
printf("%d ",matxc[i][j]);
}
printf(" ");
}
return 0;
}
void *thread_Multiply_Matrix(void *para)
{
int i,j,k;
for(i=1;i<row1;i=i+2)
{
for(j=0;j<col2;j++)
{
for(k=0;k<col1;k++)
{
matxc[i][j]+=matxa[i][k] * matxb[k][j];
}
}
}
printf("Thread Completed ...");
pthread_exit(NULL);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.