Challenge: Check if array is ascending, descending or not sorted | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

Challenge: Check if array is ascending, descending or not sorted

Input an array of number from user and check if the given array is in ascending order or descending order or not sorted. e.g. If the given array is 12, 13, 22, 27, 40 the program should print "Ascending". If the given array is 45, 44, 23, 11, 11 the program should print "Not sorted" and print "Descending" if the given array is in descending order. Note: The array can be of any size but not less than 10.

24th Jul 2017, 6:01 PM
Kartikey Sahu
Kartikey Sahu - avatar
21 Answers
+ 5
24th Jul 2017, 6:45 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 8
I will do this challenge
24th Jul 2017, 11:15 PM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 8
Just need aome clearer definitions
24th Jul 2017, 11:17 PM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 8
Hope mine works Function itself took 10 lines. It was the getting input I was spending more time on. https://code.sololearn.com/cDN4tlkZeY6O/?ref=app
24th Jul 2017, 11:32 PM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
24th Jul 2017, 9:00 PM
Garikai
Garikai - avatar
+ 4
Seems like we're all missing something important here; the initial instruction​ says the array is supposed to come as input from the user, and the list has to have at least 10 elements.
25th Jul 2017, 5:20 AM
Garikai
Garikai - avatar
+ 4
it wil work only if you enter all the six numbers https://code.sololearn.com/chLW650w0Vc5/?ref=app
25th Jul 2017, 6:52 PM
‎ ‏‏‎Anonymous Guy
+ 3
tis program accepts user input if you have any suggestions comment it https://code.sololearn.com/cwNkPP9kuVf5/?ref=app
26th Jul 2017, 5:06 AM
‎ ‏‏‎Anonymous Guy
+ 2
something like this: PS: 9 lines and O(n) https://code.sololearn.com/cfia67RtNE9P/?ref=app
24th Jul 2017, 9:15 PM
yuri
+ 2
@Vengat Waiting...
24th Jul 2017, 11:16 PM
Kartikey Sahu
Kartikey Sahu - avatar
+ 2
Ask me if you have any doubt.
24th Jul 2017, 11:18 PM
Kartikey Sahu
Kartikey Sahu - avatar
+ 2
This challenge might interest you https://www.sololearn.com/Discuss/562254/?ref=app
25th Jul 2017, 8:54 AM
Kartikey Sahu
Kartikey Sahu - avatar
+ 2
https://code.sololearn.com/c6b0vG3UDi3h/?ref=app My own answer
25th Jul 2017, 2:11 PM
Kartikey Sahu
Kartikey Sahu - avatar
27th Jul 2017, 8:47 AM
Amit Dubey
Amit Dubey - avatar
+ 1
@kartikey, no advertising in other threads please remove
25th Jul 2017, 1:48 AM
Abdur-Rahmaan Janhangeer
Abdur-Rahmaan Janhangeer - avatar
25th Jul 2017, 7:53 AM
Augustas Macijauskas
Augustas Macijauskas - avatar
+ 1
We can use nested if statements Like for descending order if(n[0]>n[1]) { if(n[1]>n[2]) { .....so on up to n[9]>n[10] } } for ascending order invert the signs(> to <) else if if none are true then it is disordered array
26th Jul 2017, 4:55 AM
Purvesh Jain
Purvesh Jain - avatar
0
@/* S Vengat */ My code was very similar to your. But I had problems with the first if... Why do you have to put sorted(list (set (nums))) ? If the input is ascending, sorted (nums) should be equal to nums no?
25th Jul 2017, 9:58 PM
André Oliveira
André Oliveira - avatar
0
Here you go. Works with both odd and even lengthed arrays https://code.sololearn.com/cjAITs62otlO/?ref=app
27th Jul 2017, 9:25 PM
Vari93
Vari93 - avatar
0
the answer is in c language #include<stdio.h> int main() { int i,a[100],n,flag = 0,k=0,j=0; printf("enter the number of elements"); scanf("%d",&n); printf("enter the elements"); for (i=0;i<n;i++) { scanf("%d",&a[i]); } for (i=0;i<n-1;i++) { if ( a[i] < a[i+1]) k++; else if ( a[i] > a[i+1]) j++; } if (k== n-1) { printf("they are in ascending order"); } else if (j==n-1) { printf("they are in descending order"); } else if (j<n-1||i<n-1) { printf("they are not ordered"); } return 0; }
8th Dec 2019, 9:33 AM
KNG YOGESH
KNG YOGESH - avatar