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

Wednesday, July 20, 2011

Program to find transpos of a matrix

/*Serial No.93     [swami68.cpp]*/

#include<stdio.h>
#include<conio.h>
void main()
{
int mat1[10][10];
int i,j,r,c;
clrscr();
printf("enter the no of rows=");
scanf("%d",&r);
printf("enter the no of column=");
scanf("%d",&c);
printf("enter the Ist matrix\n");
for(i=0;i<r;i++)
    {
    for(j=0;j<c;j++)
        {
        scanf("%d",&mat1[i][j]);
        }
    }
printf("\n\n\nFirst Entered matrix is:\n");
for(i=0;i<r;i++)
    {
    printf("\n");
    for(j=0;j<c;j++)
        {
        printf("\t%d",mat1[i][j]);
        }
    }
printf("\n\nTranspose of matrix is:\n");
for(i=0;i<c;i++)
    {
    printf("\n");
    for(j=0;j<r;j++)
        {
        printf("\t%d",mat1[j][i]);
        }
    }
getch();
}

Program to multiply two matrices

/*Serial No.89     [swami64.cpp]*/

#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
void main()
{
int mat1[10][10],mat2[10][10],mat3[10][10];
int i,j,k,r1,r2,c2,c1;
clrscr();
printf("Enter the no. of rows and column of first matrix:");
scanf("%d%d",&r1,&c1);
printf("\nEnter the no. of rows and column of second matrix:");
scanf("%d%d",&r2,&c2);
if(c1!=r2)
    {
    printf("Matrix multiplication is not possible!");
    getch();
    exit(0);
    }
printf("\nenter the 1st matrix\n");
for(i=0;i<r1;i++)
    {
    for(j=0;j<c1;j++)
        {
        scanf("%d",&mat1[i][j]);
        }
    }
printf("\n\n\nFirst Entered matrix is:\n");
for(i=0;i<r1;i++)
    {
    printf("\n");
    for(j=0;j<c1;j++)
        {
        printf("%d\t",mat1[i][j]);
        }
    }
printf("\n\nEnter the 2nd matrix\n");
for(i=0;i<r2;i++)
    {
    for(j=0;j<c2;j++)
        {
        scanf("%d",&mat2[i][j]);
        }
    }
printf("\n\n\nSecond Entered matrix is:\n");
for(i=0;i<r2;i++)
    {
    printf("\n");
    for(j=0;j<c2;j++)
        {
        printf("%d\t",mat2[i][j]);
        }
    }

printf("\n\n Product of the two matrix is:\n");
for(i=0;i<r1;i++)
    {
    printf("\n");
    for(j=0;j<c2;j++)
        {
        mat3[i][j]=0;
        printf("\t");
        for(k=0;k<c1;k++)
            {
            mat3[i][j]+=mat1[i][k]*mat2[k][j];
            }
            printf("%d",mat3[i][j]);
        }
    }
getch();
}

Program to find sum and difference of two matrices

/*Serial No.88     [swami63.cpp]*/

#include<stdio.h>
#include<conio.h>
void main()
{
int mat1[3][2],mat2[3][2];
int i,j;
clrscr();
printf("enter the Ist matrix\n");
for(i=0;i<3;i++)
    {
    for(j=0;j<2;j++)
        {
        scanf("%d",&mat1[i][j]);
        }
    }
printf("\n\n\nFirst Entered matrix is:\n");
for(i=0;i<3;i++)
    {
    printf("\n");
    for(j=0;j<2;j++)
        {
        printf("%d\t",mat1[i][j]);
        }
    }
printf("\n\nEnter the 2nd matrix\n");
for(i=0;i<3;i++)
    {
    for(j=0;j<2;j++)
        {
        scanf("%d",&mat2[i][j]);
        }
    }
printf("\n\n\nSecond Entered matrix is:\n");
for(i=0;i<3;i++)
    {
    printf("\n");
    for(j=0;j<2;j++)
        {
        printf("%d\t",mat2[i][j]);
        }
    }

printf("\n\n Sum of the two matrix is:\n");
for(i=0;i<3;i++)
    {
    printf("\n");
    for(j=0;j<2;j++)
        {
        printf("%d\t",mat2[i][j]+mat1[i][j]);
        }
    }
printf("\n\n Difference of the two matrix is:\n");
for(i=0;i<3;i++)
    {
    printf("\n");
    for(j=0;j<2;j++)
        {
        printf("%d\t",mat1[i][j]-mat2[i][j]);
        }
    }
getch();
}

Tuesday, July 19, 2011

Program to print upper & lower triangular matrix

/*Serial No.116     [swami93.cpp]*/

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k;
int upr[3][3],lwr[3][3];
clrscr();
int mat[3][3]={{1,2,3},{4,5,6},{7,8,9}};
printf("Entered matrix is:\n");
for(i=0;i<3;i++)
    {
    for(j=0;j<3;j++)
        {
        printf("%d\t",mat[i][j]);
        }
    printf("\n");
    }
printf("Upper traingular matrix:\n");
for(i=0;i<3;i++)
    {
    for(k=0;k<i;k++)
        {
        printf(" \t");
        }
    for(j=i;j<3;j++)
        {
        printf("%d\t",mat[i][j]);
        }
    printf("\n");
    }
printf("Lower traingular  matrix:\n");
for(i=0;i<3;i++)
    {
    for(j=0;j<=i;j++)
        {
        printf("%d\t",mat[i][j]);
        }
    printf("\n");
    }
getch();
}

Program to find transpos of a matrix using function by passing matrix as parameter

/*Serial No.118     [swami95.cpp]*/

#include<stdio.h>
#include<conio.h>
void tran(int mat[10][10]);
int i,j,r,c;
void main()
{
int mat1[10][10];

clrscr();
printf("enter the no of rows=");
scanf("%d",&r);
printf("enter the no of column=");
scanf("%d",&c);
printf("enter the Ist matrix\n");
for(i=0;i<r;i++)
    {
    for(j=0;j<c;j++)
        {
        scanf("%d",&mat1[i][j]);
        }
    }
printf("\n\n\nFirst Entered matrix is:\n");
for(i=0;i<r;i++)
    {
    printf("\n");
    for(j=0;j<c;j++)
        {
        printf("\t%d",mat1[i][j]);
        }
    }
printf("\n\nTranspose of matrix is:\n");
tran(mat1);

getch();
}
void tran(int mat[10][10])
    {
    for(i=0;i<c;i++)
        {
        printf("\n");
        for(j=0;j<r;j++)
            {
            printf("\t%d",mat[j][i]);
            }
        }
    }

Program to understand use of pointers in MATRIX

/*Serial No.126     [swami105.cpp]*/

/*Accessing matrix elements & their address using pointer*/
#include<stdio.h>
#include<conio.h>
int i,j;
void main()
{
int mat1[3][3]={{11,12,13},{14,15,16},{17,18,19}};
clrscr();
printf("First Entered matrix is:\n");
for(i=0;i<3;i++)
    {
    printf("\n");
    for(j=0;j<3;j++)
        {
        printf("\t%d",mat1[i][j]);
        }
    }
printf("\n\n Address of first row  =%u",mat1);
printf("\n Address of Second row =%u",mat1+1);
printf("\n Address of Third row  =%u\n",mat1+2);
printf("\n Address of\t(0,0) =%u",*(mat1+0)+0);
printf("\t (0,1) =%u",*(mat1+0)+1);
printf("\t (0,2) =%u",*(mat1+0)+2);
printf("\n Address of\t(1,0) =%u",*(mat1+1)+0);
printf("\t (1,1) =%u",*(mat1+1)+1);
printf("\t (1,2) =%u",*(mat1+1)+2);
printf("\n Address of\t(2,0) =%u",*(mat1+2)+0);
printf("\t (2,1) =%u",*(mat1+2)+1);
printf("\t (2,2) =%u",*(mat1+2)+2);

printf("\n\n Value at\t(0,0) =%d",*(*(mat1+0)+0));
printf("\t (0,1) =%d",*(*(mat1+0)+1));
printf("\t (0,2) =%d",*(*(mat1+0)+2));
printf("\n Value at\t(1,0) =%d",*(*(mat1+1)+0));
printf("\t (1,1) =%d",*(*(mat1+1)+1));
printf("\t (1,2) =%d",*(*(mat1+1)+2));
printf("\n Value at\t(2,0) =%d",*(*(mat1+2)+0));
printf("\t (2,1) =%d",*(*(mat1+2)+1));
printf("\t (2,2) =%d",*(*(mat1+2)+2));

getch();
}

Program for sparse matrix

/*Serial No.130     [swami109.cpp]*/

#include<stdio.h>
#include<conio.h>
void main()
    {
    int i,j,rows,cols,tot=0;
    int mat[5][5];
    clrscr();
    printf("enter the no of rows= ");
    scanf("%d",&rows);
    printf("enter the no of columns= ");
    scanf("%d",&cols);
    printf("\nEnter the matrix elements:\n");
    for(i=0;i<rows;i++)
        {
        for(j=0;j<cols;j++)
            {
            scanf("%d",&mat[i][j]);
            }
        }
    printf("\nEntered matrix is:\n");
    for(i=0;i<rows;i++)
        {
        for(j=0;j<cols;j++)
            {
            printf("\t%d",mat[i][j]);
            }
        printf("\n");
        }

    printf("\nSparse matrix is:\n");
    for(i=0;i<rows;i++)
        {
        for(j=0;j<cols;j++)
            {
            if(mat[i][j]!=0)
            tot++;
            }
        }

    printf("\t%d\t%d\t%d\n",rows,cols,tot);
    for(i=0;i<rows;i++)
        {
        for(j=0;j<cols;j++)
            {
            if(mat[i][j]!=0)
            printf("\t%d\t%d\t%d\n",i+1,j+1,mat[i][j]);
            }
        }
    getch();
    }

Program to print tridiagonal matrix

/*Serial No.131     [swami110.cpp]*/

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k;
clrscr();
int mat[5][5]={{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15},{16,17,18,19,20},{21,22,23,24,25}};
printf("Entered matrix is:\n\n");
for(i=0;i<5;i++)
    {
    for(j=0;j<5;j++)
        {
        printf("%d\t",mat[i][j]);
        }
    printf("\n");
    }

printf("\nTridiagnal matrix:\n\n");
printf("%d\t%d\n",mat[0][0],mat[0][1]);
for(i=1;i<5;i++)
    {
    for(k=1;k<i;k++)
        {
        printf("\t");
        }
    if(i==4)
        printf("%d\t%d\n",mat[4][3],mat[4][4]);
    else
        {
        for(j=i;j<5;j++)
            {
            if(i==j)
            printf("%d\t%d\t%d",mat[i][j-1],mat[i][j],mat[i][j+1]);
            }
        printf("\n");
        }
    }
getch();
}