[英]Finding frequency of an integer in an array and calculating x to the nth power
I am trying to solve two different C problems and would like some help and advice in order to better understand how C works and if I'm on the right track with these. 我正在尝试解决两个不同的C问题,并希望得到一些帮助和建议,以便更好地理解C的工作原理以及我是否正确地使用这些问题。
First problem is: To write a function that counts the number of times the value (x) appears among the first (n) elements of an array and returns that count as the frequency of x in theArray. 第一个问题是:编写一个函数,该函数计算值(x)在数组的第一个(n)元素中出现的次数,并将该计数作为theArray中x的频率返回。 So, an example would be if the array being passed contained the values {5, 7, 23, 8, 23, 67, 23}. 因此,一个例子是,如果传递的数组包含值{5,7,23,8,23,67,23}。 And n was 7 and x was 23, then it would return a value of 3 since 23 occurs 3 times within the first 7 elements of the array. 并且n是7而x是23,那么它将返回值3,因为23在阵列的前7个元素中发生3次。
Here is what I have so far: 这是我到目前为止:
#include <stdio.h>
#define SIZE 20 /* just for example - function should work with array of any size */
int frequency (int theArray[], int n, int x)
{
int i;
int count = 0;
for (i = 0; i < n; i++)
{
if (theArray[i] == x)
{
count = count++;
}
}
return (count);
}
int main(void)
{
/* hard code n and x just as examples */
int n = 12; /* look through first 12 items of array */
int x = 5; /* value to find */
int numberFrequency;
long int theArray[SIZE] = {5,2,3,4,5,6,1,2,10,5,10,12,6,8,7};
numberFrequency = frequency (theArray[SIZE], n, x);
printf ("%i", numberFrequency);
return 0;
}
Currently I'm getting a run time error message and believe it has something to do with the for loop function. 目前我收到运行时错误消息,并认为它与for循环功能有关。
Second problem is: Write a function that raises an integer to a positive integer power. 第二个问题是:编写一个将整数提升为正整数幂的函数。 Have the function return a long int, which represents the results of calculating x to the nth power. 让函数返回一个long int,它表示计算x到n次幂的结果。 Do not use the C pow library function and do not use recursion! 不要使用C pow库函数,不要使用递归!
My code so far: 我的代码到目前为止:
#include <stdio.h>
int x_to_the_n (int x, int n)
{
int i;
long int result = 1;
if (n == 0)
{
return(result);
}
else
{
for (i = 0; i < n ; ++i)
{
/* equation here - How can I make (x*x*x*x*x*x,etc...? */
result = x*(n*x);
}
}
return (result);
}
int main(void)
{
int x =4;
int n =5;
long int result;
result = x_to_the_n (x, n);
printf ("%i", result);
return 0;
}
I can't use recursion so that is out of the question. 我不能使用递归,所以这是不可能的。 So, I thought the next best thing would be a for loop. 所以,我认为下一个最好的东西是for循环。 But I'm a little stuck in how I would go about making a for loop do (x x x*x....) based on value of (n). 但我有点陷入如何根据(n)的值制作for循环do(x x x * x ....)。 Any help and advice would be appreciated! 任何帮助和建议将不胜感激!
In the first problem you give an element after the array as a parameter to your function. 在第一个问题中,您将数组后面的元素作为函数的参数。
You define a long int array, and pass it into a function expecting an int array. 您定义一个long int数组,并将其传递给期望int数组的函数。
long int theArray[SIZE] = {5,2,3,4,5,6,1,2,10,5,10,12,6,8,7};
should be 应该
int theArray[SIZE] = {5,2,3,4,5,6,1,2,10,5,10,12,6,8,7};
Instead of this: 而不是这个:
numberFrequency = frequency (theArray[SIZE], n, x);
try this: 尝试这个:
numberFrequency = frequency (theArray, n, x);
And replace: 并替换:
count = count++;
with: 有:
count++;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.