Ask 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".

+4
Answers (2)
  1. 28 October, 11:00
    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".
  2. 28 October, 11:21
    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
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 & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers