0

我真的很困惑如何调用该函数。更新代码以响应一个或多个答案。下面列出了当前代码和构建错误。

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

//function prototypes
void signs(int x[], int size, int *negPtr, int *zeroPtr, int *posPtr);
double average(int x[], int size, int *greaterPtr);

int main()
{
//variable declarations
int zeroCounter, posCounter, negCounter, greaterThanAveCounter;
double ave;
int i;
int arr[63];
int size = 63;
int x[63], int size, int* negPtr, int* zeroPtr, int* posPtr, int* greaterPtr;

//seeding the random number generator function with the time
srand(time(NULL));

//filling the array with random numbers from the interval [-15, 40]
for(i=0; i<63; i++)
{
    arr[i] = rand()%(40-(-15)+1)+(-15);
}

//printing the array to the screen 9 elements per line
for(i=0; i<63; i++)
{
    if(i%9==0)
    {
        printf("\n");
    }
    printf("%5d",arr[i]);
}

//call function signs
signs (x, size, negPtr, zeroPtr, posPtr);

printf("\n\nThe number of elements that are negative is: %d\n",negCounter);
printf("The number of elements that are equal to zero is: %d\n",zeroCounter);
printf("The number of elements that are positive is: %d\n",posCounter);


//call function average
average(x, size, greaterPtr);

printf("The average of all the elements is: %.2f\n",ave);
printf("The number of elements that are greater than the average is: %d\n",greaterThanAveCounter);

return 0;
}



/***************************************************************************
signs:
This function finds the number of elements that are negative, positive and
equal to zero in an integer array. The number of elements that are negative,
positive and equal to zero are returned by 3 pointers that the function
receives.

Inputs
1. The integer array
2. The size of the aray
3. An integer pointer to the negative counter
4. An integer pointer to the zero counter
5. An integer pointer to the positive counter
***************************************************************************/
void signs(int x[], int size, int *negPtr, int *zeroPtr, int *posPtr)
{
int i;
for (i=0; i<size; i++)
{
    if(x[i]<0)
    {
        (*negPtr)++;
    }
    else if (x[i]==0)
    {
        (*zeroPtr)++;
    }
    else
    {
        (*posPtr)++;
    }
}
}



/***************************************************************************
average:
This function finds the average and the number of elements that are greater
than the average in an integer array. The function returns the value of the
average and returns the number of elements that are greater than the average
using a pointer that the function should receive.

Inputs
1. The integer array
2. The size of the aray
3. An integer pointer to the greater than average counter
***************************************************************************/
double average(int x[], int size, int *greaterPtr)
{
int i;
double ave;
int sum=0;

for (i=0; i<size; i++)
{
    sum+=x[i];
}

ave = (double)sum/size;

*greaterPtr = 0;
for (i=0; i<size; i++)
{
    if(x[i]>ave)
    {
        (*greaterPtr)++;
    }
}
return ave;
}

新错误

  • (32): 错误 C2059: 语法错误: 'type'
  • (35): 警告 C4244: 'function' : 从 'time_t' 转换为 'unsigned int',可能丢失数据
  • (54): 错误 C2065: 'negPtr' : 未声明的标识符

  • (54): 警告 C4047: 'function' : 'int *' 在
    间接级别上与 'int'不同

  • (54): 警告 C4024: 'signs' : 形参和实参 3 的不同类型
  • (54): 错误 C2065: 'zeroPtr' : 未声明的标识符
  • (54): 警告 C4047: 'function' : 'int *' 在间接级别上与 'int' 不同

  • (54): 警告 C4024: 'signs' : 形式参数和实际参数 4 的不同类型

  • (54): 错误 C2065: 'posPtr' : 未声明的标识符
  • (54): 警告 C4047: 'function' : 'int *' 在间接级别上与 'int' 不同

  • (54): 警告 C4024: 'signs' : 形式参数和实际参数 5 的不同类型

  • (62): 错误 C2065: 'greaterPtr' : 未声明的标识符

  • (62): 警告 C4047: 'function' : 'int *' 在
    间接级别上与 'int'不同

  • (62): 警告 C4024: 'average' : 形参和实参 3 的不同类型
  • 1> 1>构建失败。
4

3 回答 3

2
double average(int x[], int size, int *greaterPtr);

is how you declare a function. To call it, you need something like:

int a[10], b, *c;             // should also set these to sensible values.
double d = average (a, b, c);

In terms of your newly added error messages, I'll help out with a few:

int x[63], int size, int* negPtr, int* zeroPtr, int* posPtr, int* greaterPtr;

This is not how you define multiple variables, it should be:

int x[63], size, *negPtr, *zeroPtr, *posPtr, *greaterPtr;

That's probably the single cause of most of your current problems (I haven't confirmed this, it's more from a quick analysis).

于 2012-12-11T04:26:34.483 回答
1

signs (x, size, negPtr, zeroPtr, posPtr); is the correct way to call the function. The reason you get undefined errors is because you never declare those variables in the main function, so you need something like

int x[];
int size;
int* negPtr;

etc.

于 2012-12-11T04:28:37.497 回答
0
  • (32): 错误 C2059: 语法错误: 'type'

您声明变量错误。此行应为:

int x[63], size, *negPtr, *zeroPtr, *posPtr, *greaterPtr;

但是,我建议将一个变量声明保留在一行中,因为这样可以避免此类错误并使代码更具可读性。

  • (35): 警告 C4244: 'function' : 从 'time_t' 转换为 'unsigned int', 可能丢失数据

time()返回 atime_tsrand()接受一个unsigned int. 您需要显式地转换返回值time()来告诉编译器您期望这种情况发生:

srand((unsigned int)time(NULL));

其余的警告似乎是第一个错误的连锁反应。

于 2012-12-11T11:43:48.287 回答