### Frequency of random generated 30 numbers from range of 0 to 9

I have been attempting to finish one of my codes for homework I got the list to work listing o to 9 numbers and displaying their frequency but I could not get the count of frequency of lowest and highest frequency to display multiple instances.
For Example: The number with the lowest frequency of 5 are: 1 5
but all I get is it only display 1 only and not the second instance which is 5.

#include <iostream>
#include <iomanip>
#include <time.h>
using namespace std;
struct NUM
{
int n;
int freq = 0;
};

int main()
{
NUM All[10];
int numbers;
srand(time(0));

cout << "Number" << " " << "Frequency" << endl;
cout << "------------------" << endl;

for (int i = 0; i < 10; i++)
{
All[i].n = i;
}

for (int i = 0; i<30; i++)
{
numbers = rand() % 10;
All[numbers].freq += 1;
}

for (int i = 0; i < 10; i++)
{
cout << " " << All[i].n << " " <<All[i].freq << endl;
}

//--------------------------------------------------------------------------

int high = All[0].freq;
int j = 0;
int i;

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

if (All[i].freq >= high)
{
high = All[i].n;
j = i;
}

}

cout << "Number(s) with largest frequency of " << All[j].freq <<" is /are " << All[j].n;
cout << endl;

int low = All[0].freq;
int t = 0;

for (int i = 0; i < 10; i++)
{
if (All[i].freq <= low)
{
low = All[i].freq;
t = i;
}
}
cout << "Number(s) with lowest frequency of " << All[t].freq << " is /are " << All[t].n;
cout << endl;

system("PAUSE");
return 0;
}
ok, so from a design point of view, you have an array of structs, where each struct has an integer "label" and its frequency. When an integer is a label like that, and especially when it goes from 0 to 9, you can usually just simplify it down to using an array's indices as the "labels".

[ 9, 8, 12, ...] for example, would show that 0 occurred nine times, 1 occurred eight times, 2 occurred twelve times, etc. (I didn't change this part)

As for the logic itself, as you said, it's working pretty well, but you want to add the feature of displaying multiple "winners". Some notes:
0. Use [ code ] ... [ /code ] tags (no spaces)
1. Add print statements to actually show what data was generated, and a few mini labels like colons to show that one thing occurred X times
2. Vector is your friend for easily adding an unknown number of items to an array-like container.
3. You can do all your high/low checking during one pass through the items; just need to check conditions carefully
4. Once you assign highest or lowest index 0 of the array, you can iterate through 1...9 (no need to check index 0)

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990`` ``````#include #include #include #include using namespace std; struct NUM { int n; int freq = 0; }; int main() { NUM All[10]; int numbers; srand(time(0)); cout << "Number" << " " << "Frequency" << endl; cout << "------------------" << endl; for (int i = 0; i < 10; i++) { All[i].n = i; } cout << "Generating numbers:\n"; for (int i = 0; i<30; i++) { numbers = rand() % 10; cout << numbers << " "; All[numbers].freq += 1; } cout << endl; for (int i = 0; i < 10; i++) { cout << " " << All[i].n << ": " < highs; vector lows; for (int i = 1; i < 10; i++) { NUM& num = All[i]; if (num.freq > highest) { highest = num.freq; highs.clear(); highs.push_back(num.n); } else if (num.freq == highest) { highs.push_back(num.n); } if (num.freq < lowest) { lowest = num.freq; lows.clear(); lows.push_back(num.n); } else if (num.freq == lowest) { lows.push_back(num.n); } } cout << "Number(s) with largest frequency of " << highest <<" are "; for (auto& n : highs) cout << n << " "; cout << endl; cout << "Number(s) with lowest frequency of " << lowest << " are "; for (auto& n: lows) cout << n << " "; cout << endl; return 0; }``````

Last edited on
Problem with that is it is still not displaying the multiple "winners" or instances:
out put:

0 : 6 times
1 : 3
2 : 2
3 : 0
4 : 4
5 : 6
6 : 4
7 : 1
8 : 1
9 : 3
Number(s) with largest frequency of 6 are 5
Number(s) with lowest frequency of 0 are 3

Problem is there are more numbers with the frequency of 6 but only the number 5 is displayed what I am trying to ask is how would I get it to display the number 0 as well because that has frequency of 6 as well ??
Sorry, I had a bug because, while I set the initial values of highest and lowest, I didn't actually initialize winners vectors with the 0th frequencies =)

So sometimes it'd work, but sometimes not (if 0 was one of the winners). Fixed:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990`` ``````#include #include #include #include using namespace std; struct NUM { int n; int freq = 0; }; int main() { NUM All[10]; int numbers; srand(time(0)); cout << "Number" << " " << "Frequency" << endl; cout << "------------------" << endl; for (int i = 0; i < 10; i++) { All[i].n = i; } cout << "Generating numbers:\n"; for (int i = 0; i<30; i++) { numbers = rand() % 10; cout << numbers << " "; All[numbers].freq += 1; } cout << endl; for (int i = 0; i < 10; i++) { cout << " " << All[i].n << ": " < highs = {All[0].n}; vector lows = {All[0].n}; for (int i = 1; i < 10; i++) { NUM& num = All[i]; if (num.freq > highest) { highest = num.freq; highs.clear(); highs.push_back(num.n); } else if (num.freq == highest) { highs.push_back(num.n); } if (num.freq < lowest) { lowest = num.freq; lows.clear(); lows.push_back(num.n); } else if (num.freq == lowest) { lows.push_back(num.n); } } cout << "Number(s) with highest frequency of " << highest <<": "; for (auto& n : highs) cout << n << " "; cout << endl; cout << "Number(s) with lowest frequency of " << lowest << ": "; for (auto& n: lows) cout << n << " "; cout << endl; return 0; }``````