Search code examples
carraysfunctionloopsfrequency

How do I record the frequency of numbers inputted by a user using functions in C


Hey there i'm currently developing a lotto type game and one of my requirements is to record the frequency of the numbers inputted by the user and then display them if the users wishes to see them. The program also must be modular hence the functions.

My problem is that i can't seem to figure out how to keep track of the numbers I tried numerous things and this is the closest I've gotten...

void num_free(int *picked_nums)
{
    static int elements[MAX] = { 0 };
    int i;

    for (i = 0; i < MAX; i++)
        if (*(picked_nums + i) == i)
        {
            elements[i]++;
        }

    for (i = 0; i < MAX; i++)
    {
        if (elements[i] != 0)
        {
            printf("\nThe amount of times you chose %d is %d", i, elements[i]);
        }
    }

    printf("\nEnter any key to return to main menu");
    getchar();
}

The output of this every time i run it no matter the input is

"The amount of times you chose 11 is 1"

I'm really clueless as to what to do next so any and all help would be appreciated. Thanks in advance!

EDIT: The user can play multiple rounds and thats how the frequency of the numbers can add up.


Solution

  • The problem is how you count and store the numbers:

    if (*(picked_nums + i) == i)
    {
        elements[i]++;
    }
    

    Your i is moving and at the same time the element chosen from picked_nums is moving. This loop will not count or store properly.

    The provided solution assumes that picked numbers are stored in the numbers array. I assumed that numbers are in 1 to 64 range. You can adjust program to your needs. Test provided:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void num_free(int picked_nums[], int size )
    {
        static int elements[65] = { 0 }; // numbers can be from 1 to 64 range
        int i;
    
        for (int j = 0; j < size; j++)
        {
            int n = picked_nums[j];
    
            for (i = 1; i < 65; i++)   // numbers can be from 1 to 64 range
            {  
                if ( n == i)
                {
                    elements[i] = elements[i]+1;
                }
            }
        }
    
        for (i = 0; i < 65; i++)
        {
            if (elements[i] != 0)
            {
                printf("\nThe amount of times you chose %d is %d", i, elements[i]);
            }
        }
    
       // printf("\nEnter any key to return to main menu");
       // getchar();
    }
    
    // array of entered numbers:
    int numbers[] = { 2, 2, 2, 40, 7, 7, 8, 9, 40 };
    
    int main(void) {
    
        num_free(numbers, 9); // call with sizeof numbers
    
        return 0;
    }
    

    Test:

    The amount of times you chose 2 is 3                                                                                                          
    The amount of times you chose 7 is 2                                                                                                          
    The amount of times you chose 8 is 1                                                                                                          
    The amount of times you chose 9 is 1                                                                                                          
    The amount of times you chose 40 is 2