0

My program is to find the smallest positive number missing from an array. With the following input I expect an output of 2.

6 
0
-9
1
3
-4
5

My problem is that it does not give any output. Can anyone explain this please?

#include <bits/stdc++.h>
using namespace std;
   
int main()
{
    int n;
    cin >> n;

    int array[n];

    for (int i = 0; i < n; i++)
    {
        cin >> array[n];
    }
    int const N = 1e4+2;
    bool indexarray[N];

    for (int i = 0; i < N; i++)
    {
        indexarray[i] = false;
    }
    for (int i = 0; i < n; i++)
    {

        if (array[i] > 0)
        {
            indexarray[array[i]] = true;
        }
    }
    int ans = -1;
    for (int i = 1; i < N; i++)
    {
        if (indexarray[i] == false)
        {
            ans = i;
            
        }
    }
    cout << ans << endl;
    return 0;
}

acraig5075
  • 10,588
  • 3
  • 31
  • 50
  • 1
    Run it in a debugger or add some intermediate cout statements to see how far you're getting. – Joseph Larson Apr 29 '21 at 18:38
  • _`int array[n];`_ isn't valid c++ code. – πάντα ῥεῖ Apr 29 '21 at 18:40
  • [A warning about `#include `](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). This is made worse with `using namespace std;` because now the entire C++ Standard library and its tens of thousands of identifiers are now effectively in the global namespace, turning the program into a minefield. Here's a simple example of how fast that can make things go wrong: https://godbolt.org/z/bhEnE66Th the only change is a the compiler was updated to the newest version and now the code fails to compile. – user4581301 Apr 29 '21 at 18:50
  • 3
    my rubber duck wants to know which element you are reading here `cin >> array[n];` – 463035818_is_not_an_ai Apr 29 '21 at 18:54
  • Could you please explain what problem this program is supposed to solve? – Bob__ Apr 29 '21 at 18:56
  • You are given an array and u have to find smallest positive number missing from array – Rohan Sarna Apr 29 '21 at 19:04
  • whatever resource you are using to learn c++, you should drop it immediately and look for something else. Here you can find a selection of [books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Any decent C++ introduction should tell you about `std::vector` which makes a solution for this 3-4 lines, but not much more – 463035818_is_not_an_ai Apr 29 '21 at 19:07

1 Answers1

1

I think because int array[n]; makes an array called array with n elements in it, with the first one starting at array[0]. cin >> array[n]; needs to modify array[n], but because the first element is array[0], the last element is array[n-1], and array[n] does not exist. Your code gave an error and exited.

Try changing

for (int i = 0; i < n; i++)
    {
        cin >> array[n];
    }

to

for (int i = 0; i < n; i++)
    {
        cin >> array[i];
    }

Also, I think variable length arrays are non-standard, so maybe try changing that. Replace it with std::vector<int> array(n) should work.

acraig5075
  • 10,588
  • 3
  • 31
  • 50
Max
  • 26
  • 2
  • Please consider mentioning that OP's code also lacks a `break;` in the last loop, to be effective. – Bob__ Apr 29 '21 at 19:24
  • All too often a program will not notice that an array was accessed out of bounds and will continue accessing invalid memory until it either crashes over accessing memory that is unavailable, crashes as a result on an invalid operation triggered by bad data read from invalid memory, or produces an incorrect result. I love it when programs crash because I know for sure that I've got a problem, but reading invalid memory has what's called [Undefined Behaviour](https://en.cppreference.com/w/cpp/language/ub) and when the behaviour is undefined, anything can happen, right down to appearing to work. – user4581301 Apr 29 '21 at 19:56