1

So I've written this to print "Yes when the code is in ascending order and "No" when it is not. But if I start with equal values in the beginning, the program prints an incorrect result. I don't understand why the if statement runs even if the condition isn't met.

#include <iostream>

using namespace std;

int main()
{
    int N, i;
    scanf("%d", &N);
    int arr[N];

    for(i = 1; i <= N; i++)
    {
        scanf("%d", &arr[i - 1]);
    }

    for(i = 1; i <= N; i++)
    {

        if(arr[i - 1] > arr[i])
        {
            printf("No");
            return 0;
        }
    }

    printf("Yes");

    return 0;
}
Blaze
  • 16,736
  • 2
  • 25
  • 44
  • 3
    `arr[i]` accesses the array out of bounds on the last iteration when `i == N`. – HolyBlackCat Jul 03 '19 at 11:34
  • 1
    your check loop goes until `i <= N` which is out of the bounds. – amlucas Jul 03 '19 at 11:34
  • 1
    fwiw, there is [`std::is_sorted`](https://en.cppreference.com/w/cpp/algorithm/is_sorted). is this an exercise? – 463035818_is_not_an_ai Jul 03 '19 at 11:34
  • 1
    Also note that arrays of non-constant size are not in the standard C++ and don't work on some compilers. Consider using `std::vector` instead. – HolyBlackCat Jul 03 '19 at 11:35
  • 1
    `int arr[N];` -- Your program won't sort anything, since this is not allowed in C++. – PaulMcKenzie Jul 03 '19 at 11:36
  • @PaulMcKenzie come on, it is not standard c++, but a compiler extension, as such it is not the nicest thing, but it does work on compilers that have the extension. – 463035818_is_not_an_ai Jul 03 '19 at 11:38
  • @formerlyknownas_463035818 I guess the many programmers who have Visual C++ are sad to hear that. – PaulMcKenzie Jul 03 '19 at 11:38
  • @PaulMcKenzie that's right, when I try to run someone's snippet I often have to replace the VLAs first. – Blaze Jul 03 '19 at 11:40
  • @Blaze That's just one of the drawbacks from using syntax like that, and that is painstakingly having to convert all of the usage of VLA's to vector if the helping hand uses Visual C++. Then imagine the post that's riddled with that syntax -- a good portion of whoever could help here just move on to another question. – PaulMcKenzie Jul 03 '19 at 11:43
  • @PaulMcKenzie In principle I do agree with you, even with a compiler that has the extension I do not see a single good reason to use it – 463035818_is_not_an_ai Jul 03 '19 at 12:01
  • 1
    You need to embrace zero based counting. Doing it any other way is pure madness. You also need to throw away your C++ learning material and get a good book from the [SO recommended list](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). There are no variable length arrays in C++, and `printf` and `scanf` are not in the `` header (and are not recommended anyway). – n. m. could be an AI Jul 03 '19 at 12:07

1 Answers1

1

You have an off by one error where you skip the first element of your array and go one past the last element. If your array has N elements, it goes from array[0] to array[N-1]. Change this here:

for(i = 1; i <= N; i++){

To this here:

for(i = 0; i < N; i++){

The second for(i = 1; i <= N; i++) where you do the check can start at 1 since you look at arr[i - 1] within, but it should be i < N instead of i <= N nevertheless.

Furthermore, int arr[N]; doesn't work in C++ (some compilers will tolerate it, but not all of them). Try std::vector<int> arr(N); instead.

Blaze
  • 16,736
  • 2
  • 25
  • 44