0% found this document useful (0 votes)
33 views6 pages

Threads

The document is a lab report submitted by Muhammad Yasir Hassan to Ma'am Rabia Arshad for Software Engineering Department at University of Engineering and Technology, Taxila. The lab task involves writing a C program to perform matrix addition, subtraction and multiplication using multithreading. The program defines matrices, threads for operations and performs input, calculation and output of results for addition, subtraction and multiplication based on matching dimensions of matrices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views6 pages

Threads

The document is a lab report submitted by Muhammad Yasir Hassan to Ma'am Rabia Arshad for Software Engineering Department at University of Engineering and Technology, Taxila. The lab task involves writing a C program to perform matrix addition, subtraction and multiplication using multithreading. The program defines matrices, threads for operations and performs input, calculation and output of results for addition, subtraction and multiplication based on matching dimensions of matrices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA

SOFTWARE ENGINEERING DEPARTMENT (SED)

Lab #14

SUBMITTED TO:

Ma’am Rabia Arshad

SUBMITTED BY:

Muhammad Yasir Hassan

ROLL NUMBER:

19-SE-31

SECTION:

Alpha (α)

DATE:

12-01-2022
Lab Task
Task 1:
Write a program for matrix addition, subtraction and multiplication using
multithreading.
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
void *Addition(void *ptr);
void *Subtraction(void *ptr);
void *Multiplication(void *ptr);

int A[10][10], B[10][10], C[10][10],r1,r2,c1,c2,i,j,k;


pthread_t thread1,thread2,thread3;
int main()
{
printf("\n Number of Rows (Matrix 1): ");
scanf("%d",&r1);
printf("\n Number of Columns (Matrix 1): ");
scanf("%d",&c1);
printf("\n Enter the Elements (Matrix 1):\n\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("\t");
scanf("%d",&A[i][j]);
}
printf("\n");
}
printf("\n Number of Rows (Matrix 2): ");
scanf("%d",&r2);
printf("\n Number of Columns (Matrix 2): ");
scanf("%d",&c2);
printf("\n Enter the Elements (Matrix 2):\n\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf("\t");
scanf("%d",&B[i][j]);
}
printf("\n");
}
pthread_create(&thread1,NULL,Addition,NULL);
pthread_join(thread1,NULL);
printf("\n");
pthread_create(&thread2,NULL,Subtraction,NULL);
pthread_join(thread2,NULL);
printf("\n");
pthread_create(&thread3,NULL,Multiplication,NULL);
pthread_join(thread3,NULL);
printf("\n");
exit(0);
return 0;
}
void *Addition(void *ptr)
{
printf("\n\t\t\t Sum of Matrix 1 and Matrix 2:\n\n");
if(r1==r2 && c1==c2)
{
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
C[i][j] = A[i][j] + B[i][j];
printf("\t\t\t%d",C[i][j]);
}
printf("\n");
}
}
else
printf("\n Addition is Not Possible....");
}
void *Subtraction(void *ptr)
{
printf("\n\t\t\t Difference of Matrix 1 and Matrix 2:\n\n");
if(r1==r2 && c1==c2)
{
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
C[i][j] = A[i][j] - B[i][j];
printf("\t\t\t%d",C[i][j]);
}
printf("\n");
}
}
else
printf("\n Subtraction is Not Possible....");
}
void *Multiplication(void *ptr)
{
printf("\n\t\t\t Product of Matrix 1 and Matrix 2:\n\n");
if(c1==r2)
{
for(i=0;i<r1;i++)
for(j=0;j<c2;j++)
for(k=0;k<c1;k++)
C[i][j] += A[i][k] * B[k][j];

for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
printf("\t\t\t%d",C[i][j]);
}
printf("\n");
}
}
else
printf("\n Multiplication is Not Possible....");
}

You might also like