-2

When I run this code in VScode I don't get any error but the program doesn't take input and ends.

 #include <iostream>

using namespace std;

int main()
{
    int t, N, a, b, i, j, k, n, l = 1, m = 1;
    int number[N];
    cin >> t;
    for (k = 1; k <= t; k++)
    {
        cin >> N >> a >> b;
        for (i = 1; i <= N; i++)
        {
            cin >> number[i];
        }
        for (j = l; j <= N; j++)
        {
            if (number[n] % a != 0)
            {
                cout << "ALICE" << endl;
                break;
            }
            if (number[j] % a == 0)
            {
                l = j;
                break;
            }
        }
        for (n = m; n <= N; n++)
        {
            if (number[n] % b != 0)
            {
                cout << "BOB" << endl;
                break;
            }
            if (number[n] % b == 0)
            {
                m = n;
                break;
            }
        }
    }

    return 0;
}

Please explain why I am getting this error.

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
  • 7
    `int number[N];` ask yourself what value `N` is being used to create that non-standard VLA ? Hint: The only valid answer is "I have no idea", and with that you're in good company, because neither does your code. That isn't how you create dynamic vectors in C++. Include [``](https://en.cppreference.com/w/cpp/header/vector) and use [`std::vector`](https://en.cppreference.com/w/cpp/container/vector). – WhozCraig Jan 13 '21 at 18:30
  • @Milind Sharma Do you yourself know what language is used to write the program? Why did you use two language targs? This declaratgion int number[N]; does not make a sense at least because the variable N is uninitialized. – Vlad from Moscow Jan 13 '21 at 18:31
  • 1
    Indices in C++ start at 0, not 1. Such online coding competition sites mostly teach exceptionally bad practices and coding style. Better get a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Lukas-T Jan 13 '21 at 18:32
  • When the same code behaves differently for different compilers, _it's time to look for Undefined Behavior_. You have UB described in the first comment. – Drew Dormann Jan 13 '21 at 18:32
  • Please provide information on the problem in the post, not through a link – Damien Jan 13 '21 at 18:33
  • with the right settings this code does not compile: https://godbolt.org/z/5sEn5K. Warnings are your friends, make use of them – 463035818_is_not_an_ai Jan 13 '21 at 18:49
  • I got the point, the reason why I am getting an error because I have not defined the value of N. If I want to create a variable array then I have to use vector. – Milind Sharma Jan 13 '21 at 19:16
  • Sites such as codechef are **not** there to teach C++. Whatever questions they are asking you, the assumption is that whatever language you use to solve the problem, that you are well-versed in the computer language you're using -- enough to never have to ask a basic question about the language. – PaulMcKenzie Jan 13 '21 at 19:17
  • The best way to avoid a question ban is to ask good questions. I'll admit that's hard to do. Usually by the time I've gathered all the information I need in order to ask a good question, I've tripped over the solution. Here are a couple good links to help you frame a good question: https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/ and https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/ – user4581301 Jan 13 '21 at 19:49

1 Answers1

0

The reason you are getting the error because of the line int number[N]; Here N is not defined and N can have any garbage value. You have to either statically define the value of N which will result in memory leakage because you initially doesn't know how much length array is required to avoid this leakage you can use vectors for dynamic allocation.

For vector implementation you can refer this.

  • 3
    I wouldn't _create your own vector class/ dynamic array class_. Use `std::vector` instead. – 001 Jan 13 '21 at 18:53
  • 1
    your `DynamicArray` solves one problem but comes with more. There are lots of memory leaks – 463035818_is_not_an_ai Jan 13 '21 at 18:54
  • @JohnnyMopp yes I have already mentioned you can use inbuilt vectors, that was just a basic sample to get him familiar with how vectors works internally. – Vibhav Sharma Jan 13 '21 at 19:24
  • 1
    A beginner in C++ would know nothing about the code you posted -- what beginner would know what a copy constructor, assignment operator are? So I don't see the utility in posting it as an answer. If you were to give a mini tutorial on `std::vector`, then maybe that would be more worth it. – PaulMcKenzie Jan 13 '21 at 19:31
  • @PaulMcKenzie Thanks for the advice , I have recently started my journey on satckoverflow as a contributor so kinda new in this domain. – Vibhav Sharma Jan 13 '21 at 19:37
  • @VibhavSharma your desire to contribute is greatly appreciated! The code in this answer has bugs though. You may want to try making your first few answers small and to the point. – Drew Dormann Jan 14 '21 at 14:47
  • I agree that sometimes less is more. Appreciate your effort of providing some example implementation as well, but it has serious flaws that a beginner is likely to miss – 463035818_is_not_an_ai Jan 14 '21 at 14:55
  • @DrewDormann thanks for the advice , I have edited the answer and tried to make it to the point , your support is appreciated. – Vibhav Sharma Jan 14 '21 at 15:05
  • @largest_prime_is_463035818 I respect your concern, Thank you for guiding appropriately (; – Vibhav Sharma Jan 14 '21 at 15:07