C Program For Matrix Collection

#include<stdio.h>
int main()
{
    int a[20][20],b[20][20],C[20][20],i,j,k,r,c,r2,c2,cho;
    char c1;
    while(1)
    {
        printf("\n**********ENTER ANY ON OF THE FOLLOWING OPERATION***********\n");  
        printf("1. product of given two matrices\n");
        printf("2. sum of the given two matrices\n");
        printf("3. difference of the given two matrices\n");
        printf("4. transpose of given matrix\n");
        scanf("%d",&cho);
        switch(cho)
        {
            case 1:printf("\nenter the number of rows and number of coloumns of matrix A\n");
                scanf("%d%d",&r,&c);
                printf("\nenter the number of rows and number of coloumns of matrix B\n");
                scanf("%d%d",&r2,&c2);
                if(c!=r2)
                {
                    printf("\nno.of rows of A and no.of coloumns of B should be equal\n");
                    break;
                }
                printf("\nenter elements into a matrix A\n");
                for(i=0;i<r;i++)
                    for(j=0;j<c;j++)
                        scanf("%d",&a[i][j]);
                
                printf("\nenter elements into a matrix B\n");
                for(i=0;i<r2;i++)
                    for(j=0;j<c2;j++)
                        scanf("%d",&b[i][j]);
                    
                printf("\nthe matrices you entered is\n");
                for(i=0;i<r;i++)
                {
                    for(j=0;j<c;j++)
                        printf("%d   ",a[i][j]);
                    printf("\n");
                }
                printf("\n\n\n");
                for(i=0;i<r2;i++)
                {
                    for(j=0;j<c2;j++)
                        printf("%d   ",b[i][j]);
                    printf("\n");
                }
                printf("\nproduct of given two matrices is\n");
                for(i=0;i<r;i++)
                {
                    for(j=0;j<c2;j++)
                    {
                        C[i][j] = 0;
                        for(k = 0;k<r2;k++)
                        {
                            C[i][j] = C[i][j] + a[i][k] * b[k][j];
                        }
                    }
                }   
                for(i=0;i<r;i++)
                {
                    for(j=0;j<c2;j++)
                    {
                        printf("%d  ",C[i][j]);
                    }
                    printf("\n");
                }
               
                break;
            case 2:
                printf("\nenter the number of rows and number of coloumns of matrix A\n");
                scanf("%d%d",&r,&c);
                printf("\nenter the number of rows and number of coloumns of matrix B\n");
                scanf("%d%d",&r2,&c2);
                if(r!=r2||c!=c2)
                {
                    printf("\nno.of rows and no.of coloumns should be equal\n");
                    break;
                }
                printf("\nenter elements into a matrix A\n");
                for(i=0;i<r;i++)
                    for(j=0;j<c;j++)
                        scanf("%d",&a[i][j]);
                
                printf("\nenter elements into a matrix B\n");
                for(i=0;i<r2;i++)
                    for(j=0;j<c2;j++)
                        scanf("%d",&b[i][j]);
                    
                printf("\nthe matrices you entered is\n");
                for(i=0;i<r;i++)
                {
                    for(j=0;j<c;j++)
                        printf("%d   ",a[i][j]);
                    printf("\n");
                }
                printf("\n\n\n");
                for(i=0;i<r2;i++)
                {
                    for(j=0;j<c2;j++)
                        printf("%d   ",b[i][j]);
                    printf("\n");
                }
                printf("\nsum of given two matrices is\n");
                for(i=0;i<r;i++)
                    for(j=0;j<c;j++)
                        C[i][j] = a[i][j] + b[i][j];
                   
                for(i=0;i<r;i++)
                {
                    for(j=0;j<c;j++)
                    {
                        printf("%d  ",C[i][j]);
                    }
                    printf("\n");
                }
                break;
            case 3:
                printf("\nenter the number of rows and number of coloumns of matrix A\n");
                scanf("%d%d",&r,&c);
                printf("\nenter the number of rows and number of coloumns of matrix B\n");
                scanf("%d%d",&r2,&c2);
                if(r!=r2||c!=c2)
                {
                    printf("\nno.of rows and no.of coloumns should be equal\n");
                    break;
                }
                printf("\nenter elements into a matrix A\n");
                for(i=0;i<r;i++)
                    for(j=0;j<c;j++)
                        scanf("%d",&a[i][j]);
                
                printf("\nenter elements into a matrix B\n");
                for(i=0;i<r2;i++)
                    for(j=0;j<c2;j++)
                        scanf("%d",&b[i][j]);
                   
                printf("\nthe matrices you entered is\n");
                for(i=0;i<r;i++)
                {
                    for(j=0;j<c;j++)
                        printf("%d   ",a[i][j]);
                    printf("\n");
                }
                printf("\n\n\n");
                for(i=0;i<r2;i++)
                {
                    for(j=0;j<c2;j++)
                        printf("%d   ",b[i][j]);
                    printf("\n");
                }
                printf("\ndifference of given two matrices is\n");
                for(i=0;i<r;i++)
                    for(j=0;j<c;j++)
                        C[i][j] = a[i][j] - b[i][j];
                   
                for(i=0;i<r;i++)
                {
                    for(j=0;j<c;j++)
                    {
                        printf("%d  ",C[i][j]);
                    }
                    printf("\n");
                }
                break;
            case 4:
                printf("\nenter the number of rows and number of coloumns\n");
                scanf("%d%d",&r,&c);
                printf("\nenter elements into a matrix\n");
                for(i=0;i<r;i++)
                    for(j=0;j<c;j++)
                        scanf("%d",&a[i][j]);
                   
                printf("\nthe matrix you entered is\n");
                for(i=0;i<r;i++)
                {
                    for(j=0;j<c;j++)
                        printf("%d   ",a[i][j]);
                    printf("\n");
                }
               
                printf("\ntranspose of given matrix is\n");
                for(i=0;i<r;i++)
                    for(j=0;j<c;j++)
                        b[j][i] = a[i][j];
                for(i=0;i<c;i++)
                {
                    for(j=0;j<r;j++)
                    {
                        printf("%d  ",b[i][j]);
                    }
                    printf("\n");
                }
                break;
            default:
                printf("\nyou entered wrong option\n");
        }
        printf("\nDO YOU WANT CONTINUE y/n\n");
        fflush(stdin);
        scanf("%c",&c1);
        if(c1 == 'y' || c1 =='Y')
            continue;
        else
            break;
    }
    return(0);
}


Bhanu Namikaze

Bhanu Namikaze is an Ethical Hacker, Security Analyst, Blogger, Web Developer and a Mechanical Engineer. He Enjoys writing articles, Blogging, Debugging Errors and Capture the Flags. Enjoy Learning; There is Nothing Like Absolute Defeat - Try and try until you Succeed.

No comments:

Post a Comment