MATRIX MULTIPLICATION

Q. Write a program that contains three functions, the first two will be encoder and decoder function used to decode and encode a matrix of integers.The encoder will simply swap the values of elements Aij and Aji. The decoder will unswapp the values,So for example A12 will be swapped with A21during encoding and back during decoding.Write the third function to multiply three matrices.

Now a data file will be in the following formate
<'E or D'><Nrows><Mcolumn><A11A12A13....ANrowsMcolumns>
The first character of the file will be either E or D indicating whether the data is encoded or decoded. The following integers indicate the number of rows and columns,followed by matrix data.

Write a program to read two encoded files (Say M1.dat M2.dat) and multiply them to generate a third encoded file (say M3.dat) in the exact same formatedescribed above.use command line argument. So to run it with the above three files I should type
./a.out M1.dat M2.dat M3.dat
Solution:-
test.c
#include <stdio.h>
#include<stdlib.h>
main()
{
    FILE *f1=fopen("M1.dat", "w");
    FILE *f2=fopen("M2.dat", "w");
    char c1,c2;int rc1[2],rc2[2],i,j,n1=0,n2=0;
    printf("enter character: D 'Decode' || E 'Encode':-");
    scanf("%c %c",&c1,&c2);
    printf("enter row and column of first matrix:\n");
    for(i=0;i<2;i++)
    {    printf("rc1[%d]=",i);
        scanf("%d",&rc1[i]);
    }
    printf("enter row and column of second matrix:\n");
    for(i=0;i<2;i++)
    {    printf("rc2[%d]=",i);
        scanf("%d",&rc2[i]);
    }
    int **a=calloc(sizeof(int *),rc1[0]);
    for(i=0;i<rc1[0];i++)
        a[i]=calloc(sizeof(int),rc1[1]);   
    int **b=calloc(sizeof(int *),rc2[0]);
    for(i=0;i<rc2[0];i++)
        b[i]=calloc(sizeof(int),rc2[1]);
    int size1=rc1[0]*rc1[1];
    int m1[size1];
    int size2=rc2[0]*rc2[1];
    int m2[size2];
    printf("\nEnter the first Matrix: \n");
    for(i=0;i<rc1[0];i++)
    {
        for(j=0;j<rc1[1];j++)
        {
            printf("a[%d][%d]:",i,j);
            scanf("%d",&a[i][j]);
            m1[n1]=a[i][j];
            n1++;
        }           
    }
    printf("\nEnter the 2nd Matrix: \n");
    for(i=0;i<rc2[0];i++)
    {
        for(j=0;j<rc2[1];j++)
        {
            printf("b[%d][%d]:",i,j);
            scanf("%d",&b[i][j]);
            m2[n2]=b[i][j];
            n2++;
        }           
    }
    fwrite(&c1, sizeof(char),1,f1);
    fwrite(rc1, sizeof(int),2,f1);
    fwrite(m1,sizeof(int), size1 ,f1);
    fwrite(&c2, sizeof(char),1,f2);
    fwrite(rc2, sizeof(int),2,f2);
    fwrite(m2,sizeof(int), size2 ,f2);
    fclose(f1);
    fclose(f2);
}
compile & run:
sujit@sujit-desktop:~/sunil$ cc test.c
sujit@sujit-desktop:~/sunil$ ./a.out
enter character: D 'Decode' || E 'Encode':-D E
enter row and column of first matrix:
rc1[0]=2
rc1[1]=2
enter row and column of second matrix:
rc2[0]=2
rc2[1]=2

Enter the first Matrix:
a[0][0]:1
a[0][1]:2
a[1][0]:2
a[1][1]:2

Enter the 2nd Matrix:
b[0][0]:3
b[0][1]:4
b[1][0]:3
b[1][1]:4

mm.c
#include<stdio.h>
#include<stdlib.h>
void mul(int **a,int **b,int rc1[2],int rc2[2],char str[]);
void encode(int **a,int rc1[2]);
void decode(int **a,int rc2[2]);
main(int argc, char *argv[])
{
    FILE *f1=fopen(argv[1],"r");
    FILE *f2=fopen(argv[2],"r");
    FILE *f3=fopen(argv[3],"w");
    char ch1,ch2;
    int rc1[2],rc2[2],k,i,j;
    fread(&ch1,sizeof(char),1,f1);
    fread(rc1,sizeof(int),2,f1);
    printf("Char=%c,Row=%d,Column=%d\n",ch1,rc1[0],rc1[1]);
    int size1=rc1[0]*rc1[1];
    int m1[size1];
    fread(m1,sizeof(int),size1,f1);
    int **m11=calloc(sizeof(int *),rc1[0]);
    for(i=0;i<rc1[0];i++)
        m11[i]=calloc(sizeof(int),rc1[1]);
    k=0;
    for(i=0;i<rc1[0];i++)
    {   
       
        for(j=0;j<rc1[1];j++)
        {
            m11[i][j]=m1[k];
            k++;
        }   
    }
   
   
    fread(&ch2,sizeof(char),1,f2);
    fread(rc2,sizeof(int),2,f2);
    printf("Char=%c,Row=%d,Column=%d\n",ch2,rc2[0],rc2[1]);
    int size2=rc2[0]*rc2[1];
    int m2[size2];
    fread(m2,sizeof(int),size2,f2);
    int **m22=calloc(sizeof(int *),rc2[0]);
    for(i=0;i<rc2[0];i++)
        m22[i]=calloc(sizeof(int),rc2[1]);
    k=0;
    for(i=0;i<rc2[0];i++)
    {   
       
        for(j=0;j<rc2[1];j++)
        {
            m22[i][j]=m2[k];
            k++;
        }   
    }
    if(ch1=='E')
    {
        decode(m11,rc1);
    }
    if(ch2=='E')
    {
        decode(m22,rc2);
    }
    mul(m11,m22,rc1,rc2,argv[3]);
}
void mul(int **a,int **b,int rc1[2],int rc2[2],char str[])
{
    int i,j,s,**c,l,rw=rc1[0],cl=rc2[1];char ch='E';
    int size3=rc1[0]*rc2[1],x[size3],k=0;
    FILE *f3=fopen(str,"w");
    if(rc1[1]!=rc2[0])
    {
        printf("\nMatrix multiplication is not possible\n");
    }
    c=calloc(sizeof(int *),rc1[0]);
    for(i=0;i<rc1[0];i++)
        c[i]=calloc(sizeof(int),rc2[1]);   
    for(i=0;i<rc1[0];i++)
        {           
            for(j=0;j<rc2[1];j++)
            {
                s=0;
                for(l=0;l<rc1[1];l++)
                {
                    s=s+a[i][l]*b[l][j];
                }
                c[i][j]=s;
                x[k]=c[i][j];
                k++;
            }               
            printf("\n");
        }
    int rc3[2];rc3[0]=rc1[0];rc3[1]=rc2[1];
    encode(c,rc3);
    printf("\nThe result Matrix: \n");
    for(i=0;i<rc1[0];i++)
        {
            for(j=0;j<rc2[1];j++)
            printf("%3d",c[i][j]);   
            printf("\n");       
        }
    fwrite(&ch, sizeof(char),1,f3);
    fwrite(rc3, sizeof(int),2,f3);
    fwrite(x, sizeof(int),size3,f3);
    fclose(f3);
}
void encode(int **a,int rc2[2])
{
    int k,i,j;
    for(i=0;i<rc2[0];i++)
    {
        for(j=0;j<rc2[1];j++)
        {
            if(i!=j && i<j)
            {
                k=a[j][i];
                a[j][i]=a[i][j];
                a[i][j]=k;
            }
        }
    }   
}
void decode(int **a,int rc2[2])
{
    int k,i,j;
    for(i=0;i<rc2[0];i++)
    {
        for(j=0;j<rc2[1];j++)
        {
            if(i!=j && i<j)
            {
                k=a[j][i];
                a[j][i]=a[i][j];
                a[i][j]=k;
            }
        }
    }   
}
sujit@sujit-desktop:~/sunil$ cc mm.c
sujit@sujit-desktop:~/sunil$ ./a.out M1.dat M2.dat M3.dat
Char=D,Row=2,Column=2
Char=E,Row=2,Column=2



The result Matrix:
 11 14
 11 14
sujit@sujit-desktop:~/sunil$

Popular posts from this blog

8 Bit Plane Slicing of an image in Image Processing

Code to upload multiple files simultaneously using JSP, Servlet .

STRING PALINDROME USING STACK AND QUEUE