0
#include <iostream>
using namespace std;

void aUnionB(int A[], int B[], int a, int b)
{
    int n = a + b;
    int aUb[n]{0}; // n is max num of elements aUb can have

    // filling elements of a in aUb
    for (int i = 0; i < a; i++)
    {
        aUb[i] = A[i];
    }
    
    int z = sizeof(aUb) / sizeof(aUb[0]);
    int temp = z;
    // compare element from set B with aUb and if not fount add it in aUb
    for (int i = 0; i < z; i++)
    {
        for (int j = 0; j < b; j++)
        {
            if (aUb[i] == B[j])
            {
                continue;
            }
            else
            {
                aUb[temp] = B[j];
                temp++;
             }
        }
    }
    //print a union b
    for (int i : aUb)
    {
        cout << i << " ";
    }
}
    
int main()
{
    int TestCases = 1, NoOfElementsInA, NoOfElementsInB, element;
 
    while (TestCases--) //testing for just one test case
    {
        cin >> NoOfElementsInA >> NoOfElementsInB;
        int A[NoOfElementsInA], B[NoOfElementsInB];
    
        //assigning elements in array A
        for (int i = 0; i < NoOfElementsInA; i++)
        {
            cin >> element;
            A[i] = element;
        }
        //assigning elements in array B
        for (int i = 0; i < NoOfElementsInB; i++)
        {
            cin >> element;
            B[i] = element;
        }
    
        aUnionB(A, B, NoOfElementsInA, NoOfElementsInB);
    }
    return 0;
}

I am trying for past 1 hour what's the problem but unable to find why the program is not running despite not having any error or warning after execution , I'm not able to pass inputs.

This the numerical question:

The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case consist of three lines. The first line of each test case contains two space separated integers N and M, where N is the size of array A and M is the size of array B. The second line of each test case contains N space separated integers denoting elements of array A. The third line of each test case contains M space separated integers denoting elements of array B.

samgakhyeong
  • 534
  • 5
  • 7
  • 4
    The *first* thing I would do is stop using VLAs in C++. They're non-standard at-best, and depending on the values for their sizing, a recipe for disaster. Use `std::vector` instead. Doing so for *all* your VLAs in this code will alter it significantly. The second thing I would do is stop assuming all your IO just works. There is exactly *zero* places in this code where IO operations are confirmed. Finally, if you can, why not just use a `std::unordered_set` in the first place. – WhozCraig Nov 28 '20 at 08:56
  • I am new to programming and just started my learning so I have very little to no knowledge I picked this question on basis of question tag (Arrays) and started to solve it. I will definitely see whats the unordered_ser and try to solve it again. Any other suggestion to tackle this question is appreciated since I'm in a new learning phase and discovering the language. – Aniket Ujgare Nov 28 '20 at 09:05
  • 2
    I suggest you reconsider resources you use for learning. Those resources that suggest `void aUnionB(int A[], int B[], int a, int b)` and `int aUb[n]{0};` won't teach you any good. Start with a [good introductory book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) instead. – Evg Nov 28 '20 at 09:15
  • 3
    *I picked this question on basis of question tag (Arrays)* -- You cannot learn C++ from online competition websites. Learning C++ is not what those websites are designed for. Also, [variable length arrays are not part of C++](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard), as this: `int n = a + b; int aUb[n]{0};` is not valid C++. – PaulMcKenzie Nov 28 '20 at 09:17
  • I cannot reproduce your issue. I ran it on cpp.sh, and I successfully entered numbers and got some output. Note that the number of test cases is NOT an input. Also, the output is some numbers, but probably not what you intended (but that is not your question) – P. Saladin Nov 29 '20 at 20:17

0 Answers0