Skip to content

Program to calculate Addition of 3×3 Matrices using loop splitting

Aim: Program to calculate Addition of 3×3 Matrices using loop splitting

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:=>> %dn”,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(“%dt”,A[i][j]);
        }
        printf(“n”);
    }
    printf(“<:== Matrix B :=>>n”);
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            printf(“%dt”,B[i][j]);
        }
        printf(“n”);
    }
    printf(“<:== Resulting Matrix C :==>>n”);
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            printf(“%dt”,C[i][j]);
        }
        printf(“n”);
    }
    free_shm(id1);
    return 0;
}

Output:


Comment bellow for your Query and Feedback

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!