Showing posts with label Matrix in C. Show all posts
Showing posts with label Matrix in C. Show all posts

Monday, September 12, 2011

matrix with row-wise sum

matrix with row-wise sum

#include<stdio.h>
#include<conio.h>
main()
{

    int a[5][5],m,n,i,j,sum[5];
    clrscr();
    printf("\n enter the no. of rows of the matrix:");
    scanf("%d",&m);
    printf("\n enter the no. of columns of the matrix:");
    scanf("%d",&n);
    for(i=0;i<m;i++)  /* scan the entire matrix  */
        for(j=0;j<n;j++)
        {
        printf("\n enter the  a[%d][%d]  element:",i+1,j+1);
        scanf("%d",&a[i][j]);
        }
    if(m==n)  /* check whether square matrix or not  */
        printf("\n The given matrix is a square matrix and ");
    else
        printf("\n The given matrix is not a square matrix and ");
    printf("\n The elements of the given matrix are:");

    for(i=0;i<m;i++)  /* finding row-wise sum */
    {
        sum[i]=0;
        for(j=0;j<n;j++)
              sum[i]=sum[i]+a[i][j];
     }
    for(i=0;i<m;i++)  /* print the matrix with row-sum  */
    {
    printf("\n");
        for(j=0;j<n;j++)
        printf("\t %d ",a[i][j]);
        printf("\t Row Sum=%d",sum[i]);
    }
    getch();
}

print the given matrix

print the given matrix

#include<stdio.h>
#include<conio.h>
main()
{

    int a[5][5],m,n,i,j;
    clrscr();
    printf("\n enter the no. of rows of the matrix:");
    scanf("%d",&m);
    printf("\n enter the no. of columns of the matrix:");
    scanf("%d",&n);
    for(i=0;i<m;i++)  /* scan the entire matrix  */
        for(j=0;j<n;j++)
        {
        printf("\n enter the  a[%d][%d]  element:",i+1,j+1);
        scanf("%d",&a[i][j]);
        }
    if(m==n)  /* check whether square matrix or not  */
        printf("\n The given matrix is a square matrix and ");
    else
        printf("\n The given matrix is not a square matrix and ");
    printf("\n The elements of the given matrix are:");
    for(i=0;i<m;i++)  /* print the matrix  */
    {
    printf("\n");
        for(j=0;j<n;j++)
        printf("\t %d",a[i][j]);
    }
    getch();
}

multiply two given matrix

multiply two given matrix
#include<stdio.h>
#include<conio.h>
main()
{

    int a[5][5],b[5][5],c[5][5],m,n,p,q,i,j,k;
    clrscr();
    printf("\n enter the no. of rows the matrix A:");
    scanf("%d",&m);
    printf("\n enter the no. of columns the matrix A:");
    scanf("%d",&n);
    printf("\n enter the no. of rows the matrix B:");
    scanf("%d",&p);
    printf("\n enter the no. of columns the matrix B:");
    scanf("%d",&q); /* check for the condition of multiplication  */
    if(n==p)
    {
        printf("\n The two matrices can be multiplied ");
        for(i=0;i<m;i++)     /* input matrix A */
            for(j=0;j<n;j++)
            {   
            printf("\n enter the  a[%d][%d]  element:",i+1,j+1);
            scanf("%d",&a[i][j]);
            }
            /* enter the matric B  */
        for(i=0;i<p;i++)
            for(j=0;j<q;j++)
            {
            printf("\n enter the  b[%d][%d]  element:",i+1,j+1);
            scanf("%d",&b[i][j]);
            }
        clrscr();
                /*  printing the matric A  */
    printf("\n The elements of the given matrix A are:");
    for(i=0;i<m;i++)
    {
    printf("\n");
        for(j=0;j<n;j++)
        printf("\t %d",a[i][j]);
    }
                /* printing the matric  B  */
    printf("\n The elements of the given matrix B are:");
    for(i=0;i<p;i++)
    {
    printf("\n");
        for(j=0;j<q;j++)
        printf("\t %d",b[i][j]);
    }

    for(i=0;i<m;i++)             /* multipling the two matrices */
        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];
        }
        /* printing the multiplication of the given matrices  */
    printf("\n The resultant matrix is of order %d*%d",m,q);
    printf("\n The multiplication  of the given matrices is:");
        for(i=0;i<m;i++)
        {
        printf("\n");
            for(j=0;j<q;j++)
                printf("\t %d",c[i][j]);
        }
    }
    else
        printf("\n The two matrices can not be multiplied ");
    getch();
}

Print greatest element of given matrix

greatest element of given matrix

#include<stdio.h>
#include<conio.h>
main()
{

    int a[5][5],m,n,i,j,big;
    clrscr();
    printf("\n enter the no. of rows of the matrix:");
    scanf("%d",&m);
    printf("\n enter the no. of columns of the matrix:");
    scanf("%d",&n);
    for(i=0;i<m;i++)    /* input the elements */
        for(j=0;j<n;j++)
        {
        printf("\n enter the  A[%d][%d]  element:",i+1,j+1);
        scanf("%d",&a[i][j]);
        }
    if(m==n) /* check whether square matrix or not */
        printf("\n The given matrix is a square matrix and ");
    else
        printf("\n The given matrix is not a square matrix and ");
    printf("\n The elements of the given matrix are:");
    for(i=0;i<m;i++)  /* display the matrix  */
    {
    printf("\n");
        for(j=0;j<n;j++)
        printf("\t %d",a[i][j]);
    }
    big=a[0];  /* logic to find the greatest element */
    for(i=0;i<m;i++)
        for(j=0;j<n;j++)
            if(a[i][j]>=big)
                big=a[i][j];

    printf("\n biggest element in the matrix=%d",big); /* print the greatest element */

    getch();
}

transpose of the given matrix

transpose of the given matrix

#include<stdio.h>
#include<conio.h>
main()
{

    int a[5][5],b[5][5],m,n,i,j;
    clrscr();
    printf("\n enter the no. of rows of the matrix:");
    scanf("%d",&m);
    printf("\n enter the no. of columns of the matrix:");
    scanf("%d",&n);
    for(i=0;i<m;i++)
        for(j=0;j<n;j++)
        {
        printf("\n enter the  a[%d][%d]  element:",i+1,j+1);
        scanf("%d",&a[i][j]);
        }
            /* printing the given series */
    clrscr();
    printf("\n The given matrix is of order %d*%d",m,n);
    printf("\n The elements of the given matrix are:");
    for(i=0;i<m;i++)
    {
    printf("\n");
        for(j=0;j<n;j++)
        printf("\t %d",a[i][j]);
    }
    for(i=0;i<m;i++)     /* logic for transposing the matrix */
        for(j=0;j<n;j++)
            b[j][i]=a[i][j];
             /* printing the transpose */
    printf("\n ");
    printf("\n The transpose of the matrix is of order %d*%d",n,m);
    printf("\n The elements of the transpose of the given matrix are:");
    for(i=0;i<n;i++)
    {
    printf("\n");
        for(j=0;j<m;j++)
        printf("\t %d",b[i][j]);
    }
    getch();

}

add two given matrix, Martrix C

add two given matrix

#include<stdio.h>
#include<conio.h>
main()
{

    int a[5][5],b[5][5],c[5][5],m,n,p,q,i,j;
    clrscr();
           
    printf("\n enter the no. of rows the matrix A:");
    scanf("%d",&m);
    printf("\n enter the no. of columns the matrix A:");
    scanf("%d",&n);
    printf("\n enter the no. of rows the matrix B:");
    scanf("%d",&p);
    printf("\n enter the no. of columns the matrix B:");
    scanf("%d",&q);
    if((m==p) && (n==q))
    {
        printf("\n The two matrices can be added ");
            /* enter the matrix A */
        for(i=0;i<m;i++)
            for(j=0;j<n;j++)
            {
            printf("\n enter the  a[%d][%d]  element:",i+1,j+1);
            scanf("%d",&a[i][j]);
            }
            /* enter the matric B  */
        for(i=0;i<p;i++)
            for(j=0;j<q;j++)
            {
            printf("\n enter the  b[%d][%d]  element:",i+1,j+1);
            scanf("%d",&b[i][j]);
            }
        clrscr();
                /*  printing the matric A  */
    printf("\n The elements of the given matrix A are:");
    for(i=0;i<m;i++)
    {
    printf("\n");
        for(j=0;j<n;j++)
        printf("\t %d",a[i][j]);
    }
                /* printing the matric  B  */
    printf("\n The elements of the given matrix B are:");
    for(i=0;i<p;i++)
    {
    printf("\n");
        for(j=0;j<q;j++)
        printf("\t %d",b[i][j]);
    }

    for(i=0;i<m;i++)             /* adding the two matrices */
        for(j=0;j<n;j++)
            c[i][j]=a[i][j]+b[i][j];
        /* printing the addition of the given matrices  */
    printf("\n The addition of the given matrices are:");
        for(i=0;i<m;i++)
        {
        printf("\n");
            for(j=0;j<n;j++)
                printf("\t %d",c[i][j]);
        }
    }
    else
        printf("\n The two matrices can not be added ");
    getch();
}

Popular 5 Posts Last 7 days