Pages

Saturday, 16 July 2016

Chapter 4 Exercise // Try This - 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 - Try This pg 105

Use the example as a model for a program that converts yen ('y'), kroner ('k), and pounds ('p') into dollars. If you like realism, you can find conversion rates on the web.

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


int main()
{
double amount;
string currency;

cout << "Please select which currency you would like to convert: \n";
cout << "Y = Yen \nK = Kroner \nP = GB Pound\n";
cin >> currency;

cout << "Please enter the amount to convert to dollars: \n";
cin >> amount;

const double yToD = amount*0.0096;
const double kToD = amount*0.15;
const double pToD = amount*1.47;

if (currency == "Y" || currency == "y")
cout << "That is $" << yToD << endl;
else if (currency == "K" || currency == "k")
cout << "That is $" << kToD << endl;
else if (currency == "P" || currency == "p")
cout << "That is $" << pToD << endl;
else cout << "Sorry, that is an invalid unit. Please try again.";


keep_window_open();

return 0;

}

Chapter 4 - Try This pg 109

Rewrite the currency converter from the previous try this to use a switch-statement. Add conversions from yuan and kroner. Which version is easier to write, understand and modify? Why?

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


int main()
{
double amount;
char currency;

cout << "Please select which currency you would like to convert: \n";
cout << "Y = Yen \nK = Danish Kroner \nP = GB Pound\nC = Chinese Yuan\nS = Swedish Krona\n";
cin >> currency;

cout << "Please enter the amount to convert to dollars: \n";
cin >> amount;

const double yToD = amount*0.0096;
const double kToD = amount*0.15;
const double pToD = amount*1.47;
const double cToD = amount*0.15;
const double sToD = amount*0.12;

switch (currency)
{
case 'Y': case 'y':
cout << "That is $" << yToD << endl;
break;
case 'K': case 'k':
cout << "That is $" << kToD << endl;
break;
case 'P': case 'p':
cout << "That is $" << pToD << endl;
break;
case 'C': case 'c':
cout << "That is $" << cToD << endl;
break;
case 'S' : case 's':
cout << "That is $" << sToD << endl;
break;
default:
cout << "Sorry, that is an invalid unit, please try again.\n";
}


keep_window_open();

return 0;
}


I am a big fan of the If and Else If formula however the switch is much easier to read. That said, you can't compare strings on a switch but for numbers and chars it's much more ideal.

Chapter 4 - Try This pg 111

The character 'b' is char('a'+1), 'c' is char('a'+2), etc. Use a loop to write out a table of characters with their corresponding integer values.

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


int main()
{
int i = 'a';
char a = 'a';

while (i < 'z')
{
cout << a << '\t' << i << '\n';
++i;
++a;
}

keep_window_open();

return 0;

}


This took me so long to figure out mainly because I didn't understand exactly what he wanted me to do at first, it was only after some head scratching and other people being as confused as me online that I realized he was talking about the actual char numbers. It seems obvious now I've been learning for a few months but when you're a complete newbie I was like, where the eff are you getting these numbers from?


Chapter 4 - Try This pg 113


Rewrite the character value example from the previous try this to use a for-statement. Then modify your program to to also write out a table of the integer value for uppercase letters and digits.

Part 1 

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


int main()
{

for (char a = 'a'; a < 'z'; ++a)
{
cout << a << '\t' << int(a) << '\n';
++a;
}

keep_window_open();

return 0;
}

Part 2

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


int main()
{

for (char a = '0'; a < 'z'; ++a)
{
cout << a << '\t' << int(a) << '\n';
}

keep_window_open();

return 0;

}


It took me a while to figure this one out initially, mainly because I kept thinking that I had to keep both variables but then I remembered you can easily convert between char and int.

Chapter 4 - Try This pg 116

Implement square() without the multiplication operator; that is, do the x*x by repeated addition (start a variable result at 0 and add x to it x times). Then run some version of the "first program" using that square.

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

int square(int x)
{
//this variable will become x (the inputted value)
int y = 0;

//while z is less than x(inputted value), ++z and add x
//to y until z equals x. So if 2 is entered, 2 will be      added 
//to y until z equals 2.
for (int z = 0; z < x; ++z)
y += x;

return y;
}


int main()
{


cout << "Please enter a value to square: \n";
int value;
cin >> value;

cout << value << " squared is: " << square(value) << endl;

keep_window_open();

return 0;

}


I'm adding some extra comments to this mainly because I couldn't quite wrap my head around how the loop worked. Maths isn't my strongest subject (but that doesn't stop me from enjoying it) and a lot of explanations on the web just didn't explain it in dunce terms enough. So for those of you like me square() works like this:

  • (int x) will become whatever is written in the variable value
  • y is going to become the squared number that we will return.
  • z is created to count how many times we need to loop and add x to y. So if we enter 5, x becomes 5, the loop will check to see if z is smaller than 5. If it is, z will be incremented by 1 and x(5) will be added to y. This continues until z matches x, at which point y will have had x added to it enough times to create the squared number.

I know this loop is stupidly simple to most but for me it would have been great to find a explanation somewhere like this without some expert coming in and going "you noob, go learn some math". Now I've wrapped my head around it, I understand it but everyone learns at different rates and in different ways.


Chapter 4 - Try This pg 125


Write a program that "bleeps" out words you don't like; that is, you read in words using cin and print them again on cout. If a word is among a few you have defined, write out BLEEP instead of that word. Start with one "disliked word" such as:

string disliked = "broccoli";

When that works, add a few more.

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



int main()
{
vector<string> words;
string disliked = "broccoli";
string replacement = "BLEEP";

cout << "Please enter words, press enter then Ctrl + Z when finished: \n";

//read whitespaced words into enterWords
for (string enterWords; cin >> enterWords;)
words.push_back(enterWords);
 

//sort them 
sort(words);


//loop to print all words entered, so if 5 words
//are entered, each word will be printed until
//a == 4
for (int a = 0; a < words.size(); ++a)
{
if (words[a] != disliked)
cout << words[a] << endl;

else
cout << replacement << endl;
}

keep_window_open();

return 0;
}

OK, this one was a bit of an arse mainly because ctrl+z doesn't actually seem to stop input in a loop, well not on my computer anyway. I spent a long time searching the internet for answers as well but again came up with know-it-alls saying "oh just do this" (something we haven't learnt yet or can even understand) or "why are you even bothering?". 

For some reason when I press ctrl+z in my loop it shows "^Z" and would read this into the input stream because it's a valid character. The trick I learnt to get it to actually work though was to press enter, then press ctrl+z, then press enter again. This ended the input loop and proceeded to the next part of the program.

#include "stdafx.h"
#include "std_lib_facilities_new_version.h"
#include <vector>
using namespace std;



int main()
{
vector<string> words;
string d1 = "broccoli";
string d2 = "donald";
string d3 = "manbearpig";
string d4 = "marmite";
string replacement = "BLEEP";

cout << "Please enter words, press enter then Ctrl + Z when finished: \n";

//read whitespaced words into enterWords
for (string enterWords; cin >> enterWords;)
words.push_back(enterWords);
 

//sort them 
sort(words);


//loop to print all words entered, so if 5 words
//are entered, each word will be printed until
//a == 4
for (int a = 0; a < words.size(); ++a)
{
if (words[a] == d1 || words[a] == d2 || words[a] == d3 || words[a] == d4)
cout << replacement << endl;
else
cout << words[a] << endl;
}

keep_window_open();

return 0;
}

I originally tried to use another vector containing a bunch of strings however, as I tried to compare the two vectors I realised that it was a little beyond me as of yet. 

No comments:

Post a Comment