How to find whether the given array is sorted in descending order in c programming
Solution 1
Arrays are indexed from 0
to size - 1
. So if you have
int array[15];
the elements are a[0]
to a[14]
. But in your code, you are starting from a[1]
to a[15]
and might try to access a[15]
, which is unsafe memory and will cause problems.
So you should first change your for loop.
You should change it to
for( i = 0 ; i < n ; i++ )
{
scanf("%d",&a[i]);
}
for( i = 0 ; i < n - 1 ; i++ )
{
if( a[i] < a[i+1] )
{
k++;
break;
}
}
In the second for loop, you should loop till i < n - 1
because otherwise, in
if(a[i]<a[i+1])
when i = n
, you will try to access the n + 1
th with element with a[i+1]
, which might be out of bounds.
You can also just exit from the loop once you find out that the array is not in descending order to save time.
You must also be certain that a[15]
is enough to store all the values ( that is, the number of values given as input should not exceed 15, check the problem statement to ensure this )
Solution 2
you should start array index from 0
in both for loop.
Use this
for(i=0;i<n;i++)
in second for loop you should use
for(i=0;i<n-1;i++) // you need to compare up to second last element with last, so run loop upto `i<n-1`
for example if you enter n = 5
loop will work for 0
to 4
It is good to use break;
if value of k
increases
for(i=1;i<=n;i++)
{
if(a[i]<a[i+1]){
k++;
break; // if k increments break the loop.
}
}
Solution 3
Here you are.:)
#include <stdio.h>
#define N 15
int main(void)
{
int a[N];
int i, n;
printf( "Enter number of elements in the array (not greater than %d: ", N );
scanf( "%d", &n );
if ( N < n ) n = N;
printf( "Enter %d elements of the array: ", n );
for ( i = 0; i < n; i++ ) scanf( "%d", &a[i] );
i = 0;
while ( i++ < n && !( a[i-1] < a[i] ) );
if ( i == n ) puts( "The array is sorted in the descending order" );
else puts( "The array is not sorted in the descending order" );
return 0;
}
The program output might look like
Enter number of elements in the array (not greater than 15: 15
Enter 15 elements of the array: 10 10 9 9 9 8 7 6 5 5 4 3 2 1 0
The array is sorted in the descending order
As for you code then this loop
for(i=1;i<=n;i++)
{
if(a[i]<a[i+1])
k++;
}
is unsafe because using index i+1
in expression a[i+1]
you can access memory beyond the array.
Also you have to check whether the entered value of n is less than 15 that is the size of the array because you use the range of indices [0, n]
.
And in the first loop the index has to start from 0. Otherwise the first element of the array will not be initialized.
Solution 4
First, you define array a[15]:
int n,a[15],i,k=0;
and you use variable n to control its size
scanf("%d",&n);
for(i=1;i<=n;i++)
{ scanf("%d",&a[i]);}
then you need to keep n <= 15.
p.s., array starts with '1' in Matlab but '0' in C.
Anish Kumar
Updated on January 08, 2020Comments
-
Anish Kumar over 4 years
I need to find out whether the given array is sorted in descending order or not...
I got the output but in portal it shows as Wrong Answer.This is my code.
#include<stdio.h> int main() { int n,a[15],i,k=0; scanf("%d",&n); for(i=1;i<=n;i++) { scanf("%d",&a[i]);} for(i=1;i<=n;i++) { if(a[i]<a[i+1]) k++; } if(k==0) printf("yes"); else printf("no"); return 0; }
Help me to figure it out...
-
Some programmer dude about 9 yearsYou don't need to check all the number in the array, the first time you find a number that is larger than the previous number you have your answer.
-
Some programmer dude about 9 yearsAnd you're out of bounds of the read values. If you read
n
value, what value wouldn + 1
have? -
Abhi about 9 yearsHow did you assume that the user will always enter less than 15 numbers in the array a[15]? The portal where you are trying to test the code would be using random test cases where the array size would be much more than 15. In which case your code would fail. You have to find a better way to do this.
-
The Paramagnetic Croissant about 9 yearsplease learn the language's basics before trying to write programs in it. the fact that you are indexing your arrays from 1 shows that you have no idea what you are doing.
-
-
Himanshu about 9 yearsyou should write
break;
inside parenthesis{ }
otherwise it will break; loop at first time as it is outside if condition. -
Himanshu about 9 yearsin this case it will break loop, whether condition is true or not.
-
bugwheels94 about 9 yearsis there any specific reason for
!( a[i-1] < a[i] )
over simplera[i-1]>=a[i]
-
Vlad from Moscow about 9 years@Ankit It is a programming culture. Sorted sequences are ordered by using operator <. For example in C++ it is enough to define this operator that to sort any sequences. So usually classes define only operator < .