Search This Blog

Thursday, October 13, 2011

Write a program to sort an array of real Num using separate functions. Access array through pointers

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  void readdata(int *size,float *addx)
   4:  {
   5:  int i;
   6:  float n;
   7:  char ch;
   8:  printf("Enter the elements of array:\n");
   9:  i=0;
  10:  do
  11:  {
  12:  scanf("%f%c",&n,&ch);
  13:  *(addx+i)=n;
  14:  i++;
  15:  }while(ch!='\n');
  16:  *size=i;
  17:  }
  18:   
  19:  void displayarray(int size,float *addx)
  20:  {
  21:  int i;
  22:  for(i=0;i<size;i++)
  23:  printf("%.2f ",*(addx+i));
  24:  printf("\n");
  25:  }
  26:   
  27:  void sort(int size,float *addx)
  28:  {
  29:  int i,j;
  30:  for(j=0;j<size-1;j++)
  31:  for(i=0;i<size-1;i++)
  32:  if(*(addx+i)>*(addx+i+1))
  33:      {
  34:      int p,q;
  35:      p=*(addx+i);
  36:      q=*(addx+i+1);
  37:      *(addx+i)=q;
  38:      *(addx+i+1)=p;
  39:      }
  40:  }
  41:   
  42:  void main()
  43:  {
  44:  int i,size;
  45:  float x[50];
  46:  readdata(&size,x);
  47:  printf("Array is:\n");
  48:  displayarray(size,x);
  49:  sort(size,x);
  50:  printf("sorted array is:\n");
  51:  displayarray(size,x);
  52:  getch();
  53:  }

No comments:

Post a Comment