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
|
#include <iostream>
using namespace std;
void input_number(int*, int, int&, int*, bool&);
void get_min_max(int*, int, int&, int&, int&, int&);
void display_results(int i_max, int i_min, int max, int min);
int main ()
{
int min, max;
const int aantalGetallen = 10;
int a[aantalGetallen];
const int n = 21;
int number_checker[n];
for(int i = 0; i < 20; i++)
number_checker[i] = 0;
bool number_rejected = true;
int number = 0;
cout << "Voer 10 getallen tussen de 0 en de 20 in:\n";
for (int i=0;i< aantalGetallen;i++)
{
input_number(a, i, number, number_checker, number_rejected);
}
int i_max;
int i_min;
get_min_max(a, aantalGetallen, i_max, i_min, max, min);
display_results(i_max, i_min, max, min); // <--
cin.get();
return 0;
}
void input_number
(int *a, int i, int &number, int *number_checker, bool &number_rejected)
{
number_rejected = true;
while( number_rejected == true) // <--
{
cout << i << ": ";
cin >> number;
if( number >= 0 && number <= 20 && number_checker[number] == 0 )
{
a[i] = number;
number_checker[number] = 1;
number_rejected = false;
}
else
{
cout << "Number is rejected ... try again\n";
number_rejected = true;
}
}
}
void get_min_max(int *a, int aantalGetallen, int &i_max, int &i_min, int &max, int &min)
{
min=a[0];
max=a[0];
i_max = 0; i_min = 0;
for(int i=1;i< aantalGetallen;i++)
{
if(min>a[i])
{
min=a[i];
i_min = i; // <--
}
else if(max<a[i])
{
max = a[i];
i_max = i; // <--
}
}
}
void display_results(int i_max, int i_min, int max, int min)
{
cout<<"Het grootste getal is: "<< max << "\nIndex grootstegetal: "
<< i_max << endl; // <--
cout<<"Het kleinste getal is: "<< min << "\nIndex kleinstegetal: "
<< i_min << endl;
}
|