Program to determine whether an array is in ascending, descending or unsorted order

Program:

/*       cpp program to determine whether an array is in ascending, descending or unsorted order       */

#include"iostream"

using namespace std;

int main()
{

    int num, a =0, d=0, s=0, i, j;
    do
    {
        cout<<"How many elements are there in array: ";
        cin>>num;
        if(num<1)
        {
            cout<<"Please enter a positive integer."<<endl;
        }
    }while(num<1);
    int arr[num];                 //declaration of array of size num
    cout<<"Enter "<<num<<" elements:"<<endl;
    for( i=0; i<num; i++)
    {
        cin>>arr[i];           //taking array elements by user
    }
    cout<<"\n";
    for( i=0; i<num; i++)
    {
       
            if(arr[i] < arr[i+1] && arr[i+1] < arr[i+2])
            {
                a++;   
               
            }
            else if(arr[i] > arr[i+1] && arr[i+1] > arr[i+2])
            {
                d++;
               
            }
            else
            {
                s++;
                break;
            }
           
       
    }

    if(a>0)   
    {
        cout<<"Array is in Ascending order."<<endl;
    }
    else if(d>0)
    {   
        cout<<"Array is in Descending order."<<endl;
    }
    else
    {
        cout<<"Array is Unsorted."<<endl;
    }
   
}

Output:

 

Comments

Popular Posts