Code Competitions

Coding competition

Wednesday, 29 May 2013

program to perform matrix operations using pointers

#include<iostream.h>
#include<stdio.h>
#include<process.h>
#include<conio.h>
void output(int m,int n,int a[][20])
{
    int i,j;
    cout<<"\n";
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
        {
            cout<<'\t'<<a[i][j];
        }
        cout<<"\n";
    }

}
void read(int *m1,int *n1,int *m2,int *n2,int a[][20],int b[][20])
{
    int i,j;
    cout<<"\nthe matrix A  ";
    cout<<"\nenter the number rows in A matrix  ";
    cin>>*m1;
    cout<<"\nenter the number coloumns in A matrix  ";
    cin>>*n1;
    cout<<"\nenter the elements of matrix A ";
    for(i=0;i<*m1;i++)
    {
        for(j=0;j<*n1;j++)
        {
            scanf("%d",(*(a+i)+j));
        }
    }
    cout<<"\nthe matrix B  ";
    cout<<"\nenter the number rows in B matrix  ";
    cin>>*m2;
    cout<<"\nenter the number coloumns in B matrix  ";
    cin>>*n2;
    for(i=0;i<*m2;i++)
    {
        for(j=0;j<*n2;j++)
        {
            cin>>b[i][j];
        }
    }
}
void add(int m,int n,int a[][20],int b[][20])
{
    int i,j,c[20][20];
    cout<<"\n the resultant matrix is \n";
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
        {
            c[i][j]=a[i][j]+b[i][j];
            cout<<'\t'<<c[i][j];
        }
        cout<<"\n";
    }
}
void mult(int m1,int n1,int m2,int n2,int a[][20],int b[][20])
{
    int c[20][20]={0},i,j,k;
    for(i=0;i<m1;i++)
    {
        for(j=0;j<n2;j++)
        {
            for(k=0;k<m2;k++)
            {
                c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
            }
        }
    }
    for(i=0;i<m1;i++)
    {
        for(j=0;j<n2;j++)
        {
            cout<<'\t'<<c[i][j];
        }
        cout<<"\n";
    }
}
int  menu()
{
    int ch;
    cout<<"\n1. enter 2 matrices ";
    cout<<"\n2. addition of 2 matrices";
    cout<<"\n3. subtraction of 2 matrices";
    cout<<"\n4. multiplication of 2 matrices";
    cout<<"\n5. EXIT";
    cin>>ch;
    return(ch);
    //cout<<"\naddition of 2 matrices";
}
void sub(int m,int n,int a[][20],int b[][20])
{
    int c[20][20];
    cout<<"\n the resultant matrix is \n";
    for(int i=0;i<m;i++)
    {
        for(int j=0;j<n;j++)
        {
            c[i][j]=a[i][j]-b[i][j];
            cout<<'\t'<<c[i][j];
        }
        cout<<"\n";
    }
}
void main()
{
    int m1,n1,m2,n2,i,j,a[20][20],b[20][20],c[20][20];
    clrscr();
    int num;
    while(1)
    {
        clrscr();
        num=menu();
        switch(num)
        {
            case 1:
                read(&m1,&n1,&m2,&n2,a,b);
                getch();
                break;
            case 2:
                cout<<"\nthe matrix A is \t";
                output(m1,n1,a);
                cout<<"\nthe matrix B is \t";
                output(m2,n2,b);
                if((m1==m2)&&(n1==n2))
                {
                    add(m1,n1,a,b);

                }
                else
                cout<<"\norder should be same for addition ";
                getch();
                break;
            case 3:
                cout<<"\nthe matrix A is \t";
                output(m1,n1,a);
                cout<<"\nthe matrix B is \t";
                output(m2,n2,b);
                cout<<"\n1.subtraction by assuming A as subtrahend and B as minuend";
                cout<<"\n2.subtraction by assuming B as subtrahend and A as minuend";
                cin>>num;
                if(num==1)
                {
                sub(m1,n1,a,b);
                }
                else if(num==2)
                {
                sub(m1,n1,b,a);
                }
                getch();
                break;
            case 4:
                cout<<"\nthe matrix A is \t";
                output(m1,n1,a);
                cout<<"\nthe matrix B is \t";
                output(m2,n2,b);
                if(n1==m2)
                {
                    cout<<"\nthe resul of multiplication is \n";
                    mult(m1,n1,m2,n2,a,b);
                }
                else
                {
                    cout<<"\nmultiplication not possible ";
                }
                getch();
                break;
            case 5:

                exit(0);
        }
    }
    //cout<<"\nthe matrix A is \t";
    //output(m1,n1,a);
  //    cout<<"\nthe matrix B is \t";
   //    output(m2,n2,b);
    //    getch();
}


No comments:

Post a Comment