2

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.

Scott
  • 319
  • 4
  • 13
  • 6
    missing `;` at the end of structure definition and function declaration. – neverhoodboy Mar 04 '14 at 04:40
  • 1
    And another missing semicolon after the function declaration after the structure definition. – Jonathan Leffler Mar 04 '14 at 04:41
  • And yet another missing semicolon in the last statement in `main`. And the initializer of `argArray` has some syntax error. – Yu Hao Mar 04 '14 at 04:42
  • Yes, I realized I had syntax errors elsewhere, but the compiler complained about the function prototype first. I'm still in MATLAB mode with [] defining a matrix instead of {}. – Scott Mar 04 '14 at 04:46
  • guys, the syntax error can be improved after reading compiler error too but the main problem is @Scott is trying to use `stats.sum = stats.sum + argArray[i];` `stats.average = (stats.sum)/5;` or `stats.stdDev = sqrt(varSum/5);`which is unacceptable . See my answer and explanations. – KNU Mar 04 '14 at 05:48

3 Answers3

2

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

KARTHIK BHAT
  • 1,410
  • 13
  • 23
1

you need to terminate the structure prototype method with ;

 struct statStruct statsCalc(int a, int b, int c, int d, int e) ;
Rishi Dwivedi
  • 908
  • 5
  • 19
1

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;
}
KNU
  • 2,560
  • 5
  • 26
  • 39
  • it might be helpful to read this post http://stackoverflow.com/questions/9653072/return-a-struct-from-a-function-in-c – KNU Mar 04 '14 at 05:55