【问题标题】:How do I record the frequency of numbers inputted by a user using functions in C如何记录用户使用 C 中的函数输入数字的频率
【发布时间】:2023-03-14 07:35:01
【问题描述】:

嘿,我目前正在开发一个乐透类型的游戏,我的要求之一是记录用户输入数字的频率,然后在用户希望看到它们时显示它们。该程序也必须是模块化的,因此功能。

我的问题是我似乎无法弄清楚如何跟踪我尝试了很多东西的数字,这是我得到的最接近的数字......

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();
}

无论输入是什么,每次我运行它时的输出

“你选择11的次数是1”

我真的不知道下一步该做什么,因此我们将不胜感激。提前致谢!

编辑:用户可以玩多轮,这就是数字的频率可以加起来的方式。

【问题讨论】:

  • 请澄清:乐透中的数字是否不需要唯一?用户如何多次选择一个号码进行抽奖?
  • 我很抱歉他们可以玩多轮
  • 好的,所以只需定义一个 number_history 数组,每次选择一个数字时,它都会增加每个数字的值(与数组中的索引匹配)。
  • 这不是我对元素数组所做的吗?
  • 对不起,我不太明白

标签: c arrays function loops frequency


【解决方案1】:

我认为您的代码中的主要问题在这里:

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

您实际上检查了用户选择的第 i 个数字是否等于 i。这意味着只有在这种情况下才会进行增量 - 这不是您想要的(如果我没听错的话)。

我认为您应该放弃if 语句,并且假设用户只选择非负数(并且elements 数组在开始时正确归零),请执行以下操作:

elements[picked_nums[i]]++;

也就是说,您增加与所选数字匹配的数组单元格(i 只是您用于迭代 picked_num 数组的索引)。

【讨论】:

    【解决方案2】:

    问题在于如何计算和存储数字:

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

    您的i 正在移动,同时从picked_nums 中选择的元素正在移动。此循环不会正确计数或存储。

    提供的解决方案假定选择的号码存储在numbers 数组中。我假设数字在 1 到 64 范围内。您可以根据需要调整程序。提供的测试:

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

    测试:

    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   
    

    【讨论】:

    • int大小有什么作用?
    • 这是int picked_nums中的元素个数
    • 啊好吧我明白了,非常感谢你的帮助哈哈
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 2017-10-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-28
    相关资源
    最近更新 更多