Answer to Question #99442 in C++ for Caleb

Question #99442
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 <iostream>
#include <cmath>
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;
}
1
Expert's answer
2019-12-02T12:29:19-0500
#include <iostream>
#include <math.h>
using namespace std;


int main() {
	double targetValue;
	double sensorReading;


	cin >> targetValue;
	cin >> sensorReading;


	if (abs(targetValue - sensorReading)<=1e-4) {
		cout << "Equal" << endl;
	}
	else {
		cout << "Not equal" << endl;
	}
	system("pause");
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS