简体   繁体   中英

How to find whether the given array is sorted in descending order in c programming

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...

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 )

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.
   }
}

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.

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.

ps, array starts with '1' in Matlab but '0' in C.

I can't agree less about what others have said regarding array indexing, the problem in not knowing whether a[i+1] exists or not, and using of break. I would add though when all the numbers in this input array are equal it will still pass as decreasing in your code. If that is a problem you can add another if statement checking whether at all it decreases anywhere and use another variable say change for checking whether it gets satisfied.

int change = 0;
for(i=0;i<n;i++)
{
    if(a[i]<a[i+1])
        k++;
    if(a[i]>a[i+1])
        change++;
}
if(k==0 && change > 1)
    printf("yes");
else
    printf("no");

Check with the problem statement whether it allows such cases as descending. If it doesn't go with this otherwise its fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM