0
using namespace std;
int i, t, n, x, a;
main()
{

    int t;
    cin >> t;

    while (t--)
    {
        cin >> n >> x;
        int b[n + x] = {0};
        while (n--)
            cin >> a, b[a] = 1; // ***here is my question
        for (i = 1; x > 0 || b[i] != 0; i++)
            if (b[i] == 0)
                x--;
        cout << i - 1  << endl ;
    }

    return 0;
}

Here, why "," has been used in " cin>> a , b[a] = 1 ;" I did not understand the purpose of using it.

Dihan
  • 13
  • 2
  • 4
    who wrote the code? Only the author knows why they wrote it like this. It is uncessarily obfuscated, I see no other purpose – 463035818_is_not_an_ai Aug 02 '21 at 15:13
  • Well.. Maybe the author hates curly braces **that** much, that they try to avoid it, wherever possible. – Algirdas Preidžius Aug 02 '21 at 15:13
  • Effectively, it's the same as `cin >> a; b[a] = 1;` , but in this case, prevents the need for curly brackets around the while. It is called a comma operator, fwiw ... also, don't use whatever resource you are using. The code posted is not valid c++ – ChrisMM Aug 02 '21 at 15:14
  • 2
    `int b[n + x] = {0};` is not standard C++, see [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard). And `main` must be declared to return `int` – 463035818_is_not_an_ai Aug 02 '21 at 15:14
  • the comma operator has some use cases, but most of the time the only reason it is present in code is a typo or a misunderstanding of how it works. This is certainly not a good use case for it, so I would bet on one of the latter options – 463035818_is_not_an_ai Aug 02 '21 at 15:15
  • 5
    Code like this is why you shouldn't learn programming from answers to competitive coding websites. – interjay Aug 02 '21 at 15:16

1 Answers1

5

This is an example of bad written code (make it weird and harder to read).

In C and C++ there is , operator where both arguments are evaluated and last one is returned as a value of whole expresion.

So this line:

cin >> a, b[a] = 1;

return value for operator, is discarded so cin >> a and b[a] = 1 are just evaluated.

This is crappy way to avoid use of parenthesis, this should be written like this:

        while (n--) {
            cin >> a;
            b[a] = 1;
        }

and I'm sure this you can read easily.

Marek R
  • 32,568
  • 6
  • 55
  • 140