0

How to input array here? i.e the user has to give the 5 values for arr[] instead of arr[] = (0, -1, 2, -3, 4} . If I give arr[] it shows error. I'm a begginer so please help me.

using namespace std; // function to print triplets with 0 sum 

void findTriplets(int arr[], int n) 
{

bool found = false;

    for (int i=0; i<n-1; i++)
    {
        // Find all pairs with sum equals to
        // "-arr[i]"
        unordered_set<int> s;
        for (int j=i+1; j<n; j++)
        {
            int x = -(arr[i] + arr[j]);
            if (s.find(x) != s.end())
            {
                printf("%d %d %d\n", x, arr[i], arr[j]);
                found = true;
            }
            else
                s.insert(arr[j]);
        }
    }

    if (found == false)
        cout << " No Triplet Found" << endl; }

int main()

{

    int arr[] = {0, -1, 2, -3, 1};

    int n = sizeof(arr)/sizeof(arr[0]);

    findTriplets(arr, n);

    return 0; }
  • 1
    Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please [take the tour](http://stackoverflow.com/tour) and [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). Lastly please learn how to create a [Minimal, **Complete**, and **Verifiable** Example](http://stackoverflow.com/help/mcve). – Some programmer dude Jan 17 '18 at 14:06
  • Also please read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – Some programmer dude Jan 17 '18 at 14:06
  • 3
    Lastly, stop going to code competition sites or online judges. That won't help you learn to program C++. Instead [get a couple of good beginners books](https://stackoverflow.com/a/388282/440558) to read. Or go to school/college/university. – Some programmer dude Jan 17 '18 at 14:09
  • 1
    @AditiRawat It's not. The OP probably just pressed the `c` key and saw something pop up and selected the first tag seen. – Some programmer dude Jan 17 '18 at 14:10

1 Answers1

0

The simplest way is to get n from user and cin arr in a loop:

int n;
cin >> n;
int arr[n];

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

findTriplets(arr, n);

If you don't want to pass the size of array you can use std::vector instead of array, and the push_back() method;