Function Program - C++ You are to write a program that will input any four float
ID: 3662838 • Letter: F
Question
Function Program - C++
You are to write a program that will input any four float values where the four variables (cin in the values) that are declared in the 'main' function ( not global variables) and perform the following calculations in functions: Have your functions return their values through the return (return value). Do not return values through a ‘pass-by-ref’ parameter unless otherwise stated.
0. read in one float number. You will call this function 4 times.
1. print out the four numbers (one argument function), the sum (two argument function), and the average of the four numbers (two argument function). (Three functions)
2. print out the difference between the first and second number and the difference between the third and fourth number. (one function called twice.)
3. print out the first number raised to the second number power (pow function) and the fourth number raised to the third number power. (one function called twice)
4. print out the largest of the four. (This function, you can only pass in two arguments at one time. You will call this function more than once.)
5. print out the smallest of the four. (This function, you can only pass in two arguments at one time. Same as the previous.)
6. print out the average of the three largest numbers using only the existing functions listed above. This is not a new function.
7. print out the average of the three smallest numbers using only the existing functions above. Same as previous.
8. print out a running total of the four numbers. Ex 10.243 17.775 33.288 ….. using the Sum function. The sum function has only two arguments.
Read in the following four numbers: 10.243 7.532 15.513 4.196 to test your code with.
Turn in the source code and the output file.
Restrictions: You have to use functions for each one of the above. The arguments must be ‘passed by value’ and no ‘pass by reference’. You have to pass in two or less arguments into each function and pass back the results (See above restriction on arguments). The main program is the only place you can do couts, NO couts in any function. Your 'main' can only have couts statements and function calls.
Explanation / Answer
#include<iostream>
#include<math.h>
using namespace std;
//function prototypes
float read();
void print(float);
float sum(float , float );
float average(float , float );
float difference(float , float );
float power(float , float);
float max(float ,float);
int main()
{
//declare four float variables
float a,b,c,d;
//call read four times
a=read();
b=read();
c=read();
d=read();
//print the numbers
print(a);
print(b);
print(c);
print(d);
//find sum of two functions called sum twice
float s=sum(a, b)+sum(c,d);
cout<<"Sum of four float vlaues : "<<s<<endl;
//find average of two functions called average twice
float avg=average(average(a,b),average(c,d));
cout<<"Average of four float vlaues : "<<avg<<endl;
//call difference of two numbers
float dif=difference(a,b);
cout<<"Difference between "<<a<<" and "<<b<<" is "<<dif<<endl;
//call difference of two numbers
dif=difference(c,d);
cout<<"Difference between "<<c<<" and "<<d<<" is "<<dif<<endl;
//calling power function
float pow=power(a,b);
cout<<"Power of "<<a<<"to "<<b<<" is "<<pow<<endl;
//calling power function
pow=power(c,d);
cout<<"Power of "<<c<<"to "<<d<<" is "<<pow<<endl;
//Call max method three times to get the maximum of two numbers
float large=max(a,b);
large=max(large,c);
large=max(large,d);
cout<<"Largest of four float values : "<<large<<endl;
//Call min method three times to get the smallest of two numbers
float small=min(a,b);
small=min(small,c);
small=min(small,d);
cout<<"Smallest of four float values : "<<small<<endl;
//find maximum of three numbers
float firstLargest=max(a,b);
firstLargest=max(firstLargest,c);
firstLargest=max(firstLargest,d);
float secondLargest=max(a,b);
secondLargest=max(secondLargest,c);
float thirdLargest=max(a,b);
//find average of three maximum numbers
float avgerageOfThree=average(average(firstLargest,secondLargest),thirdLargest);
cout<<"Average of three numbers : "<<avgerageOfThree<<endl;
//pause the program output on console until user enters a key value
system("pause");
return 0;
}
//Returns the maximum of two float values m and n
float max(float m,float n)
{
//assume m is maximum
int maximum=m;
if(n>maximum)
maximum=n;
return maximum;
}
//Returns the minimum of two float values m and n
float min(float m,float n)
{
//assume m is maximum
int minimum=m;
if(n>minimum)
minimum=n;
return minimum;
}
//The method power raised m to the power of n
float power(float m, float n)
{
//call pow function of math library
return pow(m,n);
}
//Returns the difference of the two numebrs
float difference(float num1, float num2)
{
return abs(num1-num2);
}
//Retunrs the average of two numbers
float average(float num1, float num2)
{
return (num1+num2)/2.0;
}
//Returns the sum of the two numbers
float sum(float num1, float num2)
{
return num1+num2;
}
//prints the float value num on console
void print(float num)
{
cout<<"float value : "<<num<<endl;
}
//Read a float value in cin and returns the value
float read()
{
float value;
cout<<"Enter float value : ";
cin>>value;
return value;
}
------------------------------------------------------------------------------------------------------------
Sample output:
Enter float value : 10.243
Enter float value : 7.532
Enter float value : 15.513
Enter float value : 4.196
float value : 10.243
float value : 7.532
float value : 15.513
float value : 4.196
Sum of four float vlaues : 37.484
Average of four float vlaues : 9.371
Difference between 10.243 and 7.532 is 2.711
Difference between 15.513 and 4.196 is 11.317
Power of 10.243to 7.532 is 4.07885e+007
Power of 15.513to 4.196 is 99119.6
Largest of four float values : 15
Smallest of four float values : 4.196
Average of three numbers : 12.5
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.