Friday, November 23, 2012

WRITE A PROGRAM TO CHECK WHETHER A GIVEN ARRAY IS SORTED. (ASCENDING,DESCENDING OR UNSORTED)


/* WRITE A PROGRAM TO CHECK WHETHER A GIVEN ARRAY IS SORTED.
     (ASCENDING,DESCENDING OR UNSORTED)
*/
#include<stdio.h>
#include<conio.h>

#define MAX 50

void main()
{
            int arr[MAX],asc=0,des=0,i,n;
            clrscr();
            printf("Enter the size of array : ");
            scanf("%d",&n);
            printf("Enter array elements : ");
            for(i=0;i<n;i++)
                        scanf("%d",&arr[i]);
            for(i=0;i<n-1;i++)
            {
                        if(arr[i]<arr[i+1])
                                    asc++;
                        if(arr[i]>arr[i+1])
                                    des++;
                        if(asc!=0 && des!=0)
                                    break;
            }

            if(asc==n-1)
                        printf("The array is sorted in ASCENDING Order");
            else if(des==n-1)
                        printf("The array is sorted in DESCENDING Order");
            else
                        printf("This is an unsorted array");
            getch();
}



/*
            =====OUT PUT=====

      1.Enter the size of array : 5
                        Enter array elements : 1 2 3 4 5
                        The array is sorted in ASCENDING Order

              2.Enter the size of array : 5
                        Enter array elements : 9 8 7 6 5
                        The array is sorted in DESCENDING Order

              3.Enter the size of array : 5
                        Enter array elements : 1 5 2 7 3 4
                        This is an unsorted array
*/

0 comments:

Post a Comment