Saturday, July 24, 2010

Programs on array using C Programming Langusge


Since the last few postings we are in C Language array.  Today’s topic in our C programming tutorialis also c array . We’ll solve some programs on array using C Language.


 Program on identity matrices

#include< stdio.h >
void main ()
{
int mat1[4][4],mat2[4][4],i,j,x=3;
clrscr();
printf("Program on identity matrix.\n");
for(i=0;i< 4;i++)
{
 for(j=0;j< 4;j++)
 {
  if(i==j)
   mat1[i][j]=1;
  else
   mat1[i][j]=0;
  }
}
printf("The identity matrix is :-\n");
for(i=0;i< 4;i++)
{
 for(j=0;j< 4;j++)
 {
  printf("%4d",mat1[i][j]);
 }
 printf("\n");
}
printf("\n AND\n");
for(i=0;i< 4;i++)
{
 for(j=0;j< 4;j++)
 {
  if(j==x)
   mat1[i][j]=1;
  else
   mat1[i][j]=0;
  }
  x--;
}
for(i=0;i< 4;i++)
{
 for(j=0;j< 4;j++)
 {
  printf("%4d",mat1[i][j]);
 }
 printf("\n");
}
getch ();
}

In this C Language program, we’ll work on matrix. Given a 2-D matrix of order 3*3 .Search an element x if the element in the matrix, display that the search is successful and display the position at which the element occurs. If the search is not successful-display that element is not in the matrix.


 #include< stdio.h>
void main ()
 {
 int mat[3][3],i,j,x;
 clrscr();
 printf("\nNow we will enter the values(integer) for the matrix.");
 for(i=0;i< 3;i++)
 {
  for(j=0;j< 3;j++)
  {
   printf("\nValue:-");
   scanf("%d",&mat[i][j]);
   }
 }
 printf("\nEnter the specific value to be searched:-");
 scanf("%d",&x);
 clrscr();
 printf("\nThe matrix:-\n");
 for(i=0;i< 3;i++)
 {
  for(j=0;j< 3;j++)
  {
   printf("%4d",mat[i][j]);
   }
  printf("\n");
 }
for(i=0;i< 3;i++)
 {
  for(j=0;j< 3;j++)
  {
   if(x==mat[i][j])
   break;
   }
  if(j!=3)
  break;
 }
 if(i!=3)
 {
 printf("Your input value exists in row no %d and column no %d",i+1,j+1);
 }
 else
 {
 printf("Value not available.");
 }
 getch();
 }

Same  array program with some modification

 #include< stdio.h>
 void main ()
 {
 int mat[3][3],i,j,n,x[3],y[3],a=0;
 clrscr();
 printf("\nNow we will enter the values(integer) for the matrix.");
 for(i=0;i< 3;i++)
 {
  for(j=0;j< 3;j++)
  {
   printf("\nValue:-");
   scanf("%d",&mat[i][j]);
   }
 }
 printf("\nEnter the specific value to be searched:-");
 scanf("%d",&n);
 clrscr();
 printf("\nThe matrix:-\n");
 for(i=0;i< 3;i++)
 {
  for(j=0;j< 3;j++)
  {
   printf("%4d",mat[i][j]);
   }
  printf("\n");
 }
for(i=0;i< 3;i++)
 {
  for(j=0;j< 3;j++)
  {
   if(n==mat[i][j])
   {
    x[a]=i+1;
    y[a]=j+1;
    a++;
    }
   }
 }
 if(a==0)
 printf("Value does not exists in the matrix.");
 else
 {
  printf("The value exists in the matrix and locations are:-\n");
  for(i=0;i< a;i++)
  {
  printf("%d and %d\n",x[i],y[i]);
  }
 }
  getch();
 }

6 comments:

  1. *
    * *
    * * *
    * * * *
    * * * * * *
    any body can make this programme

    ReplyDelete
  2. #include< iostream.h >
    # include < conio.h >
    class Pattern
    {
    public:
    void show()
    {
    for(int i=0; i< 5; i++0
    {
    for( int j=0;j < =i;j++0
    {
    cout<< "*";
    }
    cout<< endl;}
    }
    }
    };
    void main ()
    {
    Pattern ob;
    ob.show();
    getch();
    }

    ReplyDelete
  3. 1 0 0 0
    0 1 0 0
    0 0 1 0
    0 0 0 1

    please give me solution of this system.

    ReplyDelete
  4. * * * *
    * * *
    * *
    *

    please give me solution of this problem

    ReplyDelete
  5. #include< stdio.h >
    void main ()
    {
    int i,j;
    for(i=0;i< 4;i++)
    {
    for(j=0; j< 4;j++)
    {
    if(i==j)
    printf("1");
    else
    printf("0");
    }
    printf("\n");
    }
    getch();
    }

    ReplyDelete
  6. #include< stdio.h>
    void main()
    {
    int i,j;
    for ( i=0; i < 4;i++)
    {
    for( j=i;j < 4; j++)
    {
    printf("*");
    }
    printf("\n");
    }
    getch();
    }

    ReplyDelete

Subscribe via email

Enter your email address:

Delivered by FeedBurner