requesting help with uniqueness

Hi all,

Back again with another thing im not sure how to approach.

I've written a program in wich you randomly type 10 numbers between 0 to 20, the program will show the lowest number and the highest number.

Now I would like to wirte a code in it which prevents it from type a number more than once. lets see two times number 1 shouldnt be possible.

Also i would like the program to show the index of te lowest and the highest number. But thats a thing i just seem to fail in everytime i try to.

I hope some of you is willing to help again!


here is my code:

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

using namespace std;

int start();
int minimaal();
int maximaal();
const int aantalgetallen = 10;
int a[aantalgetallen];
int x;
int y;


int main()
{
	
	start();
	
	minimaal();
	
	cout << "Het kleinste getal is:\n\n" << x << endl;

	maximaal();

	cout << "\nHet grootste getal is:\n\n" << y << endl;
	

	return 0;

}

int start()
{
	cout << "Voer 10 willekeurig gekozen getallen in tussen de 0 en 20:\n";

	

	for (int i = 0; i < 10; i++)
	{

		cout << "Getal " << i + 1 << " : ";
		cin >> a[i];
		
		
		if (a[i] >= 21)
		{
			cout << "Kies een getal tussen de 0 en 20!\n";
			cout << "U kunt niet lezen, begin opnieuw\n";
			start();
		}
			
	}

	return 0;

}

int minimaal()
{
	int min;
	
	min = a[0];
	
	for (int i = 0; i < 10; i++)
	{
		if (min > a[i])
			min = a[i];
	}

	x = min;
	
	return (x);
	

}

int maximaal()
{
	int max;

	max = a[0];

	for (int i = 0; i < 10; i++)
	{
		if (max < a[i])
			max = a[i];
	}

	y = max;
	
	return (y);
}
Last edited on
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 <limits>
#include <algorithm>

int main()
{
	constexpr int nonums {10};
	constexpr int minum {0};
	constexpr int maxnum {20};

	int nums[nonums] {};
	bool dup[maxnum - minum + 1] {};

	for (int n = 0; n < nonums; ++n) {
		while (((std::cout << "Please enter number " << n + 1 << ": ") && !(std::cin >> nums[n])) || nums[n] < minum || nums[n] > maxnum || dup[nums[n]]) {
			std::cout << "Invalid number\n";
			std::cin.clear();
			std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
		}

		dup[nums[n]] = true;
	}

	const auto [minpos, maxpos] {std::minmax_element(std::begin(nums), std::end(nums))};

	std::cout << "The minimum number is " << nums[minpos - std::begin(nums)] << " at position " << minpos - std::begin(nums) << '\n';
	std::cout << "The maximum number is " << nums[maxpos - std::begin(nums)] << " at position " << maxpos - std::begin(nums) << '\n';
}

Seeplus,

Thanks for your reply!
Ive been looking at it to see some logic and trying to combine it in my own code, but i cant figure out what your code does.

Could you explain it a bit please?
There is a C++ std:: function minmax_element which returns the 'position' of the min and max elements - based upon iterators. See http://www.cplusplus.com/reference/algorithm/minmax_element/

For an array, std::begin() and std::end() return the begin and end iterator. To get the position in the array, subtract the returned value from std::begin().

const auto [minpos, maxpos] {...} is a structured binding (requires C++17) that enables the returned pair from minmax_element() to directly set the values of minpos and maxpos and also deduce their type.

dup is an array that has an index of the number entered. If the element is true, the number has already been entered. If false then it's a unique number so set element to true.

std::cout in this context effectively returns true. std::cin effectively returns false if a number hasn't been entered, so !std:cin gives true if a number hasn't been entered. So for a bad number, true && true gives true, so the body of the while loop is entered. If the && is false - a number has been entered - then it is checked to see if < minum or > maxnum or is a duplicate. if any of these is true, the body of the while loop is entered.

The while loop body first displays a message. If the problem was that something other than a number was entered, then the std::cin stream state is set to fail. This needs to be reset using .clear(). Also any chars remaining in the stream need to be removed so that they are not used again - hence .ignore() which removes and ignores all remaining stream chars up-to and including a '\n'.

Hope that helps.
If you want to do this without using std::minmax_element,

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>
#include <limits>

int main()
{
	constexpr int nonums {10};
	constexpr int minum {0};
	constexpr int maxnum {20};

	int nums[nonums] {};
	bool dup[maxnum - minum + 1] {};

	int minpos {0};
	int maxpos {0};

	for (int n = 0; n < nonums; ++n) {
		while (((std::cout << "Please enter number " << n + 1 << ": ") && !(std::cin >> nums[n])) || nums[n] < minum || nums[n] > maxnum || dup[nums[n]]) {
			std::cout << "Invalid number\n";
			std::cin.clear();
			std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
		}

		dup[nums[n]] = true;

		if (nums[minpos] > nums[n])
			minpos = n;

		if (nums[maxpos] < nums[n])
			maxpos = n;
	}

	std::cout << "The minimum number is " << nums[minpos] << " at position " << minpos << '\n';
	std::cout << "The maximum number is " << nums[maxpos] << " at position " << maxpos << '\n';
}

Thanks a lot again seeplus.

Im going to compare our programs and look how to combine them.
your explanation helps a lot.

Topic archived. No new replies allowed.