Check if 15 integers are unique

Pages: 12
I am honestly stuck and I am required to have **15 integer variables** and input them and after that I have to verify if all 15 of them are unique. I am not allowed to use any array structures.

If I do it by if statements there seem to be no end to it. Even though I can use functions, I don't know how to express it, and my instructor just wants it to be as short as possible. Can anyone help?
Can you use vectors ?
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.
Hello fasravo,

I have tried two possibilities with the second being the shortest. I only used 5 variables so it would need to be expanded. Notice where each loop starts and ends.

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
37
38
#include <iostream>

int main()
{
	int number1{ 2 }, number2{ 4 }, number3{ 6 }, number4{ 4 }, number5{ 8 };
	int a{ 2 }, b{ 4 }, c{ 6 }, d{ 4 }, e{ 8 };
	bool fail{ false };


	if (number1 == number2 || number1 == number3 || number1 == number4 || number1 == number5)
		fail = true;
	else if (number2 == number3 || number2 == number4 || number2 == number5)
		fail = true;

	if (fail)
	{
		std::cout << "\n Two of the numbers you have entered are the same";
		std::cout << "\n These numbers must be unique." << std::endl;
	}
	else
		std::cout << "\n All numbers are unique." << std::endl;

	fail = false;

	std::cout << "\n for loop" << std::endl;

	for (char lpo = 'a'; lpo < 'e'; lpo++)
		for (char lpi = lpo + 1; lpi < 'f'; lpi++)  // <--- Changed form 'b' to "lpo + 1".
			if (lpo == lpi)
				fail = true;
	if (fail)
	{
		std::cout << "\n Two of the numbers you have entered are the same";
		std::cout << "\n These numbers must be unique." << std::endl;
	}
	else
		std::cout << "\n All numbers are unique." << std::endl;
}


Hope that helps,

Andy

Edit Revised code. Changed the first part of the inner while loop.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

int main()
{

    int ar[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 11, 12, 13, 14 , 15};
    bool match = false;
    for (size_t i = 0; i < sizeof(ar)/sizeof(ar[0]); ++i)
    {
        for (size_t j = i + 1; j < sizeof(ar)/sizeof(ar[0]); ++j)
        {
            if (ar[i] == ar[j])
            {
                match = true;
                break;
            }
        }
    }
    std::cout << std::boolalpha << match;
}
@gunnerfunner

The point of the assignment is not to use any kind of array. That is why I came up with my two solutions.

Andy
Does std::set fulfill the "not an array" demand?
Yeah Andy, you're right indeed This teacher is particularly sadistic I must say, 15 variables w/o a data structure!!
Last edited on
To All,

UPDATE

I do not thin my for loop solution is working properly. The idea was to compare variable names, but I think it is comparing letters instead. I will check into this shortly.

Andy
You can store the numbers in std::string and then check if each 'word' in the string is unique:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

constexpr auto SIZE = 15;

int main()
{
    std::string numbers{};
    std::string temp{};
    size_t i{};
    while (i < SIZE)
    {
        std::cout << "Enter number \n";
        getline(std::cin, temp);
        numbers += temp + " ";
        ++i;
    }
    std::cout << numbers << "\n";
    //now check if each 'word' in numbers in unique ... 

}

but what are the chances an assignment that forbids arrays would allow this
Last edited on
Try changing one of the numbers.

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
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
using namespace std;

//===========================

void rotate( int &a, int &b, int &c, int &d, int &e, int &f, int &g, int &h, int &i, int &j, int &k, int &l, int &m, int &n, int &o )
{
   int temp = a;
   a = b;
   b = c;
   c = d;
   d = e;
   e = f;
   f = g;
   g = h;
   h = i;
   i = j;
   j = k;
   k = l;
   l = m;
   m = n;
   n = o;
   o = temp;
}

//===========================

int main()
{
   int a = 1, b = 2, c = 3, d = 4, e = 5, f = 6, g = 7, h = 8, i = 9, j = 10, k = 11, l = 12, m = 13, n = 14, o = 15;
   int aa= a, bb= b, cc= c, dd= d, ee= e, ff= f, gg= g, hh= h, ii= i, jj= j , kk= k , ll= l , mm= m , nn= n , oo= o ;
   bool unique = true;

   for ( int x = 1; x < 15; x++ )
   {
      rotate( aa, bb, cc, dd, ee, ff, gg, hh, ii, jj, kk, ll, mm, nn, oo );
      if ( aa == a || bb == b || cc == c || dd == d || ee == e ||
           ff == f || gg == g || hh == h || ii == i || jj == j ||
           kk == k || ll == l || mm == m || nn == n || oo == o    )
      {
         unique = false;
         break;
      }
   }

   cout << "Unique? " << boolalpha << unique;
}
This can be done cleanly using lambda function. So take advantage of it.

Something like this, perhaps :
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
#include <limits>

int main()
{
    int v_01, v_02, v_03, v_04, v_05, v_06, v_07, v_08, v_09, v_10, v_11, v_12, v_13, v_14, v_15; 

    std::cout << "Enter 15 values : " << std::endl;    
    
    if(true)
    {
        v_01 = 1; std::cout << v_01 << std::endl;
        v_02 = 2; std::cout << v_02 << std::endl;
        v_03 = 3; std::cout << v_03 << std::endl;
        v_04 = 4; std::cout << v_04 << std::endl;
        v_05 = 5; std::cout << v_05 << std::endl;
        v_06 = 6; std::cout << v_06 << std::endl;
        v_07 = 7; std::cout << v_07 << std::endl;
        v_08 = 8; std::cout << v_08 << std::endl;
        v_09 = 9; std::cout << v_09 << std::endl;
        v_10 = 10; std::cout << v_10 << std::endl;
        v_11 = 11; std::cout << v_11 << std::endl;
        v_12 = 12; std::cout << v_12 << std::endl;
        v_13 = 13; std::cout << v_13 << std::endl;
        v_14 = 14; std::cout << v_14 << std::endl;
        v_15 = 15; std::cout << v_15 << std::endl;
    }
    else
    {
        std::cin >> v_01;
        std::cin >> v_02;
        std::cin >> v_03;
        std::cin >> v_04;
        std::cin >> v_05;
        std::cin >> v_06;
        std::cin >> v_07;
        std::cin >> v_08;
        std::cin >> v_09;
        std::cin >> v_10;
        std::cin >> v_11;
        std::cin >> v_12;
        std::cin >> v_13;
        std::cin >> v_14;
        std::cin >> v_15;        
    }
    
    auto is_unique = [&](int &being_checked)
    {
        // Save the value
        int i = being_checked;
        
        // The value is currently being checked
        being_checked = std::numeric_limits<int>::min();
        
        bool unique = true;
        
        if(false) unique = unique;
        else if(i == v_01) unique = false;
        else if(i == v_02) unique = false;
        else if(i == v_03) unique = false;
        else if(i == v_04) unique = false;
        else if(i == v_05) unique = false;
        else if(i == v_06) unique = false;
        else if(i == v_07) unique = false;
        else if(i == v_08) unique = false;
        else if(i == v_09) unique = false;
        else if(i == v_10) unique = false;
        else if(i == v_11) unique = false;
        else if(i == v_12) unique = false;
        else if(i == v_13) unique = false;
        else if(i == v_14) unique = false;
        else if(i == v_15) unique = false;
        
        // Restore the original value when done
        being_checked = i;
        return unique;        
    };
    
    bool unique(is_unique(v_01));
    unique = unique && is_unique(v_02);
    unique = unique && is_unique(v_03);
    unique = unique && is_unique(v_04);
    unique = unique && is_unique(v_05);
    unique = unique && is_unique(v_06);
    unique = unique && is_unique(v_07);
    unique = unique && is_unique(v_08);
    unique = unique && is_unique(v_09);
    unique = unique && is_unique(v_10);
    unique = unique && is_unique(v_11);
    unique = unique && is_unique(v_12);
    unique = unique && is_unique(v_13);
    unique = unique && is_unique(v_14);
    unique = unique && is_unique(v_15);
    
    if(unique) 
    {
        std::cout << "15 values are unique" << std::endl;
    }
    else
    {
        std::cout << "15 values are NOT unique" << std::endl;        
    }
    
    return 0;    
}


Enter 15 values :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
15 values are unique

http://rextester.com/BWJJ19239
You can also do it the function way, but in order to avoid repetitive code, macro is extremely useful here since your teacher wants your code to be as short as possible.

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <limits>

#define ARGUMENTS v_01, v_02, v_03, v_04, v_05, v_06, v_07, v_08, v_09, v_10, v_11, v_12, v_13, v_14, v_15

bool is_unique (int &being_checked, int &v_01, int &v_02, int &v_03, int &v_04, int &v_05, 
                int &v_06, int &v_07, int &v_08, int &v_09, int &v_10, 
                int &v_11, int &v_12, int &v_13, int &v_14, int &v_15
)
{
    // Save the value
    int i = being_checked;
        
    // The value is currently being checked
    being_checked = std::numeric_limits<int>::min();
        
     bool unique = true;
        
    if(false) unique = unique;
    else if(i == v_01) unique = false;
    else if(i == v_02) unique = false;
    else if(i == v_03) unique = false;
    else if(i == v_04) unique = false;
    else if(i == v_05) unique = false;
    else if(i == v_06) unique = false;
    else if(i == v_07) unique = false;
    else if(i == v_08) unique = false;
    else if(i == v_09) unique = false;
    else if(i == v_10) unique = false;
    else if(i == v_11) unique = false;
    else if(i == v_12) unique = false;
    else if(i == v_13) unique = false;
    else if(i == v_14) unique = false;
    else if(i == v_15) unique = false;
        
    // Restore the original value when done
    being_checked = i;
    return unique;        
};


int main()
{
    int v_01, v_02, v_03, v_04, v_05, v_06, v_07, v_08, v_09, v_10, v_11, v_12, v_13, v_14, v_15; 

    std::cout << "Enter 15 values : " << std::endl;    
    
    if(true)
    {
        v_01 = 11; std::cout << v_01 << std::endl;
        v_02 = 22; std::cout << v_02 << std::endl;
        v_03 = 35; std::cout << v_03 << std::endl;
        v_04 = 44; std::cout << v_04 << std::endl;
        v_05 = 52; std::cout << v_05 << std::endl;
        v_06 = 63; std::cout << v_06 << std::endl;
        v_07 = 74; std::cout << v_07 << std::endl;
        v_08 = 81; std::cout << v_08 << std::endl;
        v_09 = 94; std::cout << v_09 << std::endl;
        v_10 = 105; std::cout << v_10 << std::endl;
        v_11 = 112; std::cout << v_11 << std::endl;
        v_12 = 121; std::cout << v_12 << std::endl;
        v_13 = 135; std::cout << v_13 << std::endl;
        v_14 = 142; std::cout << v_14 << std::endl;
        v_15 = 151; std::cout << v_15 << std::endl;
    }
    else
    {
        std::cin >> v_01;
        std::cin >> v_02;
        std::cin >> v_03;
        std::cin >> v_04;
        std::cin >> v_05;
        std::cin >> v_06;
        std::cin >> v_07;
        std::cin >> v_08;
        std::cin >> v_09;
        std::cin >> v_10;
        std::cin >> v_11;
        std::cin >> v_12;
        std::cin >> v_13;
        std::cin >> v_14;
        std::cin >> v_15;        
    }
    
    
    bool unique(is_unique(v_01, ARGUMENTS));
    unique = unique && is_unique(v_02, ARGUMENTS);
    unique = unique && is_unique(v_03, ARGUMENTS);
    unique = unique && is_unique(v_04, ARGUMENTS);
    unique = unique && is_unique(v_05, ARGUMENTS);
    unique = unique && is_unique(v_06, ARGUMENTS);
    unique = unique && is_unique(v_07, ARGUMENTS);
    unique = unique && is_unique(v_08, ARGUMENTS);
    unique = unique && is_unique(v_09, ARGUMENTS);
    unique = unique && is_unique(v_10, ARGUMENTS);
    unique = unique && is_unique(v_11, ARGUMENTS);
    unique = unique && is_unique(v_12, ARGUMENTS);
    unique = unique && is_unique(v_13, ARGUMENTS);
    unique = unique && is_unique(v_14, ARGUMENTS);
    unique = unique && is_unique(v_15, ARGUMENTS);
    
    if(unique) 
    {
        std::cout << "15 values are unique" << std::endl;
    }
    else
    {
        std::cout << "15 values are NOT unique" << std::endl;        
    }
    
    return 0;    
}


http://rextester.com/DFDBA43574

.....
Last edited on
gunnerfunner wrote:
Yeah Andy, you're right indeed This teacher is particularly sadistic I must say, 15 variables w/o a data structure!!

You must be thinking about an endless series of if-statements so you are out of mind. Your code doesn't even cover 15 integers at all.
Mantorr22: Now modify your code for ten thousand integers.
@helios
Thanks, I guess it is a little bit tiring to type them all after all.
If you really want the code, please PM me and I will give you the solution.

@gunnerfunner
I see, you are angry now. Mind you explain about that?
What if that professor were not a sadistic? :-)
Is it possible that there’s a trick in the exercise? For example, a pointer, in my opinion, cannot be classified as a data structure.
So, what if we avoided all the array-like syntax (I mean the operator []), but we use a pointer?
Something like this:
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>

using std::cout;
using std::endl;

int main()
{
   int* address_of_p, *p = (int*)malloc(sizeof(int*) * 16);
   address_of_p = p;
   for(int i{0}; i<15; i++)
      *p++ = i;

   p = address_of_p;
   for(int i{0}; i<15; i++) {
      int* anotherp = address_of_p;
      for(int j{0}; j<15; j++) {
         if( i == j ) {
            anotherp++;
            continue;
         }
         cout << "variable " << i << ": " << *p 
               << "; variable " << j << ": " << *anotherp;
         if( *p == *anotherp ) {
            cout << " --> variable are EQUAL!!!!" << endl;
         } else {
            cout << " --> variable are different" << endl;
         }
         anotherp++;
      }
      p++;
   }
   
   free(address_of_p);

   return 0;
}

Ouch! Terrible ugly, but… what the rationale behind giving an exercise so simple in the logic, but so huge in the rows of code?
closed account (48T7M4Gy)
What's to stop you using a linked list? ( Or perhaps even the equivalent to a hash table? )
closed account (48T7M4Gy)
Again, using pointers a tree arrangement (many possible types here) would presumably do the job efficiently and would enable an almost unlimited number of nodes/numbers.
Enoizat: I like your approach, one could argue that array deprecates to a pointer which is not a data-structure, ergo ... but then others will say it's an array to start with but it is still innovative and I notice you've avoided carefully using the [] operator. Your code with a few edits:
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
#include <iostream>
#include <cstdlib>
constexpr auto SIZE = 15;

int main()
{
    int* p = (int*)malloc(sizeof(int) * (SIZE ));//sizeof(int)
    bool match = false;

    for(size_t i{}; i < SIZE; i++)
    {
        std::cout << "Enter number: \n";
        std::cin >> *(p+i);
    }
    for (size_t i {}; i < SIZE; ++i)
    {
        for (size_t j = i + 1; j < SIZE; ++j)
        {
            if (*(p + i ) == *(p + j))
            {
                match = true;
                break;
            }
        }
    }
    std::cout << std::boolalpha << match;
    free(p);
}

Last edited on
Pages: 12