Pages

Wednesday, 29 June 2016

Chapter 3 Exercises - 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.



1. Do the try this exercises.


Self explanatory.


2. Write a program in c++ that converts from miles to kilometers. Your program should have a reasonable prompt for the user to enter a number of miles. Hint: There are 1.609 kilometers to the mile.


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

int main()
{
cout << "This program is to convert from Miles to Kilometers.\n";
cout << "Please enter miles to convert: \n";

double miles;
double kilometers;
const double mToK = 1.609;

cin >> miles;

kilometers = miles*mToK;

cout << "You entered " << miles << " mile(s) which is " << kilometers << " kilometer(s).\n";

keep_window_open();

return 0;

}



3. Write a program that doesn't do anything, but declares a number of variables with legal an illegal names (such as int double = 0;) so that you can see how the compiler reacts.



Skipping because this is something that will be different for everyone depending on what compiler you are using.


4. Write a program that prompts the user to enter two integer values. Store these values in int variables names val1 and val2. Write your program to determine the smaller, larger, sum, difference, product and ratio of these values and report them to the user.

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

int main()
{
cout << "Please enter two integer values: \n";

int val1;
int val2;

cin >> val1 >> val2;

//Largest & Smallest
if (val1 > val2)
    cout << "Largest: " << val1 << "\nSmallest: " << val2 << endl;
else cout << "Largest: " << val2 << "\nSmallest: " << val1 << endl;

//Sum
cout << "Sum: " << val1 + val2 << endl;

//Difference
if (val1 > val2)
    cout << "Difference: " << val1 - val2 << endl;
else cout << "Difference: " << val2 - val1 << endl;

//Product
cout << "Product: " << val1*val2 << endl;

//Ratio
double ratio;
double ratioDivide = 1;

if (val1 < val2)
    ratio = val1 / val2;
else ratio = val2 / val1;

ratio = ratioDivide / ratio;
cout << "Ratio: 1:" << ratio << endl;

keep_window_open();


return 0;

}



5. Modify the program above to ask the user to enter floating-point values and store them in double variables. Compare the outputs of the two programs for some inputs of your choice. Are the results the same? Should they be? What's the difference?


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

int main()
{
cout << "Please enter two floating-point values: \n";

double val1;
double val2;

cin >> val1 >> val2;

//Largest & Smallest
if (val1 > val2)
    cout << "Largest: " << val1 << "\nSmallest: " << val2 << endl;
else cout << "Largest: " << val2 << "\nSmallest: " << val1 << endl;

//Sum
cout << "Sum: " << val1 + val2 << endl;

//Difference
if (val1 > val2)
    cout << "Difference: " << val1 - val2 << endl;
else cout << "Difference: " << val2 - val1 << endl;

//Product
cout << "Product: " << val1*val2 << endl;

//Ratio
double ratio;

if (val1 < val2)
    ratio = val1 / val2;
else ratio = val2 / val1;

ratio = 1 / ratio;
cout << "Ratio: 1:" << ratio << endl;

keep_window_open();

return 0;
}



Basically ints cannot store floating point values so they will be rounded and information can be missed off.


6. Write a program that prompts the user to enter three integer values, and then outputs the values in numerical sequence separated by commas. So, if the user enters the values 10 4 6, the output should be 4, 6, 10. If two values are the same, they should be ordered together. So, the input 4 5 4 should give 4, 4, 5.

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

int main()
{
cout << "Please enter three integer values: \n";

int one;
int two;
int three;

cin >> one >> two >> three;

/*possiblities
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
*/

//If one is the smallest
if (one < two && three && two < three)
   cout << one << ", " << two << ", " << three << "." << endl;
else if (one < two && three && three < two)
   cout << one << ", " << three << ", " << two << "." << endl;
else if (one < two && three && two == three)
   cout << one << ", " << two << ", " << three << "." << endl;
else if (one == two && three) // only need to include this one once
   cout << one << ", " << two << ", " << three << "." << endl;

//If two is the smallest
if (two < one && three && one < three)
    cout << two << ", " << one << ", " << three << "." << endl;
else if (two < one && three && three < one)
    cout << one << ", " << three << ", " << two << "." << endl;
else if (two < one && three && one == three)
    cout << two << ", " << one << ", " << three << "." << endl;

//If three is the smallest
if (three < one && two && one < two)
    cout << three << ", " << one << ", " << two << "." << endl;
else if (three < one && two && two < one)
    cout << three << ", " << two << ", " << one << "." << endl;
else if (three < one && two && one == two)
    cout << three << ", " << one << ", " << two << "." << endl;

keep_window_open();

return 0;
}


There is a much easier way to do this however I think he was really trying to hammer home the If statement and make you think about every possible outcome. For example I listed the possible outcomes above and there are 6 however what if 2 of the same numbers are inputted or all numbers are the same? I could've also done nested if statements however for me I find it easier to read with the else if.
7. Do exercise 6, but with string values. So, if the user enters the values Steinbeck, Hemingway, Fitzgerald, the output should be Fitzgerald, Hemingway, Steinbeck.


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

int main()
{
cout << "Please enter three string values: \n";

string one;
string two;
string three;

cin >> one >> two >> three;

/*possiblities
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
*/

//If one is the smallest
if (one < two && one < three && two < three)
    cout << one << ", " << two << ", " << three << "." << endl;
else if (one < two && one < three && three < two)
    cout << one << ", " << three << ", " << two << "." << endl;
else if (one < two && one < three && two == three)
    cout << one << ", " << two << ", " << three << "." << endl;
else if (one == two && one == three) // only need to include this one once
    cout << one << ", " << two << ", " << three << "." << endl;

//If two is the smallest
if (two < one && two < three && one < three)
    cout << two << ", " << one << ", " << three << "." << endl;
else if (two < one && two < three && three < one)
    cout << one << ", " << three << ", " << two << "." << endl;
else if (two < one && two < three && one == three)
    cout << two << ", " << one << ", " << three << "." << endl;

//If three is the smallest
if (three < one && three < two && one < two)
    cout << three << ", " << one << ", " << two << "." << endl;
else if (three < one && three < two && two < one)
    cout << three << ", " << two << ", " << one << "." << endl;
else if (three < one && three < two && one == two)
    cout << three << ", " << one << ", " << two << "." << endl;

keep_window_open();

return 0;
}


8. Write a program to test an integer value to determine if it is odd or even. As always, make sure your output is clear and complete. In other words, don't just put yes or no. Your output should stand alone like 'The value 4 is an even number.' Hint: See the remainder (modulo) operator in section 3.4.


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

int main()
{
cout << "Please enter an integer: \n";

int value;
int divisByTwo = 2;
int equalToZero = 0;

cin >> value;

if (value % divisByTwo == equalToZero)
    cout << "The value " << value << " is an even number." << endl;
else cout << "The value " << value << " is an odd number." << endl;

keep_window_open();

return 0;
}



9. Write a program that converts spelled-out numbers such as "zero" and "two" into digits, such as 0 and 2. When the user inputs a number, the program should print out the corresponding digit. Do it for the values 0, 1 ,2 ,3 and 4 and write out 'not a number I know' if the user enters something that doesn't correspond, such as 'stupid computer!'


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

int main()
{
cout << "Please write zero, one, two, three or four: \n";

string number;

cin >> number;

if (number == "zero" || number == "Zero")
    cout << number << " is 0.\n";
else if (number == "one" || number == "One")
    cout << number << " is 1.\n";
else if (number == "two" || number == "Two")
    cout << number << " is 2.\n";
else if (number == "three" || number == "Three")
    cout << number << " is 3.\n";
else if (number == "four" || number == "Four")
    cout << number << " is 4.\n";
else cout << "Sorry, that is not a number I know. Stupid computer!\n";

keep_window_open();

return 0;
}


10. Write a program that takes an operation followed by two operands an outputs the result. For example:

+ 100 3.14

*4 5

Read the operation into a string called operation and use an if-statement to figure out which operation the user wants, for example, if (operation == "+"). Read the operands into variables of type double. Implement this for operations called +,-,*,/, plus, minus, mul, div with their obvious meanings.



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

int main()
{
string operation;
double val1;
double val2;

cout << "Please choose an operation: +,-,*,/, plus, minus, mul or div.\n";
cin >> operation;

cout << "Please enter two integers (they can be decimals): \n";
cin >> val1 >> val2;

cout << "You entered: " << operation << " " << val1 << " " << val2 << endl;

if (operation == "+" || operation == "plus")
    cout << "You want to add " << val1 << " to " << val2 << "which is " << val1 + val2 << endl;
else if(operation == "-" || operation == "minus")
    cout << "You want to minus " << val1 << " from " << val2 << "which is " << val1 - val2 << endl;
else if (operation == "*" || operation == "mul")
    cout << "You want to multiply " << val1 << " and " << val2 << "which is " << val1 * val2 << endl;
else if (operation == "/" || operation == "div")
    cout << "You want to divide " << val1 << " by " << val2 << " which is " << val1 / val2 << endl;
else cout << "Sorry, that operation is not valid.\n";

keep_window_open();

return 0;
}


11. Write a program that prompts the user to enter some number of pennies (1-cent coins), nickels (5-cent coins), dimes (10-cent coins), quarters (25-cent coins), half dollars (50-cent coins), and one-dollar coins (100-cent coins). Query the user separately for the number of each size coin e.g "How many pennies do you have?" Then your program should print out something like this:

You have 23 pennies.

You have 17 nickels.

You have 14 dimes.

You have 7 quarters.

You have 3 half dollars.

The value of all your coins is 573 cents.


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

int main()
{
//coins
int pennie;
int nickel;
int dime;
int quarter;
int halfDollar;
int oneDollar;

cout << "How many pennies do you have?\n";
cin >> pennie;

cout << "How many nickels do you have?\n";
cin >> nickel;

cout << "How many dimes do you have?\n";
cin >> dime;

cout << "How many quarters do you have?\n";
cin >> quarter;

cout << "How many half dollar coins do you have?\n";
cin >> halfDollar;

cout << "How many one dollar coins do you have?\n";
cin >> oneDollar;


//conversions
int nToP = nickel * 5;
int dToP = dime * 10;
int qToP = quarter * 25;
int hdToP = halfDollar * 50;
int odToP = oneDollar * 100;

int totalValue = (nToP + dToP + qToP + hdToP + odToP);


cout << "You have " << pennie << " pennies.\n";
cout << "You have " << nickel << " nickels.\n";
cout << "You have " << dime << " dimes.\n";
cout << "You have " << quarter << " quarters.\n";
cout << "You have " << halfDollar << " half dollars.\n";
cout << "You have " << oneDollar << " one dollars.\n";
cout << "The value of all your coins is " << totalValue << " cents.\n";

keep_window_open();

return 0;
}



11-b. Make some improvements: if only one coin is reported, make the output grammatically correct. eg, 14 dimes and 1 dime. Also, report the sum in dollars and cents, i.e, $5.73 instead of 573 cents.


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

int main()
{
//coins
int penny;
int nickel;
int dime;
int quarter;
int halfDollar;
int oneDollar;


cout << "How many pennies do you have?\n";
cin >> penny;

cout << "How many nickels do you have?\n";
cin >> nickel;

cout << "How many dimes do you have?\n";
cin >> dime;

cout << "How many quarters do you have?\n";
cin >> quarter;

cout << "How many half dollar coins do you have?\n";
cin >> halfDollar;

cout << "How many one dollar coins do you have?\n";
cin >> oneDollar;


//conversions
int nToP = nickel * 5;
int dToP = dime * 10;
int qToP = quarter * 25;
int hdToP = halfDollar * 50;
int odToP = oneDollar * 100;
int equalTo = 1;

int totalValue = (nToP + dToP + qToP + hdToP + odToP);
double tvInDollars = totalValue / 10.0;


if (penny == equalTo)
    cout << "You have " << penny << " penny.\n";
else cout << "You have " << penny << " pennies.\n";


if (nickel == equalTo)
    cout << "You have " << nickel << " nickel.\n";
else cout << "You have " << nickel << " nickels.\n";


if (dime == equalTo)
    cout << "You have " << dime << " dime.\n";
else cout << "You have " << dime << " dimes.\n";


if (quarter == equalTo)
    cout << "You have " << quarter << " quarter.\n";
else cout << "You have " << quarter << " quarters.\n";


if (halfDollar == equalTo)
    cout << "You have " << halfDollar << " half dollar coin.\n";
else cout << "You have " << halfDollar << " half dollars.\n";


if (oneDollar == equalTo)
    cout << "You have " << oneDollar << " one dollar coin.\n";
else cout << "You have " << oneDollar << " one dollars.\n";

cout << "The value of all your coins is quot; << tvInDollars << "\n";

keep_window_open();

return 0;

}

Sunday, 26 June 2016

Chapter 3 Drill - Principles & Practice Using C++

In this drill I am using an internet based IDE found here:

https://www.codechef.com/ide

So there may be some discrepancies from the book.

Drill 3.1

This drill is to write a program that produces a simple form letter based on user input. begin by typing the code from 3. prompting a user to enter his or her first name and writing "Hello, first_name" where first_name is the name entered by the user. then modify your code as follows: change the prompt to "Enter the name of the person you want to write to" and change the output to "Dear first_name,". Don't forget the comma.



In the book it says to use underscores to separate words however I started learning on a website that used capitals between words and I kind of prefer that method.

Drill 3.2

Add an introductory line or two, like "How are you? I am fine. I miss you." Be sure to indent the first line. Add a few more lines of your choosing - it's your letter.



*For those of you who are not as sophisticated as I am, those are the lyrics to the opening song from Spongebob Squarepants. 

Drill 3.3

Now prompt the user for the name of another friend, and store it in friend_name. Add a line to your letter : "Have you seen friend_name lately?".




Drill 3.4

Declare a char variable called friend_sex and initialize its value to 0. Prompt the user to enter an m if the friend is male and f is the friend is female. Assign the value entered to the variable friend_sex. Then use two if-statements to write the following:
If the friend is is male, write "If you see friend_name please as him to call me."
If the friend is female, write "If you see friend_name please ask her to call me."




Drill 3.5

Prompt the user to enter the age of the recipient and assign it to an int variable age. have your program write "I hear you just had a birthday and you are age years old!" If age is 0 or less or 110 or more call simple_error("you're kidding!") using simple_error() from std_lib_facilities.h



As you can see I didn't use the error function provided but that's because I'm currently writing this on a computer that has no IDE on it and I have no access to any of my files so I'm using a web based IDE from codechef. The simple if statement does the exact same job though.

Drill 3.6

Add this to your letter:
If your friend is under 12, write "Next year you will be age+1"
If your friend is 17, write "Next year you will be able to vote."
If your friend is over 70, write " I hope you are enjoying retirement."

Check your program to make sure it responds appropriately to each kind of value.



Drill 3.7

Add "yours sincerely," followed by two blank lines for a signature, followed by two blank lines for a signature, followed by your name.





This drill was pretty straight forward, covering everything taught in chapter 3. When I started this blog I was actually up to chapter 4 and originally thought I was doing well with the drills if they were all going to be this easy then I got to the chapter 4 drill and my head almost exploded. 

The only thing that initially confused me was all the newlines needed to keep everything readable but the more you write the more that \n becomes second nature.






Thursday, 23 June 2016

Chapter 2 Drill - Principles & Practice Using C++

This drill is extremely straight forward. I'm doing a comment on it though because I noticed that the link provided in the book for the header file is now out of date and header file it provides will not work.

After some digging (for a guy who created a programming language, his website is poorly organised with a ton of missing links. I don't know if this is done on purpose because you know, irony or not) I managed to find the correct header file which you can download from his website below:

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

I'm using Visual Studio 15 and so far I've found it to be an excellent compiler. The amount of errors it catches is amazing and I love that most of the time it will even pick up on spelling mistakes and suggest what you might have meant. 9 times out of 10 I've not even had to compile for it to bring up errors as it just underlines in the same red squiggle you would see on Word.

Wednesday, 22 June 2016

Beginnings

Hello! And welcome to a new blog I have developed purely to document my progress in programming. I am currently the owner of www.theparryeffect.com (which to be honest doesn't get updated as often as I'd like) and youtube channel www.youtube.com/theparryeffect91. Where I post Let's Plays and vlogs.

In January I purchased Programming: Principles and Practice Using C++, written by the creator of C++ Bjarne Stroustrup.

As someone who will be attending university in September 2016 studying Software Engineering and Game Development (and never having programmed a bean in my life) I decided to get cracking on learning as much as I could.

This blog will document my feeble attempts to get through the 1000+ page book as well as my progress into creating my own games.