2

I was testing my code against some 10001 values, but it did not print any output value.

I found out it was not even printing out anything even when I am merely taking inputs using cin and printing using cout when the value is large(some thousands).

When I ran the same thing in www.ideone.com it worked, but in my own machine it is not printing anything.

The program is:

int main() {
    int N, x;
    cin >> N;
    int ar[N];

    for (int i = 0; i < N; ++i) {
        cin >> x;
        ar[i] = x;
    }
    for (int i = 0; i < N; ++i) {
        cout << ar[i] << " ";
    }
    cout << endl;
}

and the sample input can be downloaded in http://ideone.com/S3EneQ using copy.

I am using Ubuntu 14.04 and g++ (Ubuntu 4.8.2-19ubuntu1) 4.8.2 (2013).

Eric
  • 2,635
  • 6
  • 26
  • 66
  • I'm sorry, im wrong about some of what i wrote. Entering multiple numbers should count as multiple values for cin. I will remove my comments so they dont spread missinformation. :) – Jite Dec 21 '14 at 08:39
  • We're unlikely to be able to tell you why a properly written function that works in ideoone doesn't work on your computer. Have you tried using a debugger to see what's happening when it runs? – Barmar Dec 21 '14 at 08:46

1 Answers1

1

You're creating a large array on the stack with int ar[N] and this is a bad idea.

Replace int ar[N] with std::vector<int> ar(N).

By the way dynamically-sized arrays are supported as non-portable extensions because they're not part of the standard C++ language.

6502
  • 112,025
  • 15
  • 165
  • 265