Sunday, October 16, 2011

Matrices {Transpose}

#include<stdio.h>
#include<conio.h>
void main()
{
   int m, n, c, d, matrix[10][10], transpose[10][10];
   clrscr();
   printf("Enter the number of rows and columns of matrix ");
   scanf("%d%d",&m,&n);
   printf("Enter the elements of matrix \n");
   for( c = 0 ; c < m ; c++ )
   {
      for( d = 0 ; d < n ; d++ )
      {
         scanf("%d",&matrix[c][d]);
      }
   }
   for( c = 0 ; c < m ; c++ )
   {
      for( d = 0 ; d < n ; d++ )
      {
         transpose[d][c] = matrix[c][d];
      }
   }
   printf("Transpose of entered matrix :-\n");
   for( c = 0 ; c < n ; c++ )
   {
      for( d = 0 ; d < m ; d++ )
      {
         printf("%d\t",transpose[c][d]);
      } 
      printf("\n");
   }
   getch();
}

Matrices {Difference}

#include<stdio.h>
#include<conio.h>
void main()
{
   int m, n, c, d, first[10][10], second[10][10], difference[10][10];
   clrscr();
   printf("Enter the number of rows and columns of matrix\n");
   scanf("%d%d",&m,&n);
   printf("Enter the elements of first matrix\n");
   for ( c = 0 ; c < m ; c++ )
      for ( d = 0 ; d < n ; d++ )
         scanf("%d",&first[c][d]);
   printf("Enter the elements of second matrix\n");
   for ( c = 0 ; c < m ; c++ )
      for ( d = 0 ; d < n ; d++ )
            scanf("%d",&second[c][d]);
   for ( c = 0 ; c < m ; c++ )
      for ( d = 0 ; d < n ; d++ )
         difference[c][d] = first[c][d] - second[c][d];
   printf("difference of entered matrices:-\n");
   for ( c = 0 ; c < m ; c++ )
   {
      for ( d = 0 ; d < n ; d++ )
         printf("%d\t",difference[c][d]);
      printf("\n");
   }
   getch();
}

Matrices {Sum}

#include<stdio.h>
#include<conio.h>
void main()
{
   int m, n, c, d, first[10][10], second[10][10], sum[10][10];
   clrscr();
   printf("Enter the number of rows and columns of matrix ");
   scanf("%d%d",&m,&n);
   printf("Enter the elements of first matrix\n");
   for ( c = 0 ; c < m ; c++ )
      for ( d = 0 ; d < n ; d++ )
         scanf("%d",&first[c][d]);
   printf("Enter the elements of second matrix\n");
   for ( c = 0 ; c < m ; c++ )
      for ( d = 0 ; d < n ; d++ )
            scanf("%d",&second[c][d]);
   for ( c = 0 ; c < m ; c++ )
      for ( d = 0 ; d < n ; d++ )
         sum[c][d] = first[c][d]+ second[c][d];
   printf("Sum of entered matrices:-\n");
   for ( c = 0 ; c < m ; c++ )
   {
      for ( d = 0 ; d < n ; d++ )
         printf("%d\t",sum[c][d]);
      printf("\n");
   }
   getch();
}

Delete Element {Array}

#include<stdio.h>
#include<conio.h>
void main()
{
      int array[100], position, c, n;
      clrscr();
      printf("Enter number of elements in array\n");
      scanf("%d", &n);
      printf("Enter %d elements\n", n);
      for ( c = 0 ; c < n ; c++ )
          scanf("%d", &array[c]);
      printf("Enter the location where you wish to delete element\n");
      scanf("%d", &position);
      if ( position >= n+1 )
          printf("Deletion not possible.\n");
      else
      {
          for ( c = position - 1 ; c < n - 1 ; c++ )
              array[c] = array[c+1];
          printf("Resultant array is\n");
          for( c = 0 ; c < n - 1 ; c++ )
              printf("%d\n", array[c]);
      }
      getch();
}

Insert Element {Array}

#include<stdio.h>
#include<conio.h>
void main()
{
      int array[100], position, c, n, value;
      clrscr();
      printf("Enter number of elements in array\n");
      scanf("%d", &n);
      printf("Enter %d elements\n", n);
      for ( c = 0 ; c < n ; c++ )
          scanf("%d", &array[c]);
      printf("Enter the location where you wish to insert an element\n");
      scanf("%d", &position);
      printf("Enter the value to insert\n");
      scanf("%d", &value);
      for ( c = n - 1 ; c >= position - 1 ; c-- )
          array[c+1] = array[c];
      array[position-1] = value;
      printf("Resultant array is\n");
      for( c = 0 ; c <= n ; c++ )
           printf("%d\n", array[c]);
      getch();
}

Reverse Array

#include<stdio.h>
#include<conio.h>
#include<conio.h>
void main()
{
   int n, c, j, temp, a[100];
   clrscr();
   printf("Enter the number of elements in array\n");
   scanf("%d",&n);
   printf("Enter the array elements\n");
   for ( c = 0 ; c < n ; c++ )
      scanf("%d",&a[c]);
   if( n%2 == 0 )
      c = n/2 - 1;
   else
      c = n/2;
   for ( j = 0 ; j < c ; j++ )
   {
      temp = a[j];
      a[j] = a[n -j - 1];
      a[n-j-1] = temp;
   }
   printf("Reverse array is\n");
   for( c = 0 ; c < n ; c++ )
      printf("%d\n", a[c]);
   getch();
}

Binary Search

#include<stdio.h>
#include<conio.h>
void main()
{
   int c, first, last, middle, n, search, array[100];
   clrscr();
   printf("Enter number of elements\n");
   scanf("%d",&n);
   printf("Enter %d integers\n", n);
   for ( c = 0 ; c < n ; c++ )
      scanf("%d",&array[c]);
   printf("Enter value to find\n");
   scanf("%d",&search);
   first = 0;
   last = n - 1;
   middle = (first+last)/2;
   while( first <= last )
   {
      if ( array[middle] < search )
         first = middle + 1;   
      else if ( array[middle] == search )
      {
         printf("%d found at location %d.\n", search, middle+1);
         break;
      }
      else
         last = middle - 1;

      middle = (first + last)/2;
   }
   if ( first > last )
      printf("Not found! %d is not present in the list.\n", search);
   getch();  
}