Ask Question
9 July, 23:35

Write an expression that will cause the following code to print "Equal" if the value of sensorReading is "close enough" to targetValue. Otherwise, print "Not equal".

+1
Answers (2)
  1. 10 July, 01:10
    0
    Full Question

    Write an expression that will cause the following code to print "Equal" if the value of sensorReading is "close enough" to targetValue. Otherwise, print "Not equal". Ex: If targetValue is 0.3333 and sensorReading is (1.0/3.0), output is: Equal

    #include

    #include

    using namespace std;

    int main () {

    double targetValue;

    double sensorReading;

    cin >> targetValue;

    cin >> sensorReading;

    if ( / * Your solution goes here * / ) {

    cout << "Equal" << endl;

    } else {

    cout << "Not equal" << endl;

    } return 0;

    }

    Answer:

    if (abs (targetValue - sensorReading) < = 0.0001)

    Explanation:

    Replace

    if ( / * Your solution goes here * / ) {

    With

    if (abs (targetValue - sensorReading) < = 0.0001)

    Two values are considered to be close enough if the difference between the values is 0.0001

    Splitting the codes into bits;

    abs

    targetValue - sensorReading

    <=

    0.0001

    The keyword abs is to return the absolute value of the expression in bracket.

    This is needed because the expression in the bracket is expected to return a positive value.

    To do this the absolute keyword is required.

    targetValue - sensorReading returns the difference between the variables

    < = compares if the difference falls within range that should be considered to be close enough

    0.0001 is the expected range
  2. 10 July, 02:32
    0
    The program to this question as follows:

    Program:

    targetValue = 0.3333 #defining variable targetValue and assign value

    sensorReading = 0.0 #defining variable sensorReading and assign value

    sensorReading = 1.0/3.0 #calculate value in sensorReading variable

    Val=sensorReading - targetValue

    #calculate the difference and store in Val variable

    if (Val < 0.0001) : #use of if block to check condition

    print ("Equal") #print value

    else: #else block

    print ("Not equal") #print value

    Output:

    Equal

    Explanation:

    In the above Python program code, there are two variables "targetValue and sensorReading" is defined, in which targetValue store a value, that is "0.3333", and sensorReading holds a value, that is "0.0".

    In the next step, the "Val" variable is defined, that calculate the difference between both variable, that conditional statement is used. In if block, if the value is less then "0.0001", it will print value "Equal", otherwise, it will go to the else block, that will print "Not equal".
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write an expression that will cause the following code to print "Equal" if the value of sensorReading is "close enough" to targetValue. ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers