-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelement_uniqueness_problem.cpp
More file actions
66 lines (60 loc) · 1.13 KB
/
element_uniqueness_problem.cpp
File metadata and controls
66 lines (60 loc) · 1.13 KB
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
#include <iostream>
#include <stdlib.h>
using namespace std;
// Prototypes
bool checkUniqueness(int arraySize, int array_numbers[]);
bool checkUniqueness(int arraySize, int array_numbers[])
{
// Variables
int i;
int j;
for(i=0;i<arraySize-2;i++)
{
for(j=i+1;j<arraySize-1;j++)
{
if(array_numbers[i]==array_numbers[j])
{
return false;
}
}
}
return true;
}
int main()
{
// ---------- Algorithm 1 ----------
// Variables
int i;
int j;
int arraySize;
int range;
cout << "Input array's size = ";
cin >> arraySize;
// Arrays
int array_numbers[arraySize];
cout << "Input elements' range (0..x) = ";
cin >> range;
// assign array elements randomly
for(i=0;i<arraySize;i++)
{
array_numbers[i] = rand()%(range+1);
}
// print array
cout << "Array elements = ";
for(i=0;i<arraySize;i++)
{
cout << array_numbers[i] << " ";
}
cout << endl;
// checking uniqueness
if(checkUniqueness(arraySize, array_numbers))
{
cout << "All elements in the given array are distinct!" << endl;
}
else
{
cout << "All elements in the given array are NOT distinct!" << endl;
}
// --------------------
return 0;
}