Showing posts with label Array. Show all posts
Showing posts with label Array. Show all posts

Saturday, August 20, 2011

Sort an array using selection sort.

//Program to sort an array using  selection sort..
#include<stdio.h>
#include<conio.h>
void main()
{
 int *a, n, temp, k , loc, i;
 int min, j;
 clrscr();
 printf("\nEnter the number of elements in array: ");
 scanf("%d",&n);
 a=(int *)malloc(n*sizeof(int));
  printf("\nEnter The array :");
 for (i=0;i<n;i++)
 {
   scanf("%d",&a[i]);;
 }
 for (k=0; k<n-1; k++)
 {
    min=a[k];
    loc=k;
    for (j=k+1; j<n; j++)
    {
     if(min>a[j])
      {
       min=a[j];
    loc=j;
      }
    }
   temp=a[k];
   a[k]=a[loc];
   a[loc]=temp;
 }
 printf("\nThe sorted array is:");
  for(i=0;i<n;i++)
  {
      printf("\n %d",a[i]);
  }
 getch();
}

Sort an array using Insertion sort.

//Program to sort an array using Insertion sort.
#include<stdio.h>
#include<conio.h>
void main()
{
  int *a,n,i,temp,k,j;
  clrscr();
  printf("\nEnter the no. of element in the array: ");
  scanf("%d",&n);
  a=(int *)malloc(n*sizeof(int));
  printf("\nEnter the values of %d elements: ",n);
  for(i=0;i<n;i++)
  {
  scanf("%d",&a[i]);
  }
  for(i=1;i<n;i++)
  {
   temp=a[i];
    for(j=0;j<i;j++)
    {
     if(temp<a[j])
     {
       for(k=i;k>j;k--)
    a[k]=a[k-1];
    a[j]=temp;
    break;
     }
    }
  }
  printf("\nThe sorted array is:");
  for(i=0;i<n;i++)
  {
     printf("\n %d ",a[i]);
  }
  getch();
}

Insert an element in array.

//Program to insert an element in array.
#include<stdio.h>
#include<conio.h>
void main()
{
 int i,data[30],n,m,a,l,e;
 char c;
 clrscr();
 printf("program to insert element in array");
 printf("\nEnter the number of elements in the array:");
 scanf("%d",&n);
      a=0;
      l=n-1;
 for(i=a;i<=l;i++)
 {
  printf("\nEnter the value of %d element:",i);
  scanf("%d",&data[i]);
 }
 printf("\nArray before insertion: ");
 for(i=0;i<n;i++)
 printf("%d, ",data[i]);
  printf("\nEnter the element you want to insert: ");
  scanf("%d",&e);
 printf("\nif u want to insert beginning element then press b");
 printf("\nif u want to insert middle element then press m");
 printf("\nif u want to insert end element then press e");
 printf("\nEnter ur choice: ") ;
 c=getche();
 switch(c)
 {
  case 'b':
  case 'B':

      for(i=l;i>=0;i--)
      {
       data[i+1]=data[i];
      }
      data[a]=e;
      l=l+1;
      break;
  case 'm':
  case 'M':
      m=(a+l)/2;
      for(i=l;i>=m;i--)
      {
    data[i+1]=data[i];
      }
      data[m]=e;
      l=l+1;
     break;
  case 'e':
  case 'E':
     l=l+1;
     data[l]=e;
     break;
  default:
       printf("\nInvalid choice");
       getch();
       exit(0);
 }
  printf("\nArray after insertion:");
  for(i=a;i<=l;i++)
  printf("%d, ",data[i]);
 getch();
}

Program to perform deletion operation in array.

//Program to perform deletion operation in array.
#include<stdio.h>
#include<conio.h>
void main()
{
 int i,*data,n,m,a,l,e;
 char c;
 clrscr();
 printf("Enter the number of elements in the array:");
 scanf("%d",&n);
 data=(int*)malloc(n*sizeof(int));
 for(i=0;i<n;i++)
 {
 printf("\nEnter the value of %d element:",i);
  scanf("%d",&data[i]);
 }
 printf("\nArray before deletion");
 for(i=0;i<n;i++)
  printf("%d, ",data[i]);
      a=0;
      l=n-1;
 printf("\nif u want to delete beginning element then press b");
 printf("\nif u want to delete middle element then press m");
 printf("\nif u want to delete end element then press e");
 printf("\nEnter ur choice: ") ;
 c=getche();
 switch(c)
 {
  case 'b':
  case 'B':
      e=data[0];
      for(i=0;i<n-1;i++)
      {
       data[i]=data[i+1];
      }
      n=n-1;
      break;
  case 'm':
  case 'M':
      m=(a+l)/2;
      e=data[m];
      for(i=m;i<n-1;i++)
      {
    data[i]=data[i+1];
      }
      n=n-1;
     break;
  case 'e':
  case 'E':
     e=data[l];
     n=n-1;
     break;
  default:
       printf("\nInvalid choice");
       getch();
       exit(0);
 }
  printf("\nArray after deletion:");
  for(i=0;i<n;i++)
  printf("%d, ",data[i]);
  printf("\n\n Deleted element is %d",e);
 getch();
}

Find occurance of given character in string.

//Program to find occurance of given character in string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[20],c;
int o,i,l;
clrscr();
printf("program to find occurance of given character in string");
printf("\n Enter the string: ");
gets(str);
l=strlen(str);
printf("Please enter the character whose number of occurances u want to find: ");
c=getche();
o=0;
for(i=0;i<l;i++)
{
 if(str[i]==c)
 {
 o=o+1;
 }
}
printf("\n the number of occurances of given character is %d",o);
getch();
}

Next Home
Twitter Delicious Facebook Favorites More