15,564,017 members
See more:
My task is to create a
```Creating an average_variance function to return the average of an array of data and also calculate the variance and return it in a pointer. I've created one average_variance function that I must abide by and created a for loop to calculate the average and the variance.

Yet I get an immense amount of errors such as;
<pre>Pointers.c:10:5: error: expected identifier or ‘(’ before ‘return’
10 |     return (sqDiff / numdata);
|     ^~~~~~
Pointers.c:13:2: error: expected identifier or ‘(’ before ‘for’
13 |  for (int i = 0; i<numdata; i++)
|  ^~~
Pointers.c:13:19: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘<’ token
13 |  for (int i = 0; i<numdata; i++)
|                   ^
Pointers.c:13:30: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘++’ token
13 |  for (int i = 0; i<numdata; i++)
|                              ^~
Pointers.c:15:2: error: expected identifier or ‘(’ before ‘}’ token
15 |  }
|  ^
Pointers.c:16:2: error: expected identifier or ‘(’ before ‘return’
16 |  return (sum / numdata);
|  ^~~~~~
Pointers.c:17:1: error: expected identifier or ‘(’ before ‘}’ token
17 | }
| ^
Pointers.c: In function ‘main’:
Pointers.c:24:18: error: too many arguments to function ‘average_variance’
24 |     double avg = average_variance(values, 6, & variance);
|                  ^~~~~~~~~~~~~~~~
Pointers.c:2:8: note: declared here
2 | double average_variance(double data[], int numdata)
|        ^~~~~~~~~~~~~~~~
Pointers.c:25:12: error: stray ‘\342’ in program
25 |     printf("average = % f variance = % f\ n", average, variance);
|            ^
Pointers.c:25:15: error: ‘average’ undeclared (first use in this function)
25 |     printf("average = % f variance = % f\ n", average, variance);
|             ^~~~~~~
Pointers.c:25:15: note: each undeclared identifier is reported only once for each function it appears in
Pointers.c:25:25: error: expected expression before ‘%’ token
25 |     printf("average = % f variance = % f\ n", average, variance);
|                       ^
Pointers.c:25:43: error: stray ‘\’ in program
25 |     printf("average = % f variance = % f\ n", average, variance);
|                                         ^
Pointers.c:25:46: error: stray ‘\342’ in program
25 |     printf("average = % f variance = % f\ n", average, variance);
|                                            ^```

Here's my original code
C
```#include <stdio.h>
double average_variance(double data[], int numdata)
{
int i;
double sqDiff = 0;
double avg = averages(data, numdata);
for (int i = 0; i<numdata; i++)
sqDiff = (data[i] - numdata) * (data[i] - numdata);
}
return (sqDiff / numdata);

double sum = 0;
for (int i = 0; i<numdata; i++)
sum += data[i];
}
return (sum / numdata);
}

int main(int argc, char ** argv)
{
double values[] = {1,2,3,5,6,7};
double variance;
double avg = average_variance(values, 6, & variance);
printf("average = % f variance = % f\ n", average, variance);

}```

What I have tried:

I've tried to indent numerous times as It may be linked to a syntax issue but to no avail.
Posted
Updated 25-Feb-21 21:24pm
v2

Solution 1

C++
```double average_variance(double data[], int numdata)
{
int i;
double sqDiff = 0;
double avg = averages(data, numdata);
for (int i = 0; i<numdata; i++) // probably missing a { here
sqDiff = (data[i] - numdata) * (data[i] - numdata);
} // to match the ine here
return (sqDiff / numdata);

double sum = 0;
for (int i = 0; i<numdata; i++) // probably missing a { here
sum += data[i];
} // to match the ine here
return (sum / numdata);
}```

Another problem, is the you can have only 1 return, bot two.

Advice: Learn to indent properly your code with professional editor, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
C++
```#include <stdio.h>
double average_variance(double data[], int numdata)
{
int i;
double sqDiff = 0;
double avg = averages(data, numdata);
for (int i = 0; i<numdata; i++)
sqDiff = (data[i] - numdata) * (data[i] - numdata);
}
return (sqDiff / numdata); // position of this line indicate a problem with { and }

double sum = 0;
for (int i = 0; i<numdata; i++)
sum += data[i];
}
return (sum / numdata);
}

int main(int argc, char ** argv)
{
double values[] = {1,2,3,5,6,7};
double variance;
double avg = average_variance(values, 6, & variance);
printf("average = % f variance = % f\ n", average, variance);

}```

Indentation style - Wikipedia[^]

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
ultraedit[^]
Enabling Open Innovation & Collaboration | The Eclipse Foundation[^]

v2

Solution 2

Patrice has given you some valid tips. I think you have several mistakes. You are missing the function definition for averages which was being called in average_variance. Also, average_variance needs to add the squares of the difference and the variance needs to be passed as an argument because that's how you are calling the function. Try this:
C++
```double averages( double data[], int numdata )
{
double sum = 0;
for (int i = 0; i<numdata; i++)
{
sum += data[i];
}
return (sum / numdata);
}

double square( double val )
{
return val * val;
}

double average_variance( double data[], int numdata, double & variance )
{
double avg = averages( data, numdata );
double sqDiff = 0;
int i;
for( i = 0; i < numdata; i++ )
{
sqDiff += square( data[i] - numdata );
}
variance = sqDiff / numdata;
return avg;
}

#define ARG_COUNT 6

int main(int argc, char ** argv)
{
double values[ARG_COUNT] = {1,2,3,5,6,7};
double variance = 0;
double avg = average_variance( values, ARG_COUNT, variance );
printf( "average is %.3f and variance is %.3f\n", average, variance );
}```
I almost forgot - the `printf` statement is wrong because the floating point format specifier is `%f`. If you want to display six digits after the decimal point you can use `%.6f` as the format specifier.

v5
Member 15084336 26-Feb-21 7:39am
Thanks, I've made some changes to the print statement but I receive the following errors;
\$ gcc -o Pointers Pointers.c
Pointers.c:9:2: error: expected identifier or ‘(’ before ‘return’
9 | return (sum / numdata);
| ^~~~~~
Pointers.c:10:2: error: expected identifier or ‘(’ before ‘}’ token
10 | }
| ^
Pointers.c:17:61: error: expected ‘;’, ‘,’ or ‘)’ before ‘&’ token
17 | double average_variance( double data[], int numdata, double & variance )
| ^
Pointers.c: In function ‘main’:
Pointers.c:34:18: warning: implicit declaration of function ‘average_variance’ [-Wimplicit-function-declaration]
34 | double avg = average_variance( values, 6, & variance );
| ^~~~~~~~~~~~~~~~
Rick York 26-Feb-21 12:00pm
I can't see your revised code so I can't help you fix the errors. If you are going to post it I recommend that you edit your original question so the code can be displayed correctly. It will not be displayed right in a reply message.
Rick York 26-Feb-21 13:33pm
Does C support references? I can't remember. If not then variance should be passed as a pointer.

Anyway, I noticed a few errors also so the solution has been revised. Plus - I don't like using literal values so a macro definition was added.