trying to do exercise from chapter 13 of a book on programming with objective-c
1. write a function that calculates the average of an array of 10 floating-point values and returns the result.
i have the function to get the average i think?
i tried to put 10 elements into an array, and then tried to print the average at the end of the program. here is the function for my program.
here are the error messages i am getting when i try to run my test program which i called exc1chap13.m
exc1chap13.m:5: error: syntax error before } token
exc1chap13.m: In function average:
exc1chap13.m:19: error: nested functions are disabled, use -fnested-functions to re-enable
exc1chap13.m:23: error: syntax error at end of input
and here is my test program
error messages
exc1chap13.a.m: In function main:
exc1chap13.a.m:7: error: syntax error before { token
exc1chap13.a.m:10: error: conflicting types for average
exc1chap13.a.m:7: error: previous definition of average was here
exc1chap13.a.m:12: error: syntax error before for
test program:
1. write a function that calculates the average of an array of 10 floating-point values and returns the result.
i have the function to get the average i think?
i tried to put 10 elements into an array, and then tried to print the average at the end of the program. here is the function for my program.
Code:
float average (float data[])
{
int i;
float sum, average;
for (i = 0; i < 10; ++i)
sum += data[i];
average = sum / 10;
return average;
}
exc1chap13.m:5: error: syntax error before } token
exc1chap13.m: In function average:
exc1chap13.m:19: error: nested functions are disabled, use -fnested-functions to re-enable
exc1chap13.m:23: error: syntax error at end of input
and here is my test program
Code:
#import <stdio.h>
float data [10] = { 10, 5, -3, 17, 82, 9, 0, 0, 8, -7 };
}
float average (float data[])
{
int i;
float sum, average;
for (i = 0; i < 10; ++i)
sum += data[i];
average = sum / 10;
return average;
int main (int argc, char *argv[])
{
printf ("the average of the array is %i\n", average);
}
error messages
exc1chap13.a.m: In function main:
exc1chap13.a.m:7: error: syntax error before { token
exc1chap13.a.m:10: error: conflicting types for average
exc1chap13.a.m:7: error: previous definition of average was here
exc1chap13.a.m:12: error: syntax error before for
test program:
Code:
#import <stdio.h>
int main (int argc, char *argv[])
float average (float data[])
{
int x[10] = {3, 7, -9, 3, 6, -1, 7, 9, 1, -5);
int i;
float sum, average;
for (i = 0; i < 10; ++i)
sum += data[i];
average = sum / 10;
printf ("the average of the array is %i\n", average);
return average;
}