C/C++ program to Find that a Given Data Array In Sorted Or Not

Leave a Comment
/*The Following program 1st asks the array size & then the user will input the Array elements, the program hop by hop check for the condition a[i]<a[i-1] if it is true then it will display a success message other wise the array is not  sorted*/

/*To use this program in Turbo C++, you need to convert this in ANSI standard,  which can be easily done by removing 'using namespace std;' & instead of iostream  we should have to write iostream.h*/

#include <iostream>    //Using the new C++ Standard
#include <stdlib.h>      //For system("pause") function
using namespace std;  //Using standard Namespace 
void main()
{
float* myArray;
char* message = 0;
unsigned int arraySize = 0;
cout<<"Enter Array Size : ";


cin>>arraySize;        
//Getting the array size

myArray = new float[arraySize];  //Defining new float array with the entered size

cout<<"\nInput array elements: (Add 'Space' as a separator): ";
for (unsigned int i = 0; i< arraySize; i++)
{
cin>>myArray[i];     
//inputting the array elements
}

for(unsigned int i = 1; i< arraySize; i++)
if(myArray[i] < myArray[i-1])
      //Condition For Increasing sorted
message = "\nThe list is not sorted!\n";

/*For Decreasing sort checking use myArray[i] > myArray[i-1] in place of
myArray[i] < myArray[i-1] */

if(!message)
message = "\nThe list is sorted!\n";


delete [] myArray;
cout<<message;
system("pause");
}
   //End of the program


ScreenShots:
ScreenShot of RunTime of C/C++ program to Find that a Given Data Array In Sorted Or Not
ScreenShot of RunTime of C/C++ program to Find that a Given Data Array In Sorted Or Not

0 comments: