Reputation: 319
I'm attempting to write a function that will return the sum, average, and standard deviation of 5 input numbers to be determined when the function is called from main. I elected to implement the multiple-variable return by having the function return a struct containing all of the aforementioned attributes. Here is the code:
#include "math.h"
#include "stdio.h"
struct statStruct
{
double sum;
double average;
double stdDev;
}
struct statStruct statsCalc(int a, int b, int c, int d, int e)
int main(void)
{
struct statStruct stats = statsCalc(3, 6, 9, 6, 6);
printf("Sum of inputs: %f\n Average of inputs: %f\n Standard deviation of inputs: %f\n", stats.sum, stats.average, stats.stdDev)
}
struct statStruct statsCalc(int a, int b, int c, int d, int e)
{
double argArray = [(double)a, (double)b, (double)c, (double)d, (double)e];
double varArray[5];
double varSum = 0;
int i;
struct statStruct stats;
for (i = 0; i < 5; i++)
{
stats.sum = stats.sum + argArray[i];
}
stats.average = (stats.sum)/5;
for (i = 0; i < 5; i++)
{
varArray[i] = pow(argArray[i] - stats.average, 2);
varSum = varSum + varArray[i];
}
stats.stdDev = sqrt(varSum/5);
return stats;
}
When I compile with gcc, I get the following errors:
HW2_2.c:11:1: error: expected ‘;’, identifier or ‘(’ before ‘struct’
struct statStruct statsCalc(int a, int b, int c, int d, int e)
^
HW2_2.c: In function ‘statsCalc’:
HW2_2.c:14:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
HW2_2.c:20:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
HW2_2.c:43:1: error: expected ‘{’ at end of input
}
^
I'm not entirely sure about how to define a function prototype to return a struct in this fashion, since removing the 'statStruct' identifier returns an error about not declaring the return type of the function properly.
Upvotes: 2
Views: 698
Reputation: 2515
The compilation error in your programs is due to following errors:
printf("Sum of inputs: %f\n Average of inputs: %f\n Standard deviation of inputs: %f\n", stats.sum, stats.average, stats.stdDev);//missed a semi-colon here (most common error)
double argArray[5]={(double)a, (double)b,(double)c,(double)d,(double)e}; //used [] bracket instead of {} and improper array declaration
Here's a running code with improvements shown in comment:
#include "math.h"
#include "stdio.h"
struct statStruct
{
double sum;
double average;
double stdDev;
}; //semi-colon missing
struct statStruct statsCalc(int a, int b, int c, int d, int e);//semi-colon missing
int main(void)
{
struct statStruct stats = statsCalc(3, 6, 9, 6, 6);
printf("Sum of inputs: %f\n Average of inputs: %f\n Standard deviation of inputs: %f\n", stats.sum, stats.average, stats.stdDev);//semi-colon missing
}
struct statStruct statsCalc(int a, int b, int c, int d, int e)
{
double argArray[5] = {(double)a, (double)b, (double)c, (double)d, (double)e};//error-here
double varArray[5];
double varSum = 0;
//these variables will help construct a struct
double sum=0;
double average=0;
double stdDev=0;
int i;
//calculating sum
for ( i = 0; i < 5; i++)
{
sum = sum + argArray[i];
}
//calculating average
average = sum/5;
//calculating std. deviation
for ( i = 0; i < 5; i++)
{
varArray[i] = pow(argArray[i] - average, 2);
varSum = varSum + varArray[i];
}
stdDev = sqrt(varSum/5);
//initializing the struct object to be returned
struct statStruct stats = {sum,average,stdDev}; //now construct the struct statStruct instance to be returned
return stats;
}
Upvotes: 1
Reputation: 1420
prototype has to be terminated with ;
change this line
struct statStruct statsCalc(int a, int b, int c, int d, int e);
and also
printf("Sum of inputs: %f\n Average of inputs: %f\n Standard deviation of inputs: %f\n", stats.sum, stats.average, stats.stdDev);
also in the called function initialize stats.sum
to 0
before starting to calculate sum as it will contain garbage value and you will get wrong answers
Note : there are few errors you cant initialize the array the way you have done change it to
double argArray[] = {(double)a, (double)b, (double)c, (double)d, (double)e};
Even structure has to be terminated with ;
have corrected few syntax's in the link check here
Upvotes: 2
Reputation: 928
you need to terminate the structure prototype method with ;
struct statStruct statsCalc(int a, int b, int c, int d, int e) ;
Upvotes: 1