-2

When you enter three numbers from the keyboard, the average of them is calculated and displayed, and the entered value itself and the result of subtracting the average value from the input value are displayed (+ -signed, up to three decimal places). Program to do. specification:

  1. Input from the keyboard and display the result are performed on the main side. The value entered is a floating point number.
  2. The average value is calculated in the function ave3 (). This function takes three data as arguments from the main side, calculates the average of them, and returns the result as a return value.
  3. The operation of subtracting the average value from the input data is performed in the function sub3 (). This function receives information about the three data from the main side and the average value calculated earlier as arguments, and subtracts the average from each of the three data. This function has no return value, and the main side outputs the three values changed in the function as they are.

source code

#include <stdio.h>

double ave3 (double, double, double);
double subave3 (double *, double *, double *);

int main ()
{
   double a, b, c, ave;

   printf ("Please enter three values: \ n");
   scanf ("% lf% lf% lf", & a, & b, & c);

   ave = ave3 (& a, & b, & c); / * Function call * /

   printf ("Average:% .3f \ n", ave);
   printf ("Original data:% + .3f% + .3f% + .3f \ n", a, b, c);

   subave3 (double * x, double * y, double * z);

   printf ("Data after average deduction:% + .3f% + .3f% + .3f \ n", a, b, c);

   return 0;
}

/ * A function that calculates the average. It also subtracts the average value from the data. * /
double subave3 (double x, double y, double z)
{
   x-= ave; / * Subtract the average from each data * /
   y-= ave;
   z-= ave;

   a = x;
   b = y;
   c = z;

   return a, b, c;
}

double ave3 (double x, double y, double z) {
   ave = (* x + * y + * z) / 3.0;
   return ave;
}

Error

prog01.c: In function ‘main’:
prog01.c: 13: 14: error: incompatible type for argument 1 of ‘ave3’
   13 | ave = ave3 (& a, & b, & c); / * Function call * /
      | ^ ~
      | |
      | double *
prog01.c: 3:13: note: expected ‘double’ but argument is of type ‘double *’
    3 | double ave3 (double, double, double);
      | ^ ~~~~~
prog01.c: 13: 18: error: incompatible type for argument 2 of ‘ave3’
   13 | ave = ave3 (& a, & b, & c); / * Function call * /
      | ^ ~
      | |
      | double *
prog01.c: 3:21: note: expected ‘double’ but argument is of type ‘double *’
    3 | double ave3 (double, double, double);
      | ^ ~~~~~
prog01.c: 13: 22: error: incompatible type for argument 3 of ‘ave3’
   13 | ave = ave3 (& a, & b, & c); / * Function call * /
      | ^ ~
      | |
      | double *
prog01.c: 3:29: note: expected ‘double’ but argument is of type ‘double *’
    3 | double ave3 (double, double, double);
      | ^ ~~~~~
prog01.c: 18:11: error: expected expression before ‘double’
   18 | subave3 (double * x, double * y, double * z);
      | ^ ~~~~~
prog01.c: 18: 3: error: too few arguments to function ‘subave3’
   18 | subave3 (double * x, double * y, double * z);
      | ^ ~~~~~~
prog01.c: 4: 8: note: declared here
    4 | double subave3 (double *, double *, double *);
      | ^ ~~~~~~
prog01.c: At top level:
prog01.c: 26: 8: error: conflicting types for ‘subave3’
   26 | double subave3 (double x, double y, double z)
      | ^ ~~~~~~
prog01.c: 4: 8: note: previous declaration of ‘subave3’ was here
    4 | double subave3 (double *, double *, double *);
      | ^ ~~~~~~
prog01.c: In function ‘subave3’:
prog01.c: 28: 8: error: ‘ave’ undeclared (first use in this function); did you mean ‘ave3’?
   28 | x-= ave; / * Subtract the average from each data * /
      | ^ ~~
      | ave3
prog01.c: 28: 8: note: each undeclared identifier is reported only once for each function it appears in
prog01.c: 32: 3: error: ‘a’ undeclared (first use in this function)
   32 | a = x;
      | ^
prog01.c: 33: 3: error: ‘b’ undeclared (first use in this function)
   33 | b = y;
      | ^
prog01.c: 34: 3: error: ‘c’ undeclared (first use in this function)
   34 | c = z;
      | ^
prog01.c: In function ‘ave3’:
prog01.c: 40: 3: error: ‘ave’ undeclared (first use in this function); did you mean ‘ave3’?
   40 | ave = (* x * y * z) / 3.0;
          | ^ ~~
      | ave3
prog01.c: 40:10: error: invalid type argument of unary ‘*’ (have ‘double’)
   40 | ave = (* x * y * z) / 3.0;
                                 | ^ ~
prog01.c: 40: 15: error: invalid type argument of unary ‘*’ (have ‘double’)
   40 | ave = (* x * y * z) / 3.0;
                                 | ^ ~
prog01.c: 40: 20: error: invalid type argument of unary ‘*’ (have ‘double’)
   40 | ave = (* x * y * z) / 3.0;
                                        | ^ ~
MonSorcy
  • 3
  • 2
  • 3
    Please find a [good reference book for C](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list), understand the variable scope, function, return values from function, pointers. Code is full of simple errors which you can fix by your own. – TruthSeeker Oct 21 '20 at 10:03
  • find the solution [here](https://onlinegdb.com/Sk_o5t6PP). – TruthSeeker Oct 21 '20 at 10:18
  • 2
    There are quite a lot of different errors here that you should focus on one at a time. I'm voting to close this for needing focus. – klutt Oct 21 '20 at 10:20

2 Answers2

1

The function ave expects three double arguments, such as a, b, or c. But by taking their addresses, the following is passing double * arguments:

ave = ave3 (& a, & b, & c);

Just change it to:

ave = ave3 (a, b, c);

Look at the arguments one by one, and compare what you're passing to the type that the function expects, and make sure they're compatible.

Also, the following won't work:

subave3 (double * x, double * y, double * z);

This is a function call, not a declaration. Change it to something like:

subave3 (a, b, c);

This probably isn't what you want, but it will compile.

In subave3, it's attempting to reference a, b, and c, even though they are not declared in that function. If you want to return values for those, then you need to pass their addresses and store the values through pointers, since you can't return more than one value in a return statement.

In ave3, the following won't work since a, b, and c aren't pointers:

ave = (* x + * y + * z) / 3.0;

Also, ave isn't declared. Change it to:

double ave = (x + y + z) / 3.0;

There may be additional problems, and in any case some rethinking is probably required. Just remember that applying & to a variable of type t gives a pointer of type t *, and applying a * to an expression of type t * gives a value of type t.

Tom Karzes
  • 22,815
  • 2
  • 22
  • 41
-1
double ave3 (double* x, double* y, double* z) {
   double ave = (* x + * y + * z) / 3.0;
   return ave;
}

This will solve your problem.

double subave3 (double x, double y, double z)
{
   x-= ave; / * Subtract the average from each data * /
   y-= ave;
   z-= ave;

   a = x;
   b = y;
   c = z;

   return a, b, c;
}

this is totally wrong. you can only return one value at a time. And you haven't defined any datatype to your variables a,b and c

I suggest you learn the language properly

S_coderX451
  • 95
  • 1
  • 7