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;

}

No comments:

Post a Comment