When I run it, it prints out but the largest and lowest frequency's don't come out correctly?

#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;
            }
        }
    }
}

I keep getting something like this, how do I fix this?

5 0 1 1 5 1 7 7 6 7 0 8 4 7 0 7 4 6 6 3 6 0 1 8 9 2 2 0 4 7
NUMBER           FREQUENCY
──────────────────────────────
0                  5
1                  4
2                  2
3                  1
4                  3
5                  2
6                  4
7                  6
8                  2
9                  1
Number(s) with the largest frequency of 5 is/are: 5
Number(s) with the lowest frequency of 5 is/are: 5
Number(s) with the lowest frequency of 1 is/are: 1
Press any key to continue . . .

You are going to have to loop through your data and find the min and max frequency. Then you have to loop through your array and print out all of the numbers that have the same max frequency. Then you have to loop through your array and print out all of the numbers that have the same min frequency.

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(ALL[i].freq < min)
        {
            min = ALL[i].freq;
        }
    }
    for (int i = 0; i < 10; i++)
    {
        if (ALL[i].freq == max)
            cout << "Number(s) with the largest frequency of " << max << " is/are: " << x[i] << endl;
    }
    for (int i = 0; i < 10; i++)
    {
        if (ALL[i].freq == min)
            cout << "Number(s) with the lowest frequency of " << min << " is/are: " << x[i] << endl;
    }
}

You need to take the tests for min and max outside of the loop. Also, min and max are the names for common functions (such as min(x,y) and max(x,y)) - some compilers will not like this. Change the names to something like theMin and theMax.

A few other things to think about:

Try to avoid using global variables. On a big project it's easy to lose track of which functions are modifying the variable.

A struct is probably redundant for this, since the index of an array is the number and the value can be the frequency.

Since you're including the c++11 <array> header, I assume that using c++11 arrays are ok. In that case using the assign function eliminates a separate loop for initializing the array.

Storing the numbers of the min and max frequencies in a vector<int> allows you to display only one line each for the min and max numbers.

Try to use meaningful names for your variables. This makes it easier in large projects to decipher what their purposes are.

Using constant variables instead of magic numbers makes future revisions much easier.

time(0) always return the same value making your random data the same each time you run your program. time(NULL) fixes this.

All this makes your code look something like this:

#include <ctime>
#include <iostream>
#include <iomanip>
#include <array>
#include <vector>
using namespace std;
const int DATA_SIZE = 30;
const int FREQS_SIZE = 10;
void DisplayNum(array<int, DATA_SIZE>);
void DisplayData(array<int, FREQS_SIZE>);
void FindMaxMin(array<int, FREQS_SIZE>);

int main()
{
    srand(time(NULL));
    array<int, FREQS_SIZE> freqs;
    array<int, DATA_SIZE> data;
    freqs.assign(0);
    for (int i = 0; i < DATA_SIZE; i++) //assign random numbers 0-9 to n
    {
        data[i] = rand() % FREQS_SIZE;
        freqs[data[i]]++;
    }
    DisplayNum(data); // show the random numbers assigned
    cout << endl;

    DisplayData(freqs); //display the random numbers and their frequency
    FindMaxMin(freqs);
    system("PAUSE");
    return 0;
}
void DisplayNum(array<int, DATA_SIZE> x)
{
    for (int i = 0; i < DATA_SIZE; i++)
    {
        cout << x[i] << " ";
    }
}
void DisplayData(array<int, FREQS_SIZE> freqs)
{
    cout << "NUMBER" << setw(20) << "FREQUENCY" << endl;
    for (int i = 0; i < DATA_SIZE; i++)
    {
        cout << char(196);
    }
    cout << endl;
    cout << setfill(' ');
    cout << fixed << showpoint << setprecision(2);
    for (int i = 0; i < FREQS_SIZE; i++)
    {
        //use magic numbers here as these probably won't change if the size of the freqs array does.
        cout << left << setw(10) << i << right << setw(10) << freqs[i];
        cout << endl;
    }
}
void FindMaxMin(array<int, FREQS_SIZE> x)
{
    int freqMax = 0;
    int freqMin = 9;
    for (int i = 0; i < FREQS_SIZE; i++)
    {
        if (x[i] > freqMax)
        {
            freqMax = x[i];
        }
        if (x[i] < freqMin)
        {
            freqMin = x[i];
        }
    }
    vector<int> maxNums;
    vector<int> minNums;
    for (int i = 0; i < FREQS_SIZE; i++)
    {
        if (x[i] == freqMax)
        {
            maxNums.push_back(i);
        }
        if (x[i] == freqMin)
        {
            minNums.push_back(i);
        }
    }
    cout << "Number(s) with the largest frequency of " << freqMax << " is/are: ";
    for (auto i : maxNums)
    {
        cout << i << " ";
    }
    cout << '\n';
    cout << "Number(s) with the lowest frequency of " << freqMin << " is/are: ";
    for (auto i : minNums)
    {
        cout << i << " ";
    }
    cout << '\n';
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.