forked from sachin-nono/codingpractice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsArraySorted.cpp
More file actions
69 lines (54 loc) · 1.39 KB
/
IsArraySorted.cpp
File metadata and controls
69 lines (54 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
Is Array Sorted
Enter elements of an array of size N.
Write a function which returns true if the array is sorted and false otherwise.
Print the value returned.
*/
#include<iostream>
using namespace std;
int main()
{
int N, A[20];
bool x, isSortedAsc(int [], int), isSortedDes(int [], int);
cout<<"Enter size of the array : ";
cin>>N;
if(N>0)
{
cout<<"\nEnter array elements :\n";
for(int j=0; j<N; ++j)
cin>>A[j];
int ch;
cout<<"\nWhich order you wants to check :\n"<<"1. Ascending Order. or\n2. Descending Order.\n";
cout<<"\nEnter your choice : ";
cin>>ch;
if(ch==1)
{
x=isSortedAsc(A,N);
cout<<"\nResult : "<<x<<endl;
}
else if(ch==2)
{
x=isSortedDes(A,N);
cout<<"\nResult : "<<x<<endl;
}
else
cout<<"\nWrong Choice!!!\n";
}
else
cout<<"\nEnter correct size!!!\n";
return 0;
}
bool isSortedAsc(int A[], int N) //To check ascending order
{
for(int i=0; i<(N-1); ++i)
if(!(A[i]<A[i+1]))
return false;
return true;
}
bool isSortedDes(int A[], int N) //To check descending order
{
for(int i=0; i<(N-1); ++i)
if(!(A[i]>A[i+1]))
return false;
return true;
}