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:
- Input from the keyboard and display the result are performed on the main side. The value entered is a floating point number.
- 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.
- 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;
| ^ ~