Final Exam Review


Exam Info

  1. As mentioned in class, the final exam consits of two parts: programming on computer and test on paper.
  2. For the programming on computer, you will write computer programs to solve two problems. The level of difficulty would be the same as that of most lab and programming assignments you have done. To facilitate the programming test, the instructor may help you fix some "major" syntax or grammar errors if you really need. Students are responsible for program design, coding and testing.
  3. You can use paper books and notes during the final exam.
  4. Texting and web surfing are not allowed during the exam. Texas Wesleyan's Academic Integrity rules should be followed.

Topics To Be Covered

  1. Arrays
  2. Functions: basic function structures (function heading/prototype and body), how to define and call to functions, know the difference between passing-by-value and passing-by-difference.
  3. Loops: while, do-while, and for loops. Be able to use loops to do: read and/or write data from/to files, print out two-dimensional patterns (nested loops), find sum of multiple numbers, and find the largest or smallest number from a list of numbers.
  4. Switch statement. Be familiar with the structure of switch statement and programming techniques using switch such as code sharing by multiple cases (or follow through multiple cases), and default case.
  5. Selection control structures. Boolean expressions (relational and logic), precedences of relational (==, !=, >, <, <=, >=) and logic (&&, ||, !) operators. if statement without else, if-else, and nested if-else.
  6. Input and output: cin, cin.get(char), cin.ignore(int, char), getline(inputstream, string), cout, ifstream, ofstream.
  7. Strings. string data type and its built-in functions such as length, substr, getline and find.
  8. C++ library functions such as sqrt, pow, abs, etc.
  9. Output formatting. setw, setprecision, showpoint, fixed, etc.

Practice Programming Problems

Working on the following problems will help you prepare for the programming part of the final exam.

  1. Review all the programming assignments you did, especially the ones related to arrays
  2. A fair coin is tossed 4, 10, 100, and 10000 times in four different experiments, respectively. Write a C++ program to record many times the head is seen in each experiment. What do the outcomes tell you?
  3. Write C++ program to solve the following problem.
    Two fair dice are rolled together repeatedly until the sum of the numbers on the top of the two dice is equal to 8. How many times (in average) do the two dice need to be rolled to see the sum equal to 8.
    (1) Use analytical method (probability) to calculate the average times of rolls.
    (2) Write C++ programs to simulate die rolling. The outcome of a roll can be simulated by a random number (1-6) generated by the C++ function rand(). To obtain a fairly accurate estimate, you need to conduct 10,000 experiments. Computers will do the work for you. In each experiment, record the number of rolls required for a sum of 8. Upon completion, calculate the average number of rolls of all the experiments.
  4. /* Generate random numbers */
    /* Set a seed for random number generator using computer clock time */
    srand(time(NULL));
    
    int n;
    /* rand() generates a random number between 0 and MAX_RAND */
    n = rand(); 
    
    /* Use modulus operation to set a range */
    n = rand()%6 + 1; /* generate numbers between 1 and 6 inclusive */
    
  5. Write a C++ function to find the solution to a quadratic equation:



    using the formula to find the first of the two roots of the equation:



    The C++ function prototype should look like:
    //Precondition: there are real roots to the quadratic equation specified by a, b, and c
    double findSolnQuadraticEqn(double a, double b, double c)
    {
    //a, b, and c are the constants of a quadratic equation
    }
    
  6. Write a function that prints a triangle with the user's choice of size and symbol and write a main function to test your function. Hint: use nested loops.
    void printTriangle(int size, char symbol)
    
    For example:
    int main()
    {
       printTrangle(10, '0');
       return 0;
    }
    
    0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0
    0 0 0 0 0
    0 0 0 0
    0 0 0
    0 0
    0 
    
  7. Write a program using nested loop to print out a $n$ x $n$ matrix with 1s on diagonal and zeros in the rest.
    0 0 0 0 0 0 0 0 0 1
    0 0 0 0 0 0 0 0 1 0
    0 0 0 0 0 0 0 1 0 0
    0 0 0 0 0 0 1 0 0 0
    0 0 0 0 0 1 0 0 0 0
    0 0 0 0 1 0 0 0 0 0
    0 0 0 1 0 0 0 0 0 0
    0 0 1 0 0 0 0 0 0 0
    0 1 0 0 0 0 0 0 0 0
    1 0 0 0 0 0 0 0 0 0
    
  8. Write a function that prints a flag with the user's choice of size and write a main function to test your function. Hint: use nested loops. (5) A flag.
    15
    * * * * * * * - - - - - - - -
    * * * * * * * - - - - - - - -
    * * * * * * * - - - - - - - -
    * * * * * * * - - - - - - - -
    * * * * * * * - - - - - - - -
    * * * * * * * - - - - - - - -
    * * * * * * * - - - - - - - -
    - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - -
    Press any key to continue . . .
    
    
    26
    * * * * * * * * * * * * * - - - - - - - - - - - - -
    * * * * * * * * * * * * * - - - - - - - - - - - - -
    * * * * * * * * * * * * * - - - - - - - - - - - - -
    * * * * * * * * * * * * * - - - - - - - - - - - - -
    * * * * * * * * * * * * * - - - - - - - - - - - - -
    * * * * * * * * * * * * * - - - - - - - - - - - - -
    * * * * * * * * * * * * * - - - - - - - - - - - - -
    * * * * * * * * * * * * * - - - - - - - - - - - - -
    * * * * * * * * * * * * * - - - - - - - - - - - - -
    * * * * * * * * * * * * * - - - - - - - - - - - - -
    * * * * * * * * * * * * * - - - - - - - - - - - - -
    * * * * * * * * * * * * * - - - - - - - - - - - - -
    * * * * * * * * * * * * * - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - -
    Press any key to continue . . .
    
  9. Sum all the integer numbers stored in a file without knowing the contents in the file and find the largest integer in the file.
  10. Write a program to generate a sequence of Fibonacci numbers using loop. The sequence pattern is like: 0, 1, 1, 2, 3, 5, 8, 13, 21, ... Also calculate the ratios of two successive numbers. The ratios approach a constant called the Golden Ratio (1.618...) when the sequence is very long. For example,
    Enter the length of Fibonacci sequence numbers:
    12
    Number     Ratio
         1      1
         1      1
         2      2
         3    1.5
         5  1.667
         8    1.6
        13  1.625
        21  1.615
        34  1.619
        55  1.618
        89  1.618
       144  1.618
       233  1.618
       377  1.618
    Press any key to continue . . .