#include <ctime>
#include <iostream>
#include <iomanip>
#include <array>
usingnamespace 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;
}
}
}
}
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];
...