Aim: Program to calculate Addition of 3x3 Matrices using loop splitting
Source Code:
Output:
Comment bellow for your Query and Feedback
Source Code:
#include<stdio.h>
#include"shmlib.h"
#include "stdlib.h"
typedef int RowArray[3];
RowArray *C;
int main()
{
int A[3][3], B[3][3], id, id1,i,j;
C = (RowArray*)shared(sizeof(RowArray) * 3, &id1);
printf("<:=Enter the values in Array A:=>>\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Enter the value of A[%d][%d]:=>> ",i,j);
scanf("%d",&A[i][j]);
}
printf("\n");
}
printf("<:=Enter the values in Array B:=>>\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Enter the value of B[%d][%d]:=>> ",i,j);
scanf("%d",&B[i][j]);
}
printf("\n");
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
C[i][j] = 0;
}
}
id = create_process(2);
printf("Process:=>> %d\n",id);
for(i=id;i<3;i=i+3)
{
for(j=0;j<3;j++)
{
C[i][j] = A[i][j] + B[i][j];
}
}
join_process(3,id);
printf("<:== Matrix A :=>>\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",A[i][j]);
}
printf("\n");
}
printf("<:== Matrix B :=>>\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",B[i][j]);
}
printf("\n");
}
printf("<:== Resulting Matrix C :==>>\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",C[i][j]);
}
printf("\n");
}
free_shm(id1);
return 0;
}
#include"shmlib.h"
#include "stdlib.h"
typedef int RowArray[3];
RowArray *C;
int main()
{
int A[3][3], B[3][3], id, id1,i,j;
C = (RowArray*)shared(sizeof(RowArray) * 3, &id1);
printf("<:=Enter the values in Array A:=>>\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Enter the value of A[%d][%d]:=>> ",i,j);
scanf("%d",&A[i][j]);
}
printf("\n");
}
printf("<:=Enter the values in Array B:=>>\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Enter the value of B[%d][%d]:=>> ",i,j);
scanf("%d",&B[i][j]);
}
printf("\n");
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
C[i][j] = 0;
}
}
id = create_process(2);
printf("Process:=>> %d\n",id);
for(i=id;i<3;i=i+3)
{
for(j=0;j<3;j++)
{
C[i][j] = A[i][j] + B[i][j];
}
}
join_process(3,id);
printf("<:== Matrix A :=>>\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",A[i][j]);
}
printf("\n");
}
printf("<:== Matrix B :=>>\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",B[i][j]);
}
printf("\n");
}
printf("<:== Resulting Matrix C :==>>\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",C[i][j]);
}
printf("\n");
}
free_shm(id1);
return 0;
}
Output:
Comment bellow for your Query and Feedback
0 comments:
Confused? Feel free to ask
Post a Comment