Write a program to find whether the given array is sorted in ascending or descending order.
Input Format:
Input consists of n+1 integers. The first integer corresponds to ‘n’ , the size of the array. The next ‘n’ integers correspond to the elements in the first array. Assume that the maximum value of n is 15.
Output Format:
Sample Input 1:
5
2
3
6
8
10
Sample Output 1:
yes
Sample Input 2:
5
20
13
6
4
1
Sample Output 2:
yes
Sample Input 2:
5
20
13
6
4
10
Sample Output 3:
no
Code:
#include<stdio.h> int main(){ int a[15],n,i,aflag=0,bflag=0; scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d",&a[i]); if(i>0){ if((a[i-1]>a[i])) aflag++; else bflag++; } } if(aflag==(n-1)||(bflag==(n-1))) printf("yes"); else printf("no"); return 0; }
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteIf you want only asecending order check then you dont need bflag.So just remove it and change the condition..
ReplyDeleteif(a[i-1]<a[j])
{
aflag++;
}
if(aflag==(n-1))
printf("yes");
else
printf("no");
Very Interesting and good piece of information you have been shared and keep sharing the blogs...
ReplyDeleteCheck Ordering
Thanks for sharing ! This article will teach you straightforward ways in which to proceed concerning the method of ordering and designing checks.Click here to learn more
ReplyDeleteThis comment has been removed by the author.
ReplyDeletecan you tell me why the "n" variable is used?
ReplyDelete