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.
Please help :(




#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)

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
#include <iostream>
#include <iomanip>
#include <time.h>
#include <vector>

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 << ": " <<All[i].freq << " times"<<endl;
  }
  
  
  //--------------------------------------------------------------------------
  
  
  int highest = All[0].freq;
  int lowest = All[0].freq;
  
  vector<int> highs;
  vector<int> 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;
}


In action: https://repl.it/repls/FatalWhitesmokeMetadata
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:

Again, can run at https://repl.it/repls/FatalWhitesmokeMetadata
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
#include <iostream>
#include <iomanip>
#include <time.h>
#include <vector>

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 << ": " <<All[i].freq << " times"<<endl;
  }
  
  
  //--------------------------------------------------------------------------
  
  
  int highest = All[0].freq;
  int lowest = All[0].freq;
  
  vector<int> highs = {All[0].n};
  vector<int> 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;
}
Thank you Icy1 I finally figured out what occurred by using yours as an example and I am fairly new to vectors the class has not gone through it but I am kind of self taught on the subject. I appreciate the help a lot.
Registered users can post here. Sign in or register to post.