Sunday, 3 July 2016

Chapter 4 Drill // 4.5 - Principles & Practice Using C++

In all these exercises I am using Visual Studio Community 2015 and the header file "std_lib_facilities.h" which can be found here:


http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h


My version is spelt differently so adjust the code accordingly if copying and pasting.


Chapter 4 Drill // 4.5

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/100.

#include "stdafx.h"
#include "std_lib_facilities_new_version.h"
using namespace std;


int main()
{
double one;
double two;
double larger, smaller; //to compare difference between values

cout << "Please enter two integers: \n";


while (cin >> one >> two)

{
if (one > two)
{
cout << "\nThe smaller value is: " << two;
cout << "\nThe larger value is : " << one;
larger = one;
smaller = two;
}
else if (one < two)
{
cout << "\nThe smaller value is: " << one;
cout << "\nThe larger value is : " << two;
larger = two;
smaller = one;
}

if (one == two)
{
cout << "\nThe numbers are equal." << endl;
larger = 0;
smaller = 0;
}
else if ((larger - smaller) < 1.0 / 100)
{
cout << "\nThe numbers are almost equal." << endl;
}

cout << "\nPlease enter two integers: \n";

}

}

keep_window_open();

return 0;

}

Ok, this one took me a while. Mainly because I felt like using the book to bash my head in as it seemed like Mr Stroustrup was trying to make me do something that the book hadn't taught me yet.

I spent many hours scouring the internet to find the answer and fellow newbies like myself had posted asking for help but the replies were far too technical for how far we had gotten in the book. One person even resorted to using fabs() which  again the book has not yet got to.

After hours I finally cracked it and when I did, I was so annoyed with myself for over complicating something which was staring at me right in the face. Bjarne has carefully constructed these drills to make us over-complicate it. The answer to this part of the drill only uses functions we have been taught so far, as it should.

The hardest part about this was getting it to show "the numbers are almost equal" and "the numbers are equal" separately.

Previously everything I had been doing was because my final if formula depended on the user always inputting val1 as the higher number. But as we all know, if someone can put data in how you don't want it, they will.

I ended up creating new variables which store that loops numbers for comparison. If they are equal larger and smaller are reset back to 0 because I found that if left alone, those numbers would carry on into the next loop creating unexpected results later on down the line.

A final thought, having ((larger-smaller) < 1.0/100)  as an else if after the initial if of (val1 == val2) makes all the difference. It was getting on my nerves because I realised that if larger-smaller was smaller than 0.01 it would set them both off anyway. Now it evaluates to see if they are equal first and if not prints they are almost equal if the difference is less than 0.01.

No need for fabs() or epsilon just yet.


No comments:

Post a Comment