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