My Functions\" The ability to write functions is one of the more powerful capabi
ID: 3591690 • Letter: M
Question
My Functions" The ability to write functions is one of the more powerful capabilities in C. Functions allow code to be reused, and provides a mechanism for introducing the concept of modularity to the programming process. In addition to the concept of functions, C allows complex programs to be organized using header files (.h files) that contain the prototypes of functions and implementation files that (.cpp files) that contain the implementation (definition) of the functions. In this programming assignment you will be asked to implement a number of useful functions using a header file and an implementation file. You will place the prototypes for your function in a .h file, and implement these functions in a.c file. You will need to write a driver (a program designed to test programs) to test your functions before you upload them. Deliverables MyFunctions.h Driver.c Function 1. maximum . two double values exist The value of the larger double is returned. The original doubles are unchanged returned. double Function returns the value of the larger of two doubles. . If the doubles have the same value then the value of either double is c Return e Prototype: double maximum (double, double); 2. minimum . two double values exist The value of the smaller double is returned. The original doubles are unchanged If the doubles have the same value then the value of either double is returned. . c Return double Function returns the value of the smaller of two doubles. e Prototype: double minimum (double, double) 3. absolute
Explanation / Answer
Here is the .h file:
#ifndef MY_FUNCTIONS_H
#define MY_FUNCTIONS_H
double maximum(double, double);
//Precondition: two double values exist.
//Postcondition: The value of the larger double is returned.
// The original doubles are unchanged.
// If the doubles have the same value then the value of either double is returned.
//Function returns the value of the larger of two doubles.
double minimum(double, double);
//Precondition: two double values exist.
//Postcondition: The value of the smaller double is returned.
// The original doubles are unchanged.
// If the doubles have the same value then the value of either double is returned.
//Function returns the value of the smaller of two doubles.
double absolute(double);
//Precondition: Some double value exists.
//Postcondition: Double value is unchanged.
///Function returns the absolute value of a double.
double power(double, int);
//Precondition: Double value X and integer value Y exist.
//Postcondition: The value of X and Y are unchanged.
//This function will calculate x^y.
double squareRoot(double);
//Precondition: Some double value X exists.
// NaN is defined in NaN.h
//Postcondition: The value of the radicand X is unchanged.
//The function calculates the square root of a number using Newtons method with 25 iterations.
#endif
The .c file is:
#include "MyFunctions.h"
double maximum(double value1, double value2)
{
return value1 > value2 ? value1 : value2;
}
double minimum(double value1, double value2)
{
return value1 < value2 ? value1 : value2;
}
double absolute(double value)
{
return value < 0 ? -value : value;
}
double power(double base, int exp)
{
double result = 1;
for(int i = 0; i < exp; i++)
result *= base;
return result;
}
double squareRoot(double value)
{
double root = value/2;
for(int i = 0; i < 25; i++)
{
root = 0.5 * (value + squareRoot(root)/value);
value = root;
}
return root;
}
And the Demo file is:
#include <stdio.h>
#include "MyFunctions.c"
int main()
{
double value1 = 10.5, value2 = -5.7;
printf("The maximum of %f and %f is: %f ", value1, value2, maximum(value1, value2));
printf("The minimum of %f and %f is: %f ", value1, value2, minimum(value1, value2));
printf("The absolute of %f is %f ", value1, absolute(value1));
printf("The absolute of %f is %f ", value2, absolute(value2));
printf("%f raised to the power of 2 is: %f ", value1, power(value1, 2));
printf("The square root of 3 is: %f ", squareRoot(3));
printf("The square root of 25 is: %f ", squareRoot(25));
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.