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.
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.
No comments:
Post a Comment