Modulair program with (index/arrays)

I would start small. Create an array with 10 elements and read the numbers into it. Don't worry yet about the range and the uniqueness.
Then display the array to see if you got the input right.
Once you get this done modify the reading code so that it checks the range and the uniqueness
Then it might be the time to post your code and ask for help with the min and max functions.
Consider:

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

int main()
{
	const int noNums {10};
	const int minnum {1};
	const int maxnum {20};

	int nums[noNums] {};

	for (int i = 0; i < noNums; ++i) {
		int no {};

		auto fnd {std::begin(nums)};

		do {
			while ((std::cout << "Enter number " << i + 1 << " of " << noNums << ": ") && !(std::cin >> no)) {
				std::cout << "Not a number. Please enter a number\n";
				std::cin.clear();
				std::cin.ignore(1000, '\n');
			}

			if (no < minnum || no > maxnum)
				std::cout << "Number must be in range " << minnum << " - " << maxnum << ". Please enter another number\n";
			else
				if ((fnd = std::find(std::begin(nums), std::end(nums), no)) != std::end(nums))
					std::cout << "Number already entered. Please enter another number\n";
		} while (fnd != std::end(nums));

		nums[i] = no;
	}

	const auto [miniter, maxiter] {std::minmax_element(std::begin(nums), std::end(nums))};
	const auto smallpos {std::distance(std::begin(nums), miniter)};
	const auto largepos {std::distance(std::begin(nums), maxiter)};

	std::cout << "\nThe smallest number is " << nums[smallpos] << " at position " << smallpos << '\n';
	std::cout << "The largest number is " << nums[largepos] << " at position " << largepos << '\n';
}


An example:


Enter number 1 of 10: q
Not a number. Please enter a number
Enter number 1 of 10: 0
Number must be in range 1 - 20. Please enter another number
Enter number 1 of 10: 21
Number must be in range 1 - 20. Please enter another number
Enter number 1 of 10: 1
Enter number 2 of 10: 1
Number already entered. Please enter another number
Enter number 2 of 10: 2
Enter number 3 of 10: 3
Enter number 4 of 10: 4
Enter number 5 of 10: 5
Enter number 6 of 10: 6
Enter number 7 of 10: 7
Enter number 8 of 10: 8
Enter number 9 of 10: 9
Enter number 10 of 10: 10

The smallest number is 1 at position 0
The largest number is 10 at position 9

Last edited on
@MauriceF,
What do you mean by "modular" program, and by "at random"?

If by "modular" you mean split into functions then maybe
- a function to enter your no-duplicate array (it's not clear whether the user is to input his/her numbers or you - the programmer - are to supply 10 random numbers);
- a function to find the maximum and its index;
- a function to find the minimum and its index.

As @Thomas1965 wrote: start with the first one.
MauriceF, you need to work on uniqueness (each number used only once).

Also, you appear to be trying to output the memory addresses of max and min, not the indices of the relevant positions in the array.

Please use code tags: first item on the format menu.
Last edited on
Maybe someone knows another working program??


See my previous post.
Once you modify your program you can modularise it fairly easily.

For instance you could have function that checks whether a value meets the criteria of being in the range 0 to 20 and to make sure there are no duplicates, etc etc. You can translate the English to Dutch

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
/*
 The modular program to be designed requires you to enter ten numbers at random.
 The numbers are in the range of one to twenty and are allowed only once (sic)
 appearance. The program is looking for it (sic) largest number and smallest
 number from an array and these numbers must be displayed on the screen. Also
 the index numbers of the two numbers has to be shown in the display.
 */

#include <iostream>

using namespace std;

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++)
    {
        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;
            }
        }
    }
    
    min=a[0];
    max=a[0];
    
    int 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; // <--
        }
    }
    cout<<"Het grootste getal is: "<< max << "\nIndex grootstegetal: "<< i_max << endl; // <--
    cout<<"Het kleinste getal is: "<< min << "\nIndex kleinstegetal: "<< i_min << endl; // <--
    
    cin.get();
    
    return 0;
}
And this is what that looks like if you (automatically, so it can be simplified) refactor that code to 'modularise' 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
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;
}
Topic archived. No new replies allowed.