Showing posts with label chapter 4 drill. Show all posts
Showing posts with label chapter 4 drill. Show all posts

Thursday, 14 July 2016

Chapter 4 Drill // 4.10 & 4.11 - 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.10

Keep all the values entered (converted into meters) in a vector. At the end, write out those values.

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


int main()
{
int no_values = 0;

double number = 0;
double converted = 0;
double finalConversion = 0;
double fc2 = 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_in = 12;
const double in_to_m = 0.0254;
const double ft_to_m = 0.3048;
const double cm_to_m = 0.01;

vector<double>m_values;

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;
finalConversion += number;
fc2 = number;
m_values.push_back(fc2);
}
else if (unit == "in")
{
converted = number*in_to_cm;
finalConversion += number*in_to_m;
fc2 = number*in_to_m;
m_values.push_back(fc2);
}
else if (unit == "ft")
{
converted = (number*ft_to_in)*in_to_cm;
finalConversion += number*ft_to_m;
fc2 = number*ft_to_m;
m_values.push_back(fc2);
}
else if (unit == "cm")
{
converted = number;
finalConversion += number*cm_to_m;
fc2 = number*cm_to_m;
m_values.push_back(fc2);
}


//

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

lrgConverted = converted;
smlConverted = converted;
}

//

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

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

cout << "\nThe number of values entered so far : " << ++no_values;
cout << "\nThe sum of values entered so far in meters is: " << finalConversion;
cout << "\nThe smallest value entered so far is: " << smallest << smallestUnit;
cout << "\nThe largest value entered so far is: " << largest << largestUnit << endl;
cout << "\nValues entered so far in meters: ";
for (int i = 0; i < m_values.size(); ++i)
cout << m_values[i] << " ";

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

return 0;
}


Chapter 4 Drill // 4.11

Before writing out the values from the vector, sort them (that'll make them come out in increasing order).

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


int main()
{
int no_values = 0;

double number = 0;
double converted = 0;
double finalConversion = 0;
double fc2 = 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_in = 12;
const double in_to_m = 0.0254;
const double ft_to_m = 0.3048;
const double cm_to_m = 0.01;

vector<double>m_values;

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;
finalConversion += number;
fc2 = number;
m_values.push_back(fc2);
}
else if (unit == "in")
{
converted = number*in_to_cm;
finalConversion += number*in_to_m;
fc2 = number*in_to_m;
m_values.push_back(fc2);
}
else if (unit == "ft")
{
converted = (number*ft_to_in)*in_to_cm;
finalConversion += number*ft_to_m;
fc2 = number*ft_to_m;
m_values.push_back(fc2);
}
else if (unit == "cm")
{
converted = number;
finalConversion += number*cm_to_m;
fc2 = number*cm_to_m;
m_values.push_back(fc2);
}


//

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

lrgConverted = converted;
smlConverted = converted;
}

//

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

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

cout << "\nThe number of values entered so far : " << ++no_values;
cout << "\nThe sum of values entered so far in meters is: " << finalConversion;
cout << "\nThe smallest value entered so far is: " << smallest << smallestUnit;
cout << "\nThe largest value entered so far is: " << largest << largestUnit << endl;
cout << "\nValues entered so far in meters: ";
for (int i = 0; i < m_values.size(); ++i)
{
sort(m_values);
cout << m_values[i] << " ";
}


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

return 0;

}



These two again were not as bad as the previous and just required adding a vector, printing it to the screen and then sorting them.

Then to sort I just added the very hand sort() function before it prints them out.

And that's the Chapter 4 drill done! Not going to lie, this took me far longer than I expected, it was extremely challenging for a complete novice but I got there in the end. The hardest thing for me about this drill was having to sort out in my head which variables should be assigned to each other after a certain outcome. It's like trying to predict the future for every possible outcome. I suppose that could just sum up programming in itself though.


Tuesday, 12 July 2016

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


Drill 4.9 - Keep track of the sum of values entered (as well as the smallest and the largest) and the
number of values entered. When the loop ends, print the smallest, the largest, the number of values
and the sum of values. Note that to keep the sum, you have to decide on a unit for that sum; use meters.

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


int main()
{
int no_values = 0;

double number = 0;
double converted = 0;
double finalConversion = 0;
double fc2 = 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_in = 12;
const double in_to_m = 0.0254;
const double ft_to_m = 0.3048;
const double cm_to_m = 0.01;


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;
finalConversion += number;
fc2 = number;
}
else if (unit == "in")
{
converted = number*in_to_cm;
finalConversion += number*in_to_m;
fc2 = number*in_to_m;
}
else if (unit == "ft")
{
converted = (number*ft_to_in)*in_to_cm;
finalConversion += number*ft_to_m;
fc2 = number*ft_to_m;
}
else if (unit == "cm")
{
converted = number;
finalConversion += number*cm_to_m;
fc2 = number*cm_to_m;
}

//

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

lrgConverted = converted;
smlConverted = converted;
}

//

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

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

cout << "\nThe number of values entered so far : " << ++no_values;
cout << "\nThe sum of values entered so far in meters is: " << finalConversion;
cout << "\nThe smallest value entered so far is: " << smallest << smallestUnit;
cout << "\nThe largest value entered so far is: " << largest << largestUnit << 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 was actually the easiest one of the bunch so far and required little tinkering to the code. Basically 4.9 wants us to keep a count of how many values are entered, and create a sum of all the values entered so far in meters. Then print these out at the end of the loop along with the smallest and the largest.


no_values was created to keep track of how many values we input. This was the easiest part. Since we only input 1 value each time round the loop, right at the end I just added ++no_values. Also, since it's inside the 'else' part of the entire code, it only increases if a valid unit is entered.

Next I created 3 more const variables to convert our number into meters. I added this to the beginning conversion so I didn't have to create anther set of if statements. I was wondering what loop to use to keep adding the number onto each other and then I remembered I was idiot and used += instead. 

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.