Evaluating the precision of the difference of 2 numbers

As you will understand I am an absolute beginner and therefore your help is really appreciated. The problem asks to write a program that contains a function f(double x, double y, double precision) that returns 1 if the absolute value of the difference x-y is less than the precision. The program should repeat the process 3 times of reading x,y and precision and indicating whether the numbers are equal up to the indicated precision.

i.e the output should look something like:
Input x=2.1
Input y=2.1
input precision 1E-05
The numbers are equal to that precision

and do that 3 times, giving inputs on 3 different occasions

What I got so far is:



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Prorgamm for Assignment 2a.cpp 
// Returns 1 if the absolute value of the difference of two variables x, y is lees than the precision specified.
#include <iostream>
#include <math.h>
using namespace std;

// Define the function IsEqual

double IsEqual(double x, double y, double precision)

{
	double r;
	r=abs(x - y);
	
	return r;
}

int main()

{
//Input x,y, precision
double x, y, precision;

cout <<"Give me X: ";
cin >> x;

cout <<"Give me Y: ";
cin >> y;

cout <<"Give me a precision:";
cin>> precision;

//Define local r
// Set conditions for which number are equal at the indicated precision
double r;

if(r=precision){ //OR should it be if(r==precision)
	cout<<"The numbers are equal at this precision";
}else{
	cout<<"The numbers are not equal at tha precision";
}
return 0;

}


Does this work? When i run the program and after setting the values of x,y, precision I get A message and then output screnn closes shortly after (cant really see the answer and test if the above works)

How do i get to run the program three times, each time giving different values for x, y precision?


Thank you in advance!!!

Last edited on
Of course it would not work. You have not even called IsEqual in your main routine. Double precision parameter and double r in the function definition are redundant.
r in your main routine is uninitialized.
Last edited on
Topic archived. No new replies allowed.