190k views
5 votes
. Write a function definition as follows: it returns the C++ form of a Boolean value, its function identifier is anyTwoTheSame, it has three formal parameters which all are floating-point values, and the body of the function returns true if any two of the parameters are equal, otherwise it returns false. I recommend using short, simple identifiers for the three formal parameters.

1 Answer

3 votes

Answer:

The c++ code to implement the Boolean function is given. The function definition is shown.

bool anyTwoTheSame(float a, float b, float c)

{

bool same = false;

if(a == b)

same = true;

else if(a == c)

same = true;

else if(b == c)

same = true;

return same;

}

Step-by-step explanation:

This function accepts three floating point numbers and returns true if any two numbers are equal else returns false.

The program below shows the implementation of this method.

#include <iostream>

using namespace std;

bool anyTwoTheSame(float a, float b, float c);

bool anyTwoTheSame(float a, float b, float c)

{

bool same = false;

if(a == b)

same = true;

else if(a == c)

same = true;

else if(b == c)

same = true;

return same;

}

int main() {

float one=12.34, two=56.78, three=90.1;

cout<<"Two numbers are same : " <<anyTwoTheSame(one, two, three);

}

OUTPUT

Two numbers are same : 0

The method is first declared.

bool anyTwoTheSame(float a, float b, float c);

Next step is method definition which shows what the function is supposed to do. The test to check equality of two numbers is implemented as shown.

bool same = false;

if(a == b)

same = true;

else if(a == c)

same = true;

else if(b == c)

same = true;

This is the simplest test which can be programmed.

If the numbers are not same, the Boolean variable, same, remains initialized to false.

This function is then called in main method.

No user input is taken as this is not specified in the question.

The three floating variables are declared and initialized inside the main. The values are put inside the program and not taken from the user.

The output shows either 0 or 1. 0 represents false and 1 represents true.

This program can be tested for different values of floating variables.

User Machavity
by
6.6k points