Program Plan:
• Read the user to enter input values for x, y, and z.
• Part a: Implement all_the_same function that accepts x, y, and z arguments as parameters. It returns true, if x, y, and z are equal, otherwise it returns false.
• Part b: Implement all_different function that accepts x, y, and z arguments as parameters. It returns true, if all three values are different. Otherwise it returns false.
• Part c: Implement sorted function that accepts x, y, and z arguments as parameters. It returns true, if the x, y, and z values are sorted in ascending order; otherwise it returns false.
a) Function “all_the_same”:
The all_the_same function definition uses an if/else statement to determine if x and y are equal and y and z are equal then it returns true otherwise returns false.
/* The function "all_the_same" accepts the three arguments
x, y, and z and returns true if the arguments are same.*/
bool all_the_same(double x, double y, double z)
{
if (x == y && y == z)
{
cout << "All values are the same" << endl;
return true;
}
else
{
return false;
}
}
b)
The function definition all_different uses an if/else statement to determine if all values are different then it returns true otherwise returns false.
/* The function "all_different" accepts the three arguments
x, y, and z and returns true if the arguments are all different.*/
bool all_different(double x, double y, double z)
{
if (x != y && x != z && y != z)
{
cout << "All values are different" << endl;
return true;
}
else
{
return false;
}
}
c)
The function definition sorted uses an if/else statement to determine if all values are entered from smallest to largest then it returns true otherwise returns false.
/* The function "sorted" accepts the three arguments x, y, and z and returns true if the arguments are sorted with
the smallest value coming first*/
bool sorted(double x, double y, double z)
{
if (x <= y && y <= z)
{
cout << "Values are sorted" << endl;
return true;
}
else
{
return false;
}
}
Test Program Code:
//Headers file section
#include
using namespace std;
/* The function "all_the_same" accepts the three arguments
x, y, and z and returns true if the arguments are all the
same.*/
bool all_the_same(double x, double y, double z)
{
If variable values x, y, and z are all the same, prints a statement to the user and returns true.
if (x == y && y == z)
{
cout << "All values are the same" << endl;
}
else
{
return false;
}
}
/* The function "all_different" accepts the three arguments
x, y, and z and returns true if the arguments are all different.*/
bool all_different(double x, double y, double z)
{
If variable values x, y, and z are different, prints a statement to the user and returns true.
if (x != y && x != z && y != z)
{
cout << "All values are different" << endl;
return true;
}
else
{
return false;
}
}
The function definition sorted uses an if/else statement to determine if x is less than (or equal to) y, and y is less than (or equal to) z, if it is true, print a statement to the user and returns true otherwise returns false.
/* The function "sorted" accepts the three arguments
x, y, and z and returns true if the arguments are sorted with the smallest value coming first*/
bool sorted(double x, double y, double z)
{
if (x <= y && y <= z)
{
cout << "Values are sorted" << endl;
return true;
}
else
{
return false;
}
}
Program begins with main function.
//main function
int main()
{
//Declare double data type variables
double x,y,z;
Prompt and read the user input values x, y, and z.
//Prompt and read the value of x
cout << "Enter a value for x: ";
cin >> x;
//Prompt and read the value of y
cout << "Enter a value for y: ";
cin >> y;
//Prompt and read the value of z
cout << "Enter a value for z: " ;
cin >> z;
//Call the functions
bool part_a = all_the_same(x, y, z);
bool part_b = all_different(x, y, z);
bool part_c = sorted(x, y, z);
//Pause the system for a while
system("PAUSE");
}
Sample Output:
Enter a value for x: 1
Enter a value for y: 2
Enter a value for z: 3
All values are different
Values are sorted