You are on page 1of 2

#include <stdio.

h>
#include <stdlib.h>
int main()
{
int i, j, k, row_m, col_m, row_v;
int **M, *V, *R;
//Reading size of Matrix
printf("Enter row and column size of the Matrix \n");
scanf("%d%d", &row_m, &col_m);
//Reading size of Vector
printf("Enter row size of the Vector \n");
scanf("%d", &row_v);
//Checking for Multiplicability
if(col_m != row_v)
{
printf("Matrix column size and vector row size should match.\nRecompile the code with proper
input values\n");
exit(1);
}
//Allocating dynamic memory
M = (int **)malloc(sizeof(int)*row_m);
for(i=0; i<row_m; i++)
M[i] = (int *)malloc(sizeof(int)*col_m);

V = (int *)malloc(sizeof(int)*row_v);
R = (int *)malloc(sizeof(int)*row_v);
//Reading the elements of Matrix and Vector
printf("Enter the elements of matrix\n");
for(i=0;i<row_m;i++)
{
for(j=0;j<col_m;j++)
scanf("%d", &M[i][j]);
}
printf("Enter the elements of vector\n");
for(i=0;i<row_v;i++)
{
scanf("%d", &V[i]);
}
//Printing the values of Matrix and Vector before Multiplication
printf("\n******** The entered elements of matrix are ********\n");
for(i=0;i<row_m;i++)
{
for(j=0;j<col_m;j++)
printf("%d\t", M[i][j]);
printf("\n");
}
printf("\n******** The entered elements of vector are ********\n");
for(i=0;i<row_v;i++)
{
printf("%d\t", V[i]);
}
printf("\n******** Before Multiplication elements of resultant vector are ********\n");
for(i=0;i<row_v;i++)
{
R[i] = 0;
printf("%d\t", R[i]);
}
//Applying Parallel Matrix and Vector Multiplication
#pragma omp parallel num_threads(4)
{
#pragma omp for private(i,j)
for(i=0; i<row_m; i++)
{
R[i] = 0;
for(j=0; j<col_m; j++)
{
R[i] += M[i][j] * V[j];
}
}
}
//Printing the Multiplied Resultant Vector
printf("\n******** After Multiplication elements of resultant vector are ********\n");
for(i=0;i<row_m;i++)
{
printf("%d\t", R[i]);
}

return 0;
}

Output:

Enter row and column size of the Matrix


2
2
Enter row size of the Vector
2
Enter the elements of matrix
1
2
3
4
Enter the elements of vector
1
2

******** The entered elements of matrix are ********


1 2
3 4

******** The entered elements of vector are ********


1 2
******** Before Multiplication elements of resultant vector are ********
0 0
******** After Multiplication elements of resultant vector are ********
5 11
Process returned 0 (0x0) execution time : 10.473 s
Press any key to continue.

You might also like