我一直在寻找解决方案,但没有找到任何有用的方法。我收到以下错误:
Implicit declaration of function 'sum' is invalid in C99
Implicit declaration of function 'average' is invalid in C99
Conflicting types for 'average'
有谁之前经历过这个吗?我正在尝试在 Xcode 中编译它。
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool
{
int wholeNumbers[5] = {2,3,5,7,9};
int theSum = sum (wholeNumbers, 5);
printf ("The sum is: %i ", theSum);
float fractionalNumbers[3] = {16.9, 7.86, 3.4};
float theAverage = average (fractionalNumbers, 3);
printf ("and the average is: %f \n", theAverage);
}
return 0;
}
int sum (int values[], int count)
{
int i;
int total = 0;
for ( i = 0; i < count; i++ ) {
// add each value in the array to the total.
total = total + values[i];
}
return total;
}
float average (float values[], int count )
{
int i;
float total = 0.0;
for ( i = 0; i < count; i++ ) {
// add each value in the array to the total.
total = total + values[i];
}
// calculate the average.
float average = (total / count);
return average;
}