Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Parallel Processing ProgramsRSS Feeds

Program of Matrix-vector multiplication

Posted By: Easy Tutor     Category: C Programming     Views: 6129

Write a program of Matrix-vector multiplication.

Code for Program of Matrix-vector multiplication in C Programming

# 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 Arrayint arr2[10];      // Vector Array - Singular Column Matrixint *ansArr;       // Multipication Answer Stored Here// It need to be sharedint shmid;       // For ansArrint r1,c1;         // Number of Rows and Columns of First Matrixint r2,c2=1;       // Number of Rows and Columns of Second Matrixint iCount,jCount;
    
    int id;            // Stores Process IDint nproc;         // Number of Processesint 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;
}
  
Share: 


Didn't find what you were looking for? Find more on Program of Matrix-vector multiplication Or get search suggestion and latest updates.

Easy Tutor
Easy Tutor author of Program of Matrix-vector multiplication is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!