/*----------------------------------------------------------------*/ /* testfloatmath.c (modified from testintmath.c) */ /* Author: Xiaoyan Li */ /*----------------------------------------------------------------*/ #include #include /*--------------------------------------------------------------------*/ /* Return the maximum of three float numbers: fFirst, fSecond, and fThird. */ static float max(float fFirst, float fSecond, float fThird) { float fMax; if (fFirst >= fSecond){ fMax = fFirst; }else { fMax = fSecond; } if (fThird > fMax){ fMax = fThird; } return fMax; } /*--------------------------------------------------------------------*/ /* Return the minimum of three float numbers: fFirst, fSecond, and fThird. */ static float min(float fFirst, float fSecond, float fThird) { float fMin; if (fFirst <= fSecond){ fMin = fFirst; }else { fMin = fSecond; } if (fThird < fMin){ fMin = fThird; } return fMin; } /*--------------------------------------------------------------------*/ /* Return the mean of the three float numbers: fFirst, fSecond, and fThird. */ static float mean(float fFirst, float fSecond, float fThird) { return (fFirst+fSecond+fThird)/3.0; } /*--------------------------------------------------------------------*/ /*--------------------------------------------------------------------*/ /* Read three float numbers from stdin. Return EXIT_FAILURE if stdin contains bad data and write error message to stderr. Otherwise compute the maximum, minimum, and the mean of the three numbers. Write those three values to stdout, and return 0. */ int main(void) { float f1; float f2; float f3; float fMax; float fMin; float fMean; int iScanfReturn; printf("Enter the first number:\n"); iScanfReturn = scanf("%f", &f1); if (iScanfReturn != 1) { fprintf(stderr, "Error: Not a number.\n"); exit(EXIT_FAILURE); } printf("Enter the second number:\n"); iScanfReturn = scanf("%f", &f2); if (iScanfReturn != 1) { fprintf(stderr, "Error: Not a number.\n"); exit(EXIT_FAILURE); } printf("Enter the third number:\n"); iScanfReturn = scanf("%f", &f3); if (iScanfReturn != 1) { fprintf(stderr, "Error: Not a number.\n"); exit(EXIT_FAILURE); } fMax = max(f1, f2, f3); fMin = min(f1, f2, f3); fMean = mean(f1, f2, f3); printf("The maximum of %f, %f, and %f is %f.\n", f1, f2, f3, fMax); printf("The minimum of %f, %f, and %f is %f.\n", f1, f2, f3, fMin); printf("The mean of %f, %f, and %f is %f.\n", f1, f2, f3, fMean); return 0; }