Thread: Having trouble finding Mode in a randomly-generated, 100-element array

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    2

    Having trouble finding Mode in a randomly-generated, 100-element array

    Hello, I am in a mid-level C++ course, but I'm pretty rusty with my basics. This course has asked that I create three void functions to:
    a) initialize an array of 100 randomly-generated #s (range 1-30);
    b) print that array;
    c) find the mode(s) and display.

    I managed to complete a) and b) with ease, but the crafting of c) has left me completely confused. A friend was helping, but it's been a while since he used C++. I'm supposed to use a second array to count the occurrences of the numbers. Here is the code I have so far:

    insert
    Code:
    // This program generates a 100 element array of random numbers, displays that
    // array, then finds the mode(or modes if more than one) and displays it.
    #include <iostream>
    #include <cstdlib>  // For rand and srand.
    #include <ctime>    // For the time function.
    #include <iomanip>
    using namespace std;
    
    
    void initialize(int[], int);
    void print_array(int[], int);
    void Mode(int[], const int, int[], const int);
    
    
    const int ARRAY_SIZE = 100;
    const int MAX_RANGE = 30;
    
    
    int main()
    {
        int Array[ARRAY_SIZE];  //Create an array with 100 elements.
        int second[MAX_RANGE] = {0};  //Create a secondary array for counting.
        initialize(Array, ARRAY_SIZE);
        print_array(Array, ARRAY_SIZE);
        Mode(Array, ARRAY_SIZE, second, MAX_RANGE);
        return 0;
    }
    
    
    void initialize(int nums[], int size)
    {
        // Get the system time.
        unsigned seed = time(0);
    
    
        // Seed the random number generator.
        srand(seed);
    
    
        // Loop to fill the array with random numbers from 1 to 30.
        for (int index = 0; index < size; index++)
        {
            //Generate random numbers from 1 to 30.
            nums[index] = 1 + rand() % MAX_RANGE;
        }
    }
    
    
    void print_array(int nums[], int size)
    {
        for (int x = 0; x < size; x++)
        {
            if (x % 10 == 0)    // At the end of ten elements, begin a new line.
            cout << endl;
            cout << setw(2) << nums[x] << "  ";
        }
        cout << endl << endl;
    }
    
    
    void Mode(int nums[], int size, int secondary[], int size2)
    {
        //Create a counter array to store occurrences of items in first array.
        int max = 0;
    
    
        for(int i = 0; i < size; i++)
        {
            secondary[nums[i]]++;
    
    
            if(secondary[nums[i]] > max)
                max = secondary[nums[i]];
        }
        for(int i = 0; i <= size2; i++)
        {
            if(secondary[nums[i]] == max)
                cout << "The mode is: " << secondary[i] << endl;
        }
    }

  2. #2
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Code:
    for(int i = 0; i <= size2; i++)
        {
            if(secondary[nums[i]] == max)
                cout << "The mode is: " << secondary[i] << endl;
        }
    Keep in mind that the actual value of secondary[I] is just a count of how many times the number has appeared. The actual mode can be representable by the position of 'max' in the array, like this:

    Code:
    for(int i = 0; i < ARRAY_SIZE; i++)
        {
            secondary[nums[i]]++;
        }
    
        for(int i = 0; i < MAX_RANGE; i++)
        {
            if( secondary[i] > max )
                max = i;
        }
    
        std::cout << "Mode : " << max << "\n";
    Edit: Sorry about the bad formatting, I can't seem to fix it :/ .
    Last edited by Alpo; 09-25-2014 at 10:13 PM.
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    2
    No, you're fine. Thank you very much! That really helped!

  4. #4
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by Sup3rsag3 View Post
    No, you're fine. Thank you very much! That really helped!
    I had to go back and edit, the first loop should have been ranged : I < ARRAY_SIZE (else it will only take the mode of the first 30 lol). Glad to be a help.
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing randomly generated numbers?
    By arcadedragon in forum C Programming
    Replies: 7
    Last Post: 10-13-2012, 12:42 AM
  2. vBulletin vandals and the wisdom of randomly generated passwords
    By abachler in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 12-08-2008, 05:27 PM
  3. sorting a randomly generated dynamic array
    By Led Zeppelin in forum C Programming
    Replies: 4
    Last Post: 05-03-2002, 09:10 AM
  4. unique randomly generated numbers
    By grobe59 in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2001, 08:26 PM
  5. arranging randomly generated numbers in ascending order
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-05-2001, 07:14 PM

Tags for this Thread