Search This Blog

Thursday, October 13, 2011

Write a program to sort an array in descending order

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  #include<stdlib.h>
   4:  #define developing 0
   5:  #define debug
   6:  void main()
   7:  {
   8:  int i,j,k;
   9:  char cd;
  10:  clrscr();
  11:  #if developing
  12:  //To be compiled if developing =1
  13:  int n=10;
  14:  float x[]={20,25,32,50,17,12,28,30,15};
  15:  #else
  16:  //To be compiled if developing =0
  17:  int n;
  18:  float x[50];
  19:  printf("\nNo of elements in array:");
  20:  scanf("%d",&n);
  21:  for(i=0;i<n;i++)
  22:      {
  23:      printf("Enter the element no %d: ",i);
  24:      scanf("%f",&x[i]);
  25:      }
  26:  #endif
  27:   
  28:  printf("\n\nThe original array is:\n");
  29:  for(i=0;i<n;i++)
  30:  printf("%.2f ",x[i]);
  31:  printf("\n");
  32:  for(j=0;j<n-1;j++)
  33:      for(i=0;i<n-1;i++)
  34:      {
  35:      if(x[i]<x[i+1])
  36:          {
  37:          float p,q;
  38:          p=x[i];
  39:          q=x[i+1];
  40:          x[i]=q;
  41:          x[i+1]=p;
  42:          #if defined debug
  43:          //To be compiled if degub is defined
  44:          printf("\nThe interchangeing element %d and %d:\n",i,i+1);
  45:              for(k=0;k<n;k++)
  46:              printf("%.2f ",x[k]);
  47:              printf("\n");
  48:              printf("\ncontinue debugging N/Y: ");
  49:              cd=getche();
  50:              if(cd=='N'||cd=='n')
  51:              exit(0);
  52:              #endif
  53:          }
  54:      }
  55:  printf("\nThe sorted array is:\n");
  56:  for(i=0;i<n;i++)
  57:  printf("%.2f ",x[i]);
  58:  printf("\n");
  59:  getch();
  60:  }

No comments:

Post a Comment