Showing posts with label matrix. Show all posts
Showing posts with label matrix. Show all posts

Wednesday, March 30, 2016

C program to print a 2D matrix in a linear view

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

Tuesday, March 29, 2016

C program to implement multiplication between two 2D matrices

#include<stdio.h>
#include<conio.h>
void main()
{
int p,q,m,n,a[20][20],b[20][20],c[20][20],i,j,k,sum;
printf("enter dimension of first matrix");
scanf("%d%d",&m,&n);
printf("enter values of first matrix");
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter dimension of second matrix");
scanf("%d%d",&p,&q);
printf("enter elements of 2nd matrix");
for(i=1;i<=p;i++)
{
for(j=1;j<=q;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=1;i<=m;i++)
{
for(j=1;j<=q;j++)
{
sum=0;
for(k=1;k<=n;k++)
{
sum=sum+a[i][k]*b[k][j];
c[i][j]=sum;
}
}
}
printf("multiplication is ");
for(i=1;i<=m;i++)
{
printf("\n");
for(j=1;j<=q;j++)
{
printf("%d ",c[i][j]);
}
}
getch();
}
output
Enter dimension of first matrix 2 3
enter values of first matrix 1 2 3 4 5 6
Enter dimension of second matrix 3 2
enter values of 2nd matix 1 2 3 4 5 6
multiplication is
22 28
49 64

Tuesday, March 15, 2016

C program to check whether a matrix exist in another matrix or not

#include<stdio.h>
void main()
{
 int const size =5;
 int i,j;
 int n=5;
 int a[size][size];
 int b[3][3]={{1,2,3},{6,7,8},{11,12,13}};
 int count;
 for(i=0;i<n;i++)
  {
   for(j=0;j<n;j++)
    {
     a[i][j]=n*i+j;
    }
  }
 int k,l;
 int o,p;
 for(i=0;i<n;i++)
  {
   for(j=0;j<n;j++)
    {
     count=0;
     for(o=0;o<3;o++)
      {
       for(p=0;p<3;p++)
{
if(((i+o)<n)&&((j+p)<n))
 {
 if(a[i+o][j+p]==b[o][p])
  {
   count++;
  }
 else
  {
   break;
  }
 }
else
 {
  break;
 }
}
      }
     if(count==9)
      {
       printf("Same pattern in the array found");
      }
    }
  }
}
output
Same pattern in the array found