Sunday, 10 July 2016

Chapter 4 Drill // 4.8 - 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.8

Reject values without units or with 'illegal' representations of units, such as y, yard, meter, km, and gallons.

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


int main()
{
double number = 0;
double converted = 0;
double largest = 0;
double lrgConverted = 0;
double smallest = 0;
double smlConverted = 0;

string unit = "";
string largestUnit;
string smallestUnit;

const double m_to_cm = 100;
const double in_to_cm = 2.54;
const double ft_to_cm = 12;

cout << "Please enter a number: \n";
cout << "Please enter a measurement unit, cm, m, in or ft: \n";

while (cin >> number >> unit)
{
if (unit != "cm" && unit != "ft" && unit != "in" && unit != "m")
{
cout << "\nSorry, that is an invalid unit. Please enter a number and valid unit: \n";
}

else
{
cout << "\nYou entered: " << number << unit;

if (unit == "m")
{
converted = number*m_to_cm;
}
else if (unit == "in")
{
converted = number*in_to_cm;
}
else if (unit == "ft")
{
converted = ((number*ft_to_cm)*in_to_cm);
}
else if (unit == "cm")
{
converted = number;
}

//

if (largest == 0 && smallest == 0)
{
largest = number;
smallest = number;
largestUnit = unit;
smallestUnit = unit;

lrgConverted = converted;
smlConverted = converted;

cout << "\nThe largest number so far is: " << largest << largestUnit;
cout << "\nThe smallest number so far is: " << smallest << smallestUnit << endl;
}

//

else if (converted > lrgConverted)
{
largest = number;
largestUnit = unit;
lrgConverted = converted;

cout << "\nThe largest number so far is: " << largest << largestUnit;
cout << "\nThe smallest number so far is: " << smallest << smallestUnit << endl;
}

else if (converted < lrgConverted)
{
if (converted < smlConverted)
{
smallest = number;
smallestUnit = unit;
smlConverted = converted;
}

cout << "\nThe largest number so far is: " << largest << largestUnit;
cout << "\nThe smallest number so far is: " << smallest << smallestUnit << endl;
}

cout << "\nPlease enter a number: ";
cout << "\nPlease enter a measurement unit, cm, m, in or ft: \n";
}
}
keep_window_open();

return 0;
}





This one seemed relatively easy to me as I've gotten into quite the habit of checking for bad input. 

Friday, 8 July 2016

Chapter 4 Drill // 4.7 - 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.7

Add a unit to each double entered; that is, enter values such as 10cm, 2.5in, 5ft or 3.33m. Accept thr four units cm, m , in ft. Assume conversion factors 1m == 100cm, 1in == 2.54cm, 1ft == 12in. Read th eunit indicator into a string. You may consider 12 m (with a space between the number and the unit to 12m (without a space).


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


int main()
{
double number = 0;
double converted = 0;
double largest = 0;
double lrgConverted = 0;
double smallest = 0;
double smlConverted = 0;

string unit;
string largestUnit;
string smallestUnit;

const double m_to_cm = 100;
const double in_to_cm = 2.54;
const double ft_to_cm = 12;

cout << "Please enter a number: \n";
cout << "Please enter a measurement unit, cm, m, in or ft: \n";

while (cin >> number >> unit)
{
cout << "\nYou entered: " << number << unit;

if (unit == "m")
{
converted = number*m_to_cm;
}
else if (unit == "in")
{
converted = number*in_to_cm;
}
else if (unit == "ft")
{
converted = ((number*ft_to_cm)*in_to_cm);
}
else if (unit == "cm")
{
converted = number;
}

//

if (largest == 0 && smallest == 0)
{
largest = number;
smallest = number;
largestUnit = unit;
smallestUnit = unit;

lrgConverted = converted;
smlConverted = converted;

cout << "\nThe largest number so far is: " << largest << largestUnit;
cout << "\nThe smallest number so far is: " << smallest << smallestUnit << endl;
}

//

else if (converted > lrgConverted)
{
largest = number;
largestUnit = unit;
   lrgConverted = converted;

cout << "\nThe largest number so far is: " << largest << largestUnit;
cout << "\nThe smallest number so far is: " << smallest << smallestUnit << endl;
}

else if (converted < lrgConverted)
{
if (converted < smlConverted)
{
smallest = number;
smallestUnit = unit;
smlConverted = converted;
}

cout << "\nThe largest number so far is: " << largest << largestUnit;
cout << "\nThe smallest number so far is: " << smallest << smallestUnit << endl;
}

cout << "\nPlease enter a number: ";
cout << "\nPlease enter a measurement unit, cm, m, in or ft: \n";
}

keep_window_open();

return 0;
}


This one took me roughly around 6 hours to complete, at first I thought it couldn't be done but as I stared at the screen wondering why I had decided to become a games developer instead of a photographer, it slowly came to me.

The hardest part was comparing all the values but still getting them to show in their original form, a lot of variables are needed for this one.

Basically, after the data has been inputted, we need to convert that into a universal number that we can use for comparing. I chose cm. 

The first If statement, like 4.6, gets the ball rolling and sets all the variables up for the next number. 

Another value is entered and it converts it to cm based on what unit you put in and then checks if that number is smaller or larger than the original smallest or highest number. 

If it is larger, the original largest number is replaced by the current, same for the unit. If it's smaller, it then checks to see if it's smaller than the current smallest number, if not, nothing happens. If it is smaller though, the smallest number is replaced by that number and unit is replaced too. 

Now it's finished, it seems stupidly simple and whereas I'm half elated that I finally managed to figure it out and get it to work, I'm half annoyed at myself. I'm pretty sure that there is a way to simply the code but I'm glad that Bjarne is making us figure out how to do it the long way first.


Tuesday, 5 July 2016

Chapter 4 Drill // 4.6 - 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.6

Now change the body of the loop so that it reads just one double each time around. Define two variables to keep track of which is the smallest and which is the largest value you have seen so far. Each time through the loop write out the value entered. If it is the largest so far, write the largest so far after the number, if its the smallest write the smallest so far after the number.

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


int main()
{
double number;
double largest = 0;
double smallest = 0;

cout << "Please enter a number: \n";


while (cin >> number)

cout << "\nYou entered: " << number;

{
if (largest == 0 && smallest == 0)
{
largest = number;
smallest = number;
cout << "\nThe smallest value so far is: " << smallest;
cout << "\nThe largest value so far is : " << largest << endl;
}

else if (number > largest)
{
largest = number;
cout << "\nThe smallest value so far is: " << smallest;
cout << "\nThe largest value so far is : " << largest << endl;

}

else if (number < largest)
{
if (number < smallest)
{
smallest = number;
}
cout << "\nThe smallest value so far is: " << smallest;
cout << "\nThe largest value so far is : " << largest << endl;
}

cout << "\nPlease enter a number: \n";

}

}

keep_window_open();

return 0;

}


This one didn't take me as long as 4.5 and after some head scratching got away with only 4 'if' statements.

I've found that Bjarne likes to deliberately make his exercises as vague as possible, in this one you are practically abandoning everything you have done from 4.1-4.5. 

Seeing as how I only had 1 variable to play with this time I couldn't directly start off comparing it with other values (as we have none) so the first 'if' statement gets the ball rolling setting number as the largest and smallest value we've seen so far.

The next if statement checks if number is larger than largest, if it is then it sets that as the largest variable and prints the current smallest and largest variables we've seen so far.

The 3rd if statement checks if number is smaller than the current largest number and has another if statement embedded in it. If number is smaller than largest and then number is smaller than smallest, smallest becomes number. If not nothing happens and the current largest and smallest numbers are printed. 

It was this last bit that kept throwing me as my loop would work fine but if number was smaller than largest then it just kept setting the current number as smallest which I didn't want because sometimes it wasn't the smallest.

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.


Friday, 1 July 2016

Chapter 4 Drill // 4.1 + 4.2 + 4.3 + 4.4 - 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.


4.1 - Write a program that consists of a while-loop that (each time around the loop) reads in two ints and then prints them. Exit the program when a terminating '|' is entered.

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



int main()
{
int one;
int two;

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


while (cin >> one >> two)
cout << one << '\n' << two << '\n' << "\nPlease enter two integers: \n";

keep_window_open();

return 0;
}

I'll be honest. At first this through me for a loop (pun intended), and searching the internet it seemed to throw other new users for loop because instead of actually just pressing '|' and hitting enter to close the program I thought he meant to create code that when '|' was entered the program would end....I know I'll wear my dunce hat. However I didn't actually know that '|' would terminate a program if pressed. Sometimes I wish Bjarne would make his instructions clearer.

4.2 - Change the program to write out 'the smaller value is: ' followed by the smaller of the numbers and 'the larger value is: ' followed by the larger value.


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


int main()
{
int one;
int two;

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


while (cin >> one >> two)

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

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

}

}

keep_window_open();

return 0;

}

4.3 - Augment the program so that it writes the line 'the numbers are equal ' (only) if they are equal.

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


int main()
{
int one;
int two;

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


while (cin >> one >> two)

{
if (one > two)
{
cout << "\nThe smaller value is: " << two << endl;
cout << "\nThe larger value is : " << one << endl;
}
else if (one < two)
{
cout << "\nThe smaller value is: " << one << endl;
cout << "\nThe larger value is : " << two << endl;
}
else
cout << "\nThe numbers are equal." << endl;

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

}

}

keep_window_open();

return 0;

}

4.4 - Change the program so it uses doubles instead of ints.

I'm not going to bother showing code for this one because you simply just replace 'int' with 'double'. That's it.