C Programming Tutorial

 
 
 
 

Image

# include <stdio.h>
# include </usr/include/sys/types.h>
# include </usr/include/sys/shm.h>
# include </usr/include/sys/sem.h>
# include </usr/include/sys/ipc.h>
# include \"forkjoin.h\"
# include \"sharedlib.h\"
# include \"spinlock.h\"

int main()
{
    int arr1[10][10];  // Matrix Array
    int arr2[10];      // Vector Array - Singular Column Matrix

    int *ansArr;       // Multipication Answer Stored Here
               // It need to be shared

    int shmid;       // For ansArr

    int r1,c1;         // Number of Rows and Columns of First Matrix
    int r2,c2=1;       // Number of Rows and Columns of Second Matrix

    int iCount,jCount;

    int id;            // Stores Process ID
    int nproc;         // Number of Processes

    int sum;

    printf(\"Enter Number of Rows of First Matrix :\");
    scanf(\"%d\",&r1);

    printf(\"Enter Number of Columns of First Matrix :\");
    scanf(\"%d\",&c1);

    printf(\"Enter Number of Rows of Second Matrix :\");
    scanf(\"%d\",&r2);


    if(c1!=r2)
    {
        printf(\"Matrix Multipication is not Possible ...\");
        exit(1);
    }



    // Initialize an Array        
    printf(\"\\n\\nEnter Values for Matrix ...\\n\");
    for(iCount=0;iCount<r1;iCount++)
    {
        for(jCount=0;jCount<c1;jCount++)
        {
            printf(\"Enter Value for Arr1[%d][%d] :\",iCount,jCount);
            scanf(\"%d\",&arr1[iCount][jCount]);
        }
    }    
    printf(\"\\n\\nEnter Values for Vector Matrix ...\\n\");
    for(iCount=0;iCount<r2;iCount++)
    {
        printf(\"Enter Value for Arr2[%d] :\",iCount);
        scanf(\"%d\",&arr2[iCount]);
    }

    ansArr=(int*)sshared(sizeof(int)*r1,&shmid);

    nproc=r1;    // Processes for Each Row Multipication

    id=process_fork(nproc);
    sum=0;
    for(iCount=0;iCount<c1;iCount++)
    {
        sum+=(arr1[id][iCount] * arr2[iCount]);
    }
    ansArr[id]=sum;    
    process_join(nproc,id);

    printf(\"Array 1\\n\");

    for(iCount=0;iCount<r1;iCount++)
    {
        for(jCount=0;jCount<c1;jCount++)
        {
            printf(\"%d\\t\",arr1[iCount][jCount]);
        }
        printf(\"\\n\");
    }

    printf(\"Array 2 (Vector Matrix) \\n\");
    for(iCount=0;iCount<r1;iCount++)
    {
        printf(\"%d\\n\",arr2[iCount]);
    }


    printf(\"Matrix-Vector Multipication \\n\");
    for(iCount=0;iCount<r1;iCount++)
    {
        printf(\"%d\\n\",ansArr[iCount]);
    }


    cleanup_memory(&shmid);    

    return 0;
}

 
 
Didn't find what you were looking for? Find more on Program of Matrix-vector multiplication