Final Exam Review


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. 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
    }
    
  3. 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 
    
  4. 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
    
  5. 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.
    15
    * * * * * * * - - - - - - - -
    * * * * * * * - - - - - - - -
    * * * * * * * - - - - - - - -
    * * * * * * * - - - - - - - -
    * * * * * * * - - - - - - - -
    * * * * * * * - - - - - - - -
    * * * * * * * - - - - - - - -
    - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - -
    Press any key to continue . . .
    
    
    26
    * * * * * * * * * * * * * - - - - - - - - - - - - -
    * * * * * * * * * * * * * - - - - - - - - - - - - -
    * * * * * * * * * * * * * - - - - - - - - - - - - -
    * * * * * * * * * * * * * - - - - - - - - - - - - -
    * * * * * * * * * * * * * - - - - - - - - - - - - -
    * * * * * * * * * * * * * - - - - - - - - - - - - -
    * * * * * * * * * * * * * - - - - - - - - - - - - -
    * * * * * * * * * * * * * - - - - - - - - - - - - -
    * * * * * * * * * * * * * - - - - - - - - - - - - -
    * * * * * * * * * * * * * - - - - - - - - - - - - -
    * * * * * * * * * * * * * - - - - - - - - - - - - -
    * * * * * * * * * * * * * - - - - - - - - - - - - -
    * * * * * * * * * * * * * - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - -
    Press any key to continue . . .
    
  6. 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 . . .
    
  7. Make sure to fully understand the recent "array" programming assignment. Be able to write functions that create patterns, print patterns, flip patterns, etc.