Frequency?

It is not outputting the lowest and largest frequency part correctly. How do I fix the part at the end? I tried doing it without if statements but got errors.

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

using namespace std;

void DisplayNum(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 x[30];

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

	DisplayNum(x); // 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[x[i]].freq++;
	}

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

	system("PAUSE");
	return 0;

}

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

void DisplayData(int *x)
{
	cout << "NUMBER" << setw(20) << "FREQUENCY" << 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;
			}
		}
	}
}
Last edited on
You don't initialize the NUM::freq anywhere. You should How much is undefined+1? Undefined.

You do need two passes. The first pass determines the min and max frequencies.

The second pass lists the values that have extreme frequency. You cannot do that before you have completed the first pass.


Note on line 81. If all 30 x have same value, then the min and max frequency are both 30. Your min remains 9 ...
Last edited on
Um, so how do I complete the pass then I'm kind of confused.
Also, on line 81 I sort of understand what you're saying but how would I fix that part?
?
Make one pass through the frequencies to determine the min and max. The trick here is that you want to record the INDEX of the min/max value, not the value itself. That way you can us the index to get both the value and the frequency:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
        int max = 0;
        int min = 0;

        for (int i = 0; i < 10; i++)
        {
                if (ALL[i].freq > ALL[max].freq)
                {
                    max = i;
                }
                if(ALL[i].freq < ALL[min].freq)
                {
                    min = i;
                }
        }


After this loop you can print out the value:
1
2
3
4
        cout << "Number(s) with the largest frequency of "
             << ALL[max].freq << " is/are: " << max << endl;
        cout << "Number(s) with the lowest frequency of "
             << ALL[min].freq << " is/are: " << min<< endl;

What if there's a tie for min/max frequency? It looks like you want to print out all the values. To do this you'll have to replace the code above with a second loop that goes through ALL again and prints out the ones whose freq == max. Then do another loop that prints out the values whose freq==min. You should probably put that in a function:
1
2
3
4
DisplayMatching(int freq)
{
    // go through ALL and print the values whose frequency is freq.
}


Some other comments.
Line 4: You don't need the array header file.
Line 57. It would be much clearer to write x[i] instead of i[x].

You don't actually struct NUM at all. You could just do int ALL[10]; Where the index is the digit and the value is the frequency.
Ok, thanks. I think I got it now.
Topic archived. No new replies allowed.