Generating random numbers

My homework requires me to generate 30 random numbers between the range of 0 - 9.

My problem is the following;

I need to show which number appears the most, IE

If the random generator generated the number 7 the most times (5 times for example), I have to show;

Number(s) with the largest frequency of 5 (frequency being how many times the number appeared) is: 7

I do not know how to display which number appeared the most.

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
#include <ctime>
#include <iostream>
#include <iomanip>
#include <array>

using namespace std;

void Display(int *x);
void DisplayData(int *x);
void FindMaxMin(int *x);

struct NUM
{
	int n;
	int freq;
};

NUM ALL[10];

int main()
{
	srand(time(0));

	int random[30];

	for (int i = 0; i < 30; i++) //assign random numbers 0-9 to n
	{
		random[i] = rand() % 10;
	}

	Display(random); // show the random numbers assigned

	cout << endl;

	for (int i = 0; i < 10; i++) //set all numbers to integers from 0 - 9
	{
		ALL[i].n = i;
	}

	for (int i = 0; i < 30; i++)
	{
		
		ALL[random[i]].freq++;
	}

	DisplayData(random); //display the random numbers and their frequency 

	FindMaxMin(random);

	system("PAUSE");
	return 0;

}

void Display(int *x)
{
	for (int i = 0; i < 30; i++)
	{
		cout << i[x] << " ";
	}
}

void DisplayData(int *x)
{
	cout << "\tNUMBER \tFREQUENCY" << endl;
	for (int i = 0; i < 30; i++)
	{
		cout << char(196);
	}
	cout << endl;
	cout << setfill(' ');
	cout << fixed << showpoint << setprecision(2);
	for (int i = 0; i < 10; i++)
	{
		cout << left << setw(10) << ALL[i].n << right << setw(10) << ALL[i].freq;
		cout << endl;
	}
}

void FindMaxMin(int *x)
{
	int max = 0;
	int min = 9;

	for (int i = 0; i < 10; i++)
	{
		if (ALL[i].freq > max)
		{
			max = ALL[i].freq;
			if (x[i] == max)
			{
				cout << "Number(s) with the largest frequency of " << max << " is/are: " << x[i] << endl;
			}
		}
		if (ALL[i].freq < min)
		{
			min = ALL[i].freq;
			if (x[i] == min)
			{
				cout << "Number(s) with the lowest frequency of " << min << " is/are: " << x[i] << endl;
			}
		}
	}
}
Lines 90-93. You have not seen all the frequencies yet. You cannot know the maximum before the 87-94 loop has completed. Same with minimum.

You could replace type NUM with type unsigned int and simplify your code.
What do you mean, I have not seen all the frequencies yet?

My out put shows what is the highest and lowest frequency, my problem is that I do not know how to associate it with which number is the highest and lowest.

I cannot replace anything, it has to be this struct variable
You misunderstand. Your 'FindMaxMin()' function is trying to display the most/least frequent number before it has looked at all the numbers.

Look through all the numbers first, keeping track of whichever was the most/least frequent.

Your function should look something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
void FindMaxMin(NUM *xs, int size)
{
	NUM max = xs[0];
	NUM min = xs[0];

	for (...)
	{
		...
	}

	cout << "Most frequent is: " << max.n << endl;
	cout << "Least frequent is: " << min.n << endl;
}

If you want to consider the possibility that more than one number may have the minimum frequency, you'll have to either keep an array of min[], or sort ALL and peel off the first few items that have an equal frequency. Likewise for max.

For example:

1
2
3
4
5
	NUM maxes[10];
	int num_maxes = 0;
	maxes[num_maxes++] = xs[0];

	...

Hope this helps.
Topic archived. No new replies allowed.