【问题标题】:C - Function that counts the number of occurrences of an element in a randomized arrayC - 计算随机数组中元素出现次数的函数
【发布时间】:2019-12-04 14:41:47
【问题描述】:

我对整体编程有点陌生,目前正在用 C 编程,我目前正在开发一个程序,该程序首先将 10 个数字随机化,放入一个数组中并将它们打印在屏幕上,然后让用户输入一个整数,然后程序应该检查数组并打印出用户输入的整数在数组中出现了多少次,这就是我遇到问题的地方。

#include <stdio.h>
#include <stdlib.h> 
#include <time.h>

//  Function to initialize random numbers.
int Random()                                                            
{
    srand(time(NULL));                                                  //  To initialize the random number generator.
    return 0;                                                           //  Really only the important part from the function, that it returns something.
}

// Generates randome values for the array.
void setRandomNumber(int inputArray[], int arraySize)                   
{
    int i;
    Random();                                                           //  Calls the "Random" function. 
    for(i = 0; i < arraySize; i++)                                      //  Conditions for when/how many times to run the loop.
        inputArray[i] = (rand() % 10) + 1;                              //  What values the array will get, random numbers between 1 and 10.
}

// Function to count the occurrances of an element.
int countElement(int inputArray[], int arraySize, int elementCount)
{

}

int main(void)
{
    int numbers[10];                                                    
    int loop;
    int run = 1;
    int elementCount = 1;
    setRandomNumber(numbers, 10);                                       //  Calls the "setRandomNumber" fucntion to set random values to the floats in the array. 
    countElement(numbers, 10, elementCount);

    for (loop = 0; loop < 10; loop++)                                   //  Prints out the already randomized values of the array "numbers"
        printf("Number: %d\n", numbers[loop]);

    printf("\nWhat to search for: ");
    scanf_s("%d", &elementCount);                                       //  Takes user input on what number to check.
    printf("The number %d occurs %d times.\n", elementCount, countElement);

    return 0;


}

我们需要使用函数,并且函数头必须看起来像 int countElement(int inputArray[], int arraySize, int elementCount),在这种情况下,我遇到问题的是 countElement 函数。

【问题讨论】:

  • 有什么问题?您是否尝试过编写countElement 函数?
  • 您遇到的一个主要问题是在printf("The number %d occurs %d times.\n", elementCount, countElement); 行中 - 您所拥有的最后一个参数将是countElement 函数的地址。要获取函数的返回值,您需要使用参数指定函数,如下所示:countElement(numbers, 10, elementCount).

标签: c arrays function element find-occurrences


【解决方案1】:

很简单

int countElement(int inputArray[], int arraySize, int elementCount)
{
    int count = 0l

    for ( int i = 0; i < arraySize; i++ )
    {
        if ( inputArray[i] == elementCount ) ++count; 
    }

    return count;
}

函数可以这样调用

printf("The number %d occurs %d times.\n", elementCount, 
                                           countElement( numbers, 10, elementCount ) );

虽然这样声明函数会更正确

 size_t countElement( const int inputArray[], size_t arraySize, int elementCount );

【讨论】:

  • 当我将您所说的内容添加到“countElement”函数时,代码运行,并且我更改了“printf”。但是现在每次我输入一个值时,它只会返回该数字出现 10 次,即使它没有出现。有什么建议么?顺便说一句,感谢第一部分。
  • @ConfusedChicken4 检查是否在这个语句中 if ( inputArray[i] == elementCount ) ++count;您正在使用赋值运算符而不是原始语句中的比较运算符。
  • 你是什么意思? (3 周前开始编程,今天学习了函数)这是目前在我的代码“if (inputArray[i] == elementCount)”中的样子
  • Nvm 发现了问题。我没有“退货计数”;在函数的最后。感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 2022-11-30
  • 2021-03-22
  • 1970-01-01
  • 2021-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多