我真的很困惑如何调用该函数。更新代码以响应一个或多个答案。下面列出了当前代码和构建错误。
#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>构建失败。