Check if 15 integers are unique

Pages: 12
@gunnerfunner, I love your code, it’s far better than mine, easier to read and definitely more correct.
May I ask you why did you prefer size_t to int in the for loops? Many thanks for your answer!
Thanks for the help guys.

I previously wrote :
"I cannot, it is an array structure so I can't use it anyways. Which means I cannot use any (array) structures that represent a collection of data."

I wrapped (array) because it is not just limited to arrays, but also anything that represents a collection of data. That includes struct, class, std::set, pointers, std::string, linked list etc.

My instructor wants me to declare 15 integer variables and just use raw comparisons to check if they are unique.

I tested everyone's solution and have to say Mantorr22's solutions are incredible. I also conducted some research about "std::numeric_limits<int>::min()" so that he does not need to explain anymore.

I know that people try to have workarounds but sadly the assignment is harsh :(

Thank you all for helping and Mantorr22 for masterpiece solutions :)
why did you prefer size_t to int in the for loops

http://stackoverflow.com/questions/1951519/when-should-i-use-stdsize-t
anything that represents a collection of data
My instructor wants me to declare 15 integer variables and just use raw comparisons to check if they are unique.
Wow. What a moron.
Ooh. I know how to do it!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>

template <int N>
struct enter_n_ints{
    template <typename T>
    bool operator()(const T &uniqueness_check){
        int n;
        while (!(std::cin >> n));
        return enter_n_ints<N - 1>()(
            [=](int m){
                return n != m && uniqueness_check(m) && uniqueness_check(n);
            }
        );
    }
};

template <>
struct enter_n_ints<1>{
    template <typename T>
    bool operator()(const T &uniqueness_check){
        int n;
        while (!(std::cin >> n));
        return uniqueness_check(n);
    }
};

int main(){
    bool unique = enter_n_ints<15>()([](int){ return true; });
    if (unique)
        std::cout << "The integers are all unique.\n";
    else
        std::cout << "Some integers are duplicated.\n";
    return 0;
}
Last edited on
Helios, blame my teacher if you want.

Actually it is not an offical homework, it is an challenge made by our instructor. We score bonus points if we can overcome it, but they are optional. That is why the assignment's requirements are ridiculous.

You can call me whatever you want. Fine, you are an expert, since I am just an underling asking for help. I am fine if you are refering to someone else, thanks.

"I can use functions but I don't know how to express it." - This part is the hint from my instructor if you want it. I don't know lambda functions to be honest but I can understand the second example pretty well.

You don't have 15 integer variables, that is a problem but thank you for sharing your experience and you are as great as Mantorr22 :)
You can call me whatever you want.
I was saying your instructor is a moron.

You don't have 15 integer variables
Yes, there are! There must necessarily exist 15 integer variables on the stack at run time!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>

template <int N>
struct enter_n_ints{
    template <typename T>
    bool operator()(const T &uniqueness_check){
        int n;
        std::cout << "Enter an integer. Address of this variable: " << &n << std::endl;
        while (!(std::cin >> n));
        return enter_n_ints<N - 1>()(
            [=](int m){
                return n != m && uniqueness_check(m) && uniqueness_check(n);
            }
        );
    }
};

template <>
struct enter_n_ints<1>{
    template <typename T>
    bool operator()(const T &uniqueness_check){
        int n;
        std::cout << "Enter an integer. Address of this variable: " << &n << std::endl;
        while (!(std::cin >> n));
        return uniqueness_check(n);
    }
};

int main(){
    bool unique = enter_n_ints<15>()([](int){ return true; });
    if (unique)
        std::cout << "The integers are all unique.\n";
    else
        std::cout << "Some integers are duplicated.\n";
    return 0;
}
Last edited on
> You don't have 15 integer variables, that is a problem

With 15 different variables (not just 15 different objects), the program not only becomes shorter, but also becomes more generic:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

template < typename T > bool unique(T) { return true ; }

template < typename A, typename B, typename... R > bool unique( A a, B b, R... r )
{
    if( sizeof...(R) == 0 ) return a != b ;
    else return a != b && unique( a, r... ) && unique( b, r... ) ;
}

int main()
{
    int a, b, c, d, e, f, g, h, i, j, k, m, n, p, q ;
    std::cout << "enter 15 integers\n" ;
    std::cin >> a >> b >> c >> d >> e >> f >> g >> h >> i >> j >> k >> m >> n >> p >> q ;

    const bool result = unique( a, b, c, d, e, f, g, h, i, j, k, m, n, p, q ) ;
    std::cout << "\nare these 15 unique numbers? " << ( result ? "yes\n" : "no\n" ) ;
}

The check on line 7 is redundant.
In the absence of a non-variadic binary overload, the check on line 7 is not redundant.
http://coliru.stacked-crooked.com/a/acf269866c666c86

I mean you can remove line 7 entirely, plus the else on line 8:
1
2
3
4
template < typename A, typename B, typename... R > bool unique( A a, B b, R... r )
{
    return a != b && unique( a, r... ) && unique( b, r... ) ;
}
> I mean you can remove line 7 entirely, plus the else on line 8

Yes. && unique( a, r... ) && unique( b, r... ) ; would be optimised away if sizeof...(r) is zero.
Wow thanks JLBorges. This is the shortest I have seen :)
:)
Topic archived. No new replies allowed.
Pages: 12