-5
#include <stdio.h>
#include <math.h>

float math(int, int, int, int, int, float, float, float);

main() {
  int a, b, c, d, e;
  float sum, avg, sd;
  printf("Enter Five Integers->");
  scanf("%d%d%d%d%d", &a, &b, &c, &d, &e);
  math(a, b, c, d, e, sum, avg, sd);
  printf("Sum=%.2f\nAverage=%.2f\nStandard Deviation=%.2f", sum, avg, sd);
}

float math(int a, int b, int c, int d, int e, float sum, float avg, float sd) {
  sum = a + b + c + d + e;
  avg = (sum) / 5;
  sd = pow(
      ((pow(a - avg, 2) + pow(b - avg, 2) + pow(c - avg, 2) + pow(d - avg, 2),
        pow(e - avg, 2)) /
       5),
      0.5);
  return sum, avg, sd;
}

My program always returns the answer 0.00. Can anyone explain what is the problem with my code?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261

2 Answers2

1

TL;DR Because, your code invokes undefined behavior and no justification can be made to reasonify the output.

To elaborate, first thing, the statement

 return sum, avg, sd;

does not do what you think it's doing. It does not return three values together, rather due to the usage of comma operator, it returns only sd.

That said, you did not collect the return value of the function call anywhere in your code, so there's no way you are going to get any valid output returned from the function call in the caller, there.

After that, you end up using

 printf("Sum=%.2f\nAverage=%.2f\nStandard Deviation=%.2f", sum, avg, sd);

where, the supplied variables (automatic, local variables) remains uninitialized, essentially trying to make use of indeterminate values which invokes undefined behavior.

Finally, for a hosted environment, the conforming signature for main() should be int main(void), at least.

Solution: You need to either

  • Pass pointers to those variable in which you want to store the results from the called function and then, in the caller you can use them to retrieve the updated value.

  • Form a structure containing all the variables for which you want the computed value to be returned, populate and return the structure. Then, in the caller, collect the returned value in another structure type variable and then, use individual member elements to print the value.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

There are few taking points in your code.

  1. In C you can't return more than 1 value from a function. You are trying to return 3 vales, as these are separated using "comma(,)", only last value is returned.
  2. There is a difference between local variables of two functions. You can't assign them in 1 function and use it in other without proper reference.
  3. You made function as "call by value" (read more about it in link). As you desire changed values, you should use "call by refernce", which uses pointers.

So the easiest fix for you will be using global variables.

void math(int, int, int, int, int);     //Changed function declareation
 double sum,avg,sd; //New global variables

 int main()
 {
    int a, b, c, d, e;
    printf("Enter Five Integers->");
    scanf("%d%d%d%d%d", &a, &b, &c, &d, &e);
    math(a, b, c, d, e);        //Changed function calling
    printf("Sum=%.2f\nAverage=%.2f\nStandard Deviation=%.2f\n", sum, avg, sd);
    return 0;
}

 void math(int a, int b, int c, int d, int e)
 {
    sum = a + b + c + d + e;
    avg = (sum) / 5;
    double result = (pow(a - avg, 2) + pow(b - avg, 2) + pow(c - avg, 2) +  pow(d - avg, 2) + pow(e - avg, 2) ) / 5;
    sd = pow(result , 0.5);
 }
svtag
  • 184
  • 4
  • 12
  • Using global variables is not a good way of solving the problem. It works; it may even be fairly easy; it is not good practice, though. There are other problems with the scenario too (not of your making — of the original scenario). In particular, passing a fixed number of value is odd. A better solution would read a semi-arbitrary number of values into an array, and the code in the function would process the array. – Jonathan Leffler Feb 26 '17 at 17:41