C - 计算随机数组中元素出现次数的函数
C - Function that counts the number of occurrences of an element in a randomized array
我对整体编程有点陌生,目前正在用 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
函数。
很简单
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 );
我对整体编程有点陌生,目前正在用 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
函数。
很简单
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 );