Friday, November 18, 2011

Exam #2 programming problems - part 2

I strongly suggest that each student develop a C++ solution to the this second set of practice problems in preparation for the coding portion of the second exam. While I do not promise that the practical part of the exam will be *exactly* the same as any one of these problems, the actual test question will at least be very similar to one or more of these practice problems.

Note:
Keep an eye on this post as additional problems are highly likely!
  1. Write a program that requests the user enter their first and last initials and swaps them without making copies. That is, the variables in main will be made available to the swapping function thereby making the swap persistent.

    You must write three void functions:

       void getInitial
       void displayInitials
       void swap


    You must complete the functions by determining what the formal parameters should be. All char variables are to be passed by reference.

    Sample Output
    Here is a screenshot of my code running.

    Initial Swapping output


  2. Write a program which reads in student test scores from a text file and averages those scores. You must allow for the possibility that not all students have taken the same number of tests to date. Each student's grades are on a separate line in the file as in this sample data:

    98.6 88.2 93.4 67.3 99 85.5
    89.2 77.4 85.6 98.2 77.1 81.9
    95 86 95.4
    78.6 75.3 65.9 84.2
    92.3 93.4 94.2 91.6 93.7 95.1 90.4
    70.2 80.6 90.4
    95 90 85 80 100


    You will need to use the peek() function to detect the newline character denoting the end of each student's grades. Be sure to have a newline at the end of the last line of data!

    You must write one function:

       void runAverages(ifstream &fin)

    Sample Output
    Here is a screenshot of my code running.

    Nick Christian output


  3. Write a program containing four versions of the function printMe: one each to handle int, double, char, and string data.

    You must write four functions:

       void printMe(int n)
       void printMe(double d)
       void printMe(char c)
       void printMe(string s)


    To show your code working, call each of your methods using the following (hard-coded) variables:

       int x = 13;
       double d = 13.37;
       char c = 'p';
       string s = "My name is Inigo Montoya";


    Sample Output
    Here is a screenshot of my code running.

    Print Me output


  4. Write a program to generate net pay for employees. Hours worked over 40 are paid at time and a half. Assume a flat-rate income tax of 28%. Declare appropriate constants. Your code should ask for 1 to continue and 0 to quit entering employee pay information (you'll need a loop spinning in main).

    You must write five functions:

       double getHourlyRate()
       double getHoursWorked()
       double calculateGrossPay(double rate, double hours)
       double calculateTax(double gross, double taxes)
       double calculateNetPay(double gross)


    Sample Output
    Here is a screenshot of my code running.

    Pay Day output