-1

So i have a program that finds the count of all subsets of distinct even numbers but i keep getting that error: "expected unqalified-id before '{' token". Here's my code:

#include <iostream>
#include <bits/stdc++.h>

using namespace std;
{
        int countSubsets(int array[], int n)
        unordered_set<int> us;
        int even_count = 0;

    {
        for(int i=0; i<n; i++)
            if(array[i] % 2 == 0)
                us.insert(array[i]);

        unordered_set<int>:: iterator itr;
        for(itr=us.begin(); itr!=us.end(); itr++)
            even_count++;

        return(pow(2, even_count) - 1);
    }
    int main()
    {
        int array[] = {4, 2, 1, 9, 2, 6, 5, 3};
        int n = sizeof(array) / sizeof(array[0]);
        cout << "Number of subsets = "
            << countSubsets(array, n);
        return 0;
    }
}

Any suggestions?

EPATA
  • 3
  • 1

1 Answers1

0

Why you shouldn't use #include <bits/stdc++.h> and using namespace std;.

You have to border the body of your functions with curly braces {...}. In your example there are two function:

int countSubsets(int array[], int n) {
    FUNCTION_BODY
}

and

int main() {
    FUNCTION_BODY
}

In your case it's not necessary to use more curly braces outside of these function. After fixing this error, removing using namespace std; and #include <bits/stdc++.h>:

#include <iostream>
#include <unordered_set>
#include <cmath>

int countSubsets(int array[], int n) {
    std::unordered_set<int> us;
    int even_count = 0;

    for (int i = 0; i < n; i++)
        if (array[i] % 2 == 0) {
            us.insert(array[i]);
        }

    for (std::unordered_set<int>::iterator  itr = us.begin(); itr != us.end(); itr++)
        even_count++;

    return(std::pow(2, even_count) - 1);
}

int main() {
    int array[] = {4, 2, 1, 9, 2, 6, 5, 3};
    int n = sizeof(array) / sizeof(array[0]);
    std::cout << "Number of subsets = "
        << countSubsets(array, n);
    return 0;
}
Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62