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
// 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>
usingnamespace 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?
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.