Checking Conditionals

  1. Heavy Meta: Programs that write programs

    The compiler is a program that reads C++ source code and produces an architecture-specific executable program.

    Consider the program hellogen.cpp which is reproduced below. This program produces as output text that is a legal C++ program.

    #include <iostream> using namespace std; void makeIncludes () { cout << "#include <iostream>" << endl; cout << "using namespace std;" << endl; } void makeMain () { cout << "int main ()" << endl; cout << "{" << endl; cout << " cout << \"Hello World\" << endl;" << endl; cout << " return 0;" << endl; cout << "}" << endl; } int main () { makeIncludes(); makeMain(); return 0; }

    When executed, this program produces as output legal C++ program shown below.

    #include <iostream> using namespace std; int main() { cout << "Hello World" << endl; return 0; }

    You should modify the program above so that instead of printing Hello World it prompts the user for a name and then writes a program that greets that particular person instead of the world. You should indicate what functions change (e.g., main) and what the changes are.

    See the sample output below where the user-entered input is in italics.

       prompt% hellogen
       enter first name: Oogy
    
       #include <iostream>
       using namespace std;
       int main()
       {
           cout << "Hello Oogy" << endl;
           return 0;
       }
    Note: the output of running hellogen is a program. It is a program that would say hello to name if compiled and executed.

     

  2. Parity

    Write a function checkParity that given a positive number, returns true if the number is even and false if it is odd (hint: if there is no remainder when divided by two, the number is even, use the modulus operator %). Write a program that uses your function by prompting the user to enter a positive integer. If the integer is negative an error message should be printed, otherwise print whether the number is odd or even based on the result returned by calling your function checkParity. Sample output is shown below.
     prompt> checkint
     enter positive int: -3
     -3 is not positive, sorry
     prompt> enter positive int: 15
     15 is odd
     prompt> enter positive int: 24
     24 is even

     

  3. Who is the best?

    Complete the function printTopTeam whose header is given below. printTopTeam is passed the name, wins and losses for two teams, and returns a string corresponding to the better team (or "TIE" if both teams are equally good). Which team is better is defined as follows: The team with the fewest losses is the better team. If the number of losses is the same, then the team with the most wins is the better team. If the wins and losses are both the same then the teams are tied.

    For example, the call printTopTeam("UNC", 8, 1, "Duke", 9, 0) should return "Duke". The call printTopTeam( "FSU", 4, 5, "WFU", 3, 5) should return "FSU". The call printTopTeam("NCSU", 3, 6, "Clemson", 3, 6) should return "TIED".

    Complete the function printTopTeam given the header below.

    string printTopTeam(string team1, int win1, int loss1, 
                        string team2, int win2, int loss2)
    // post: returns which team is better, or "TIED" if teams are tied.		 
    

     

  4. Three In Order

    Write a function sortThree that has three string parameters and prints the strings in alphabetical order, one per line. For example, the call SortThree("pig", "horse", "cow") should print
        cow
        horse
        pig
    This should also be the output no matter the order in which the same three strings are passed to the function. Try to write the function so that it is easy to reason about (being correct).

     

  5. Converting Numbers to Words

    First compile and run the program numtoword.cpp to figure out the purpose of the program. After running the program you will modify it so that the program works on three and four digit numbers. For extra practice you can make it work for five digit numbers.

    Here is one plan for making the program work for three digit numbers. You are welcome to follow a different plan, but this one is simple and requires minimal new programming.

    1. First rename the function numToString to twoDigitsToString since the function only works correctly for numbers that are one or two digits (see its precondition).
    2. Write a new function named numToString that returns a string. This function is called from main and the body of the new numToString looks something like what follows.
       string numToString (int number)
       {
           if (number < 100)
           {
               return twoDigitsToString(number);
           }
           else if (number < 1000)
           {
               int hundreds = number/100;
               string first = digitToString(hundreds);
               // fill in code here
           }
       }
      The idea is to break a three digit number into two pieces, the hundreds and the rest. You can already convert the rest using twoDigitsToString. You can convert the hundreds part using digitToString as shown above (note that digitToString's precondition will be satisfied in the code shown above).
    3. To convert a four digit number, break the number into two pieces, the thousands the the three digit number you already know how to convert to a string. Try to minimize the amount of duplicated code in your new program that works for four digits.
    4. Modify the program to translate numbers until the user types in a number that is not in the valid range. This modification should be done only in the main function. Sample output might be:
        enter number between 0 and 99 (inclusive):  56
        56 = fifty-six
        enter number between 0 and 99 (inclusive):  0
        0 = zero
        enter number between 0 and 99 (inclusive):  12
        12 = twelve
        enter number between 0 and 99 (inclusive):  203
        Thanks for using our number to English translator.