+ 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.
22 ответов
+ 5
https://code.sololearn.com/c7J540OFtYJz/?ref=app
Just done ! In C of course :p
+ 8
I will do this challenge
+ 8
Just need aome clearer definitions
+ 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
+ 5
Hope you like it
https://code.sololearn.com/cTNEVtyv5yon/?ref=app
+ 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.
+ 4
it wil work only if you enter all the six numbers
https://code.sololearn.com/chLW650w0Vc5/?ref=app
+ 3
tis program accepts user input
if you have any suggestions comment it
https://code.sololearn.com/cwNkPP9kuVf5/?ref=app
+ 2
something like this:
PS: 9 lines and O(n)
https://code.sololearn.com/cfia67RtNE9P/?ref=app
+ 2
@Vengat Waiting...
+ 2
Ask me if you have any doubt.
+ 2
This challenge might interest you https://www.sololearn.com/Discuss/562254/?ref=app
+ 2
https://code.sololearn.com/c6b0vG3UDi3h/?ref=app
My own answer
+ 2
Here it is:
Works well.
https://code.sololearn.com/cSNDq0Gk0E58/?ref=app
+ 1
@kartikey, no advertising in other threads please remove
+ 1
Check out my challenge:
https://www.sololearn.com/Discuss/561399/?ref=app
+ 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
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?
0
Here you go. Works with both odd and even lengthed arrays
https://code.sololearn.com/cjAITs62otlO/?ref=app
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;
}