Title: Program to display the count of the number of times the succeeding element is greater than the preceding element
Tags: c
Question: I've been meaning to write a code to display the count for the number of times the suceceding element is greater than the preceding elements but I dont think I have got the logic right. Here's my code .Kindly give me suggestions to improve the code to get the desired output.

```int main(void)
{
    int arr[10] = { '\0' };
    int i = 0, n = 0, count = 0, j = 0;
    printf("\nEnter the number of elements in the array: ");
    scanf("%d", &amp;n);
    printf("\nEnter the elements");
    for (i = 0; i < n; i++)
    {
        scanf("%d", &amp;arr[i]);
    }
    printf("\nThe array elements are: ");
    for (i = 0; i < n; i++)
    {
        printf("%d\t", arr[i]);
    }
    for (i = arr[0], j = arr[1]; i < arr[n], j = arr[n]; i++, j++)
    {
        if (i < j)
            count++;
    }
    printf("\nThe count is: %d", count);

    return 0;
}
```

Comment: Did you try even basic debugging ?

Comment: Fourth: a pencil and paper may help here too.

Comment: `for (i = arr[0], j = arr[1]; i < arr[n], j = arr[n]; i++, j++)` generates warning "warning: left-hand operand of comma expression has no effect [-Wunused-value]"  Review `i < arr[n], j = arr[n]` as a test condition.  Also  enable all your compiler warnings to save time.

Comment: First things first. Format your code properly. Second. Why are you initializing an `int` array` with a quoted constants? It's not an error, but strange.

Comment: Third. You have a total mess with the array elements and their indices in the loop. You have to rule it out. Before you can do it properly, I'll suggest you to avoid complex constructs within the `for` statement.

Comment: @rakib Just make the input data all zeros - it will make it even simpler...

Comment: Just `sort` the data then it's easy.


Here is another answer: This should do the job for you. The main ingredient is the loop 

```    for (i = 0; i < n-1; i++)
    {
        if (arr[i] < arr[i+1])
            count++;
    }
```

Lets say you have 5 elements. The ```i``` will be evaluated from 0 to 3.
In that case, you are comparing ```arr[i]``` to ```arr[i+1]``` It means, the loop will be comparing
```arr[0]``` to ```arr[1]```, ```arr[1]``` to ```arr[2]```, ```arr[2]``` to ```arr[3]``` and finally ```arr[3]``` to ```arr[4]```, which is what you want to achieve.

```#include <stdio.h&gt;

#define MAX_ELEMENTS 10

int main(void)
{
    int arr[MAX_ELEMENTS] = { '\0' };
    int i = 0, n = 0, count = 0;   
    printf("\nEnter the number of elements in the array: ");
    scanf("%d", &amp;n);

    if (n &gt; MAX_ELEMENTS)
    {
        printf("Only %d elements supported\n", MAX_ELEMENTS);
        return -1;
    }

    printf("\nEnter the elements");
    for (i = 0; i < n; i++)
    {
        scanf("%d", &amp;arr[i]);
    }

    printf("\nThe array elements are: ");

    for (i = 0; i < n; i++)
    {
        printf("%d\t", arr[i]);        
    }

    for (i = 0; i < n-1; i++)
    {
        if (arr[i] < arr[i+1])
            count++;
    }
    printf("\nThe count is: %d", count);

    return 0;
}
```

Comment for this answer: Thank you for looking into it

Here is another answer: Your last for loop is way off. It is usually easier to increment the index into the array.
For example, lets say you have this array   :

```5 4 3 2 1
```

Your code does this:


Your for loop assigns: ```5``` to ```i``` and ```4``` to ```j```.   
Then you compare ```i``` and ```j``` (this is right for now)
Next you increment ```i``` and ```j```, so now they are ```6``` and ```5``` respectively. This is not what you want.


Also, the condition in your loop is wrong. It will break out of the loop once the value you're looking at (e.g. the first element) is larger than the last element. In my example array it would break right away. (```i=5``` is not less than ```arr[n]=1```.



Instead you should increment indexes into the array and break out of the loop when you reach the last index.

```for(i = 0, j = 1; j < n; i++, j++) {
    if(arr[i]<arr[j])
        count++;
}
```



You also want to make sure ```n``` is less than or equal to 10, otherwise you'll be writing outside the array.
Add either

```if(n &gt; 10) {
    n = 10;
}
```

or

```if(n &gt; 10) {
    printf("Max number of elements is 10.\n");
    return -1;
}
```

after you get ```n```.

Comment for this answer: @KlasLindbäck Oops. I meant to put `j`. I fixed it. Thanks.
Comment for this answer: Thank you for providing such a detailed solution.

Here is another answer: the problem is in the last for loop. you are doing something really strange for iterating.

to simplify:

```for(i=0;i<n;i++){
      if(arr[i]<arr[i+1])
          count++;
}
```

Comment for this answer: Loop condition needs to be `i+1 < n` since the code accesses `arr[i+1]`.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 7, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:09/28/16', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c>', 'Title': 'Program to display the count of the number of times the succeeding element is greater than the preceding element', 'ViewCount': 65, 'id': 39750196, 'user_AboutMe': None, 'user_AccountId': 8031559.0, 'user_DisplayName': 'StephenCurry30', 'user_Reputation': 1.0, 'user_Views': 3.0, 'user_id': 6056327.0}}