Homework 4 Solution

1.  Complete the following table. The first three rows are already filled in.

Expression

Type

Value

1 / 2

int

0   (integer division,  / returns the number of 2s that can be taken out of 1)

1 % 2

int

  1   (% returns the remainder after taking as many 2s out of 1 as you can)

1.0 / 2

double

  0.5   (“normal” floating point division due to 1.0)

 5 + 7 / 2

int

=5 + 3 = 8

"Beat" + ' ' + "Army"

error

(Error -- see below)

6 + 13 / 5 - 35 % 3

int

= 6 + 2 - 2 = 6

3.5 * (5/4)

double

= 3.5 * 1 = 3.5

(3.5 * 5)/4

double

= 17.5 / 4 = 4.375

*(A subtle point -- no deduction for this homework) In C++ you can "add" variables of type string. However, string literals(things like "Beat" and "Army") are not full-fledged strings, and they can't be combined with the + operator. If you really need to do this, assign the literals to a variable first. e.g.
string s1 = "Beat";
string s2 = "Army";
cout << s1 + s2 << endl;

 
 
2. Write a flowchart (or pseudo-code) that reads in two lengths in the format x' y" (i.e. x feet y inches) and returns the difference in length between the two in the same format. You may assume that the first is always larger than the second! See hint 1 and hint 2 below. For example, if the user enters 32' 6" and 15' 11" the result should be 16' 7"
 
Pseudo-code can have varying levels of detail.  Here is one example that is fairly “high-level” (not too detailed).  Note though that we give formulas for the key computations.
 
1.    Read feet1 and inches1 from user
2.    Read feet2 and inches2 from user
3.    Compute total1 = feet1 * 12 + inches1
4.    Compute total2 = feet2 * 12 + inches2
5.    Compute difference between total1 and total2 (in inches)
6.    Convert total inches to feet and inches
a.    feet   = total / 12
b.    inches = total % 12
7.    Output results 
 

 

 

3. Write a C++ program for the problem in question 2.  See hint 3 and hint 4 below. A typical run of your program should look like this (user input shown in red):

     Enter two lengths in feet and inches (larger first!)
     32' 6"
     15' 11"

          Difference is 16' 7"

 

 

Turn In (all stapled together with your name and alpha code on it):

1.      A copy of this homework assignment with Table 1 filled in.

2.      A screen capture of your flowchart from question 2.

3.      A printout of your source code from question 3.

4.      A screen capture of your program from question 3 running on the example input from question 3.

Solution

// DMN
// Homework 4
// Converts feet and inches to inches
// It requests two inputs and finds the difference between them


#include <iostream>
using namespace std;

int main()
{
    // Keep prof happy by telling prof who I am.
    cout << "This program was written by D.M. Needham, alpha code 835491." << endl << endl;

    // Get data. Don't forget to prompt.
    int first_feet, first_inches, second_feet, second_inches;
    char junk;
    cout << "Enter two lengths in feet and inches (larger first!)" << endl;
    cin >> first_feet >> junk >> first_inches >> junk;
    cin >> second_feet >> junk >> second_inches >> junk;

    // Convert to all inches
    int first_all_inches, second_all_inches;
    first_all_inches = first_feet * 12 + first_inches;
    second_all_inches = second_feet * 12 + second_inches;

    // Find the difference
    int difference;
    difference = first_all_inches - second_all_inches;

    // convert difference to feet and inches
    int final_feet, final_inches;
    final_feet = difference / 12;
    final_inches = difference % 12;

    // print the output
    cout << "Difference is " << final_feet << "' "<< final_inches << "\"" << endl;

    return 0;
}