Sunday, 29 December 2013

PROGRAM TO ARRANGE ELEMENTS OF ARRAY IN ASCENDING AND DESCENDING ORDER

This is a program which arrange the array elements in different orders depending upon users input using "Functions"...!!!
It could be easily modified to "n" number of elements of array...!!!!
Feel free to contact me for any problem and help...!!!

#include<stdio.h>
void ascending(int a[],int size);
void descending(int a[],int size);
int main()
{
   int size=10;
int a[]={2,3,4,5,3,2,1,4,5,9};
int x,y,choice;
printf("1 for ascending\n2 for descending\nEnter the choice:");
scanf("%d",&choice);
    if (choice==1)
    ascending(a,size);
    else if(choice==2)
    descending(a,size);
    else
    printf("Not a valid input");
getch();
return 0;
}

void ascending(int a[],int size)
{
    int x=0,b;
    int temp;
    for(b=0;b<size-1;b++)
    {
 for(x=0;x<size-1;x++)
 {
     if (a[x+1]<a[x])
         {
            temp=a[x];
            a[x]=a[x+1];
            a[x+1]=temp;
         }
 }
    }
 for(x=0;x<size;x++)
 {
    printf("%d\t",a[x]);
 }

}

void descending(int a[],int size)
{
       int x,y;
       int temp;
    for(y=0;y<size-1;y++)
    {
        for(x=0;x<size-1;x++)
        {
           if (a[x]<a[x+1])
           {
            temp=a[x+1];
            a[x+1]=a[x];
            a[x]=temp;
           }
        }
    }
   for(x=0;x<size;x++)
  {
    printf("%d\t",a[x]);
  }
}

EXPECTED OUTPUT:


No comments:

Post a Comment