Finding a range between 2 numbers

Ok so I'm reading the Programming: Principles and Practice using C++ and Im stuck in Drill 4 part 5. It says:

Change the program so that it writes out the "numbers are almost equal"
after writing out which is the larger and the smaller if the two numbers
differ by less than 1.0/10000000

I'm using an If statement for it... I just need know what the formula is to check 2 numbers that were entered by person if they land within the range specified above. so then I can cout << "numbers are almost equal" << endl;

Thanks again
1
2
3
4
5
6
7
8
9
10
11
12
13
 
if(x > y)
{
    x = x - 1.0/10000000; 
    if(x < y)
   {
        cout << "Numbers are nearly equal!";
    }
}
else if(y > x)
{
    //Same code other way round. 
}


Should work?
I couldn't get that to work.... here is what I have so far... u just need to get it to work with what I have here... please excuse my not so pretty code.


#include <iostream>

int main()
{
using namespace std;

double inputone;
double inputtwo;
char flag = '|';

cout.setf(ios_base::fixed, ios_base::floatfield);
cout.precision(9);

cout << "Enter integers and an '|' to terminate program: ";
while (cin >> inputone >> inputtwo)
{
cout << "Your input was " << inputone << " " << inputtwo << "\n";

if (inputone < inputtwo)
cout << "the smaller value is: " << inputone << "\n"
<< "the larger value is: " << inputtwo << "\n"
<< "\nEnter integers and an '|' to terminate program: \n";
else if (inputone > inputtwo)
cout << "the smaller value is: " << inputtwo << "\n"
<< "the larger value is: " << inputone << "\n"
<< "\nEnter integers and an '|' to terminate program: \n";
else if (inputone = inputtwo)
cout << "the numbers are equal\n"
<< "\nEnter integers and an '|' to terminate program: \n";
}
cout << "\nYou entered " << flag << "... so the program will terminate.\n";

return 0;
}
Quick example:
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
#include <iostream>

int main()
{
    double x = 1.55551, y = 1.55554;
    if(x == y)
    {
        std::cout << "They are equal!\n";
    }
    else if(x > y)
    {
        if(x - (0.0001) < y)
        {
            std::cout << "Difference is less than 0.0001\n";
        }
    }
    else if(y > x)
    {
        if(y - 0.0001 < x)
        {
            std::cout << "Difference is less than 0.0001\n";
        }
    }
    else
    {
        std::cout << "Difference is greater than or equal to 0.0001";
    }
    return 0;
}


Try changing the values of x and/or y.
Last edited on
Thanks for the help I found out what I needed to do :)
Topic archived. No new replies allowed.