Code Competitions

Coding competition

Sunday, 12 May 2013

Program to perform matrix operations.

#include<stdio.h>
#include<conio.h>
void input(int [][10],int,int);
void output(int [][10],int,int);
void menu();
void add(int [][10],int [][10],int,int);
void sub(int [][10],int [][10],int,int);
void transpose(int [][10],int m,int n);
void add_diag(int [][10],int m,int n);
void mul(int a[][10],int b[][10],int m,int n,int p,int q);
void rows_add(int a[][10],int m,int n);
void add_non_diag(int [][10],int m,int n);
void columns_add(int [][10],int m,int n);
void main()
{
    int a[10][10],b[10][10],m,n,c[10][10],choice,p,q;
    clrscr();
    do
    {
        clrscr();
        menu();
        printf("\n\n\tEnter your choice : ");
        scanf("%d",&choice);
        switch(choice)
        {
            case 1:
                printf("Enter the size of a matrix (R x C) : ");
                scanf("%d%d",&m,&n);
                printf("enter the elements of matrix A");
                input(a,m,n);
                printf("enter the elements of matrix B");
                input(b,m,n);
                add(a,b,m,n);
                break;
            case 2:
                printf("Enter the size of a matrix");
                scanf("%d%d",&m,&n);
                printf("enter the elements of matrix A");
                input(a,m,n);
                printf("enter the elements of matrix B");
                input(b,m,n);
                sub(a,b,m,n);
                break;
            case 3:
                printf("Enter size of matrix");
                scanf("%d%d",&m,&n);
                printf("Enter elements of matrix A");
                input(a,m,n);
                transpose(a,m,n);
                break;
            case 4:
                printf("Enter size of matrix A");
                scanf("%d%d",&m,&n);
                if(m==n)
                {
                    printf("Enter elements of matrix A");
                    input(a,m,n);
                    add_diag(a,m,n);
                }
                else
                {
                    printf("NOT APPLICABLE");
                }
                    break;
            case 5:
                printf("Enter the size of matrix A:");
                scanf("%d%d",&m,&n);
                printf("Enter the size of matrix B:");
                scanf("%d%d",&p,&q);
                printf("Enter the Elements of matrix A:");
                input(a,m,n);
                printf("Enter the elements of matrix B:");
                input(b,p,q);
                mul(a,b,m,n,p,q);
                break;
            case 6:
                printf("Enter size of matrix");
                scanf("%d%d",&m,&n);
                printf("Enter A's elements");
                input(a,m,n);
                rows_add(a,m,n);
                break;
            case 7:
                printf("enter size of matrix");
                scanf("%d%d",&m,&n);
                printf("Enter elements of matrix");
                input(a,m,n);
                add_non_diag(a,m,n);
                break;
            case 8:
                printf("enter size of matrix");
                scanf("%d%d",&m,&n);
                printf("Enter elements");
                input(a,m,n);
                columns_add(a,m,n);
                break;
            default:
                printf("Enter the Valid option (1...8)");
        }
        getch();
    }
    while(choice<9);
}

void input(int a[][10],int m,int n)
{
    int i,j;
    printf("\n");
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
        {
            scanf("%d",&a[i][j]);
            printf("\t");
        }
        printf("\n");
    }
}
void output(int a[][10],int m,int n)
{
    int i,j;
    printf("Result \n");
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
        {
            printf("%d\t",a[i][j]);
        }
        printf("\n");

    }
}
void menu()
{
    printf("\t\t\t\tMenu\n");
    printf("\t\t1.) Addition of two matrices\n");
    printf("\t\t2.) Subtraction of two matrices\n");
    printf("\t\t3.) Transpose of matrices\n");
    printf("\t\t4.)    Addition of Diagonal elements\n");
    printf("\t\t5.) Multiplication of two matrices\n");
    printf("\t\t6.) Addition of rows\n");
    printf("\t\t7.) Addition of non diagonal elements\n");
    printf("\t\t8.) Addition of columns\n");
}

void add(int a[][10],int b[][10],int m,int n)
{
    int c[10][10],i,j;
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
        {
            c[i][j]=(a[i][j]+b[i][j]);
        }
    }
    output(c,m,n);

}

void sub(int a[][10],int b[][10],int m,int n)
{
    int c[10][10],i,j;
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
        {
            c[i][j]=(a[i][j]-b[i][j]);
        }
    }
    output(c,m,n);
}
void transpose(int a[][10],int m,int n)
{
    int c[10][10],i,j;
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
        {
            c[i][j]=a[j][i];
        }
    }
    output(c,m,n);
}
void add_diag(int a[][10],int m,int n)
{
    int c=0;
    int i,j;

        for(i=0;i<m;i++)
        {
            for(j=0;j<n;j++)
            {
                if(i==j)
                {
                    c=c+a[i][j];
                }
            }
        }
      printf("result is : %d",c);
}
void mul(int a[][10],int b[][10],int m,int n,int p,int q)
{
    int i,j,k,c[10][10];
    if(n==p)
    {
        for(i=0;i<m;i++)
        {
            for(j=0;j<q;j++)
            {
                c[i][j]=0;
                for(k=0;k<n;k++)
                {
                    c[i][j]=c[i][j]+a[i][k]*b[k][j];
                }
            }
        }
    output(c,m,n);
    }
    else
    {
        printf("can't applicable");
    }
}
void rows_add(int a[][10],int m,int n)
{
    int i,j,r1;
    for(i=0;i<m;i++)
    {
        r1=0;
        for(j=0;j<n;j++)
        {
            r1=r1+a[i][j];
        }
        printf("r1=%d",r1);
    }
}
void add_non_diag(int a[][10],int m,int n)
{
        int c=0;
    int i,j;

        for(i=0;i<m;i++)
        {
            for(j=0;j<n;j++)
            {
                if(i!=j)
                {
                    c=c+a[i][j];
                }
            }
        }
      printf("result is : %d",c);
}
void columns_add(int a[][10],int m,int n)
{
    int i,j,c1;
    for(i=0;i<m;i++)
    {
        c1=0;
        for(j=0;j<n;j++)
        {
            c1=c1+a[j][i];
           //    break;
        }
           //    c1=c1+a[i][j];
        printf("c=%d",c1);
    }
}

No comments:

Post a Comment