Final Exam Review


Exam Info

  1. The final exam will be held on the date scheduled by the University. Check the date and time on the University's website. You are given two hours to complete the exam.
  2. The final exam is intended to evaluate your programming proficiency.
  3. The final exam format is programming on a computer.
  4. You will write computer programs to solve two problems. Both problems require you to write functions. If you are provided with partially complete programs, you need to use them, and you are not allowed to modify them.
  5. The difficulty level would be the same as most programming assignments you have done throughout the semester.
  6. To facilitate the programming test, the instructor may help you fix some "major" syntax or grammar errors. Students are responsible for program design, coding, and testing. For an instructor to be able to help other students during the exam, a student should not "demand" too much time from the instructor.
  7. The final exam is close-book and note.
  8. Access to mobile devices and the use of the web and AI tools are not allowed during the exam. Texas Wesleyan's Academic Integrity rules should be followed. Any violation will result in a score of zero for the final exam.

Topics Needed for Final Exam

  1. Scope of a variable. Automatic variable vs static variable. Variable name hiding.
  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 the 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 problems on the final exam.

  1. Review all the programming assignments you did and test review questions.
  2. Calculate the sum of a series: \[ S = 1+x+\dfrac{x^2}{2!}+\dfrac{x^3}{3!}+\dfrac{x^4}{4!}+ \dots + \dfrac{x^n}{n!} \] where \(n! = n(n-1)(n-2) \dots 1\) \[ S = x-\dfrac{x^3}{3!}+\dfrac{x^5}{5!}-\dfrac{x^7}{7!}+ \dots + \dfrac{x^n}{n!} \]
  3. A fair coin is tossed 4, 10, 100, and 10000 times in four experiments. Write a C++ program to record how many times the head is seen in each experiment. What do the outcomes tell you?
  4. Write a 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 (on average) do the two dice need to be rolled to see the sum equal to 8.
    (1) Use the 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 reasonably 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.
  5. /* 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 */
    
  6. Write a C++ function to find the solution to a quadratic equation:
    \[ax^2+bx+c = 0\]

    using the formula to find the two roots of the equation: \[x_{1,2} = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}\] using the formula to find the coordinates of the vertex \((h, k)\): \[ h = -\frac{b}{2a}\] \[ k = f(h) = ah^2+bh+c\]


    The C++ function prototype should look like:
    double findSolnQuadraticEqn(double a, double b, double c)
    {
    //a, b, and c are the constants of a quadratic equation
    //This C++ function will check if there is any real solution to the equation. 
    //If yes, it returns the first root. Otherwise, it prints out a message that
    //states there is no real solution and still returns a special number, say, 999.
    }
    
  7. 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 
    
  8. 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
    
    Read this handouts for the equations.
    //38
    //19
    //0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
    //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 1 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 1 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 1 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 1 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 1 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 1 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 1 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 1 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 1 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 1 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 1 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 1 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 1 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 1 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 1 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 1 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 1 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 1 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
    //1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    //Press any key to continue . . .
    
    //19 38
    //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 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0 0 0 0 0 0 0 0 0 0
    //0 0 0 0 0 0 0 0 0 0 1 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 1 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 1 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 1 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 1 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
    //1 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
    //Press any key to continue . . .
    
  9. Write program to print out 1s along a 45 degree line passing through a pre-specified point (m, n). Read this handouts.
    Enter the size of a square and the coordinates of a point:
    20 7 14
    0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1
    0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0
    0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0
    0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0
    0 0 0 0 0 0 0 0 0 1 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 0 0 0
    0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    Press any key to continue . . .
    
  10. 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 . . .
    
  11. Write a program using loops to print out counting numbers (1, 2, 3, ...) in a triangle pattern with the height chosen by the user (the height < 13).
    Enter the height of the triangle:
    6
      1
      2  3
      4  5  6
      7  8  9 10
     11 12 13 14 15
     16 17 18 19 20 21
    Press any key to continue . . .
    
    Enter the height of the triangle:
    12
      1
      2  3
      4  5  6
      7  8  9 10
     11 12 13 14 15
     16 17 18 19 20 21
     22 23 24 25 26 27 28
     29 30 31 32 33 34 35 36
     37 38 39 40 41 42 43 44 45
     46 47 48 49 50 51 52 53 54 55
     56 57 58 59 60 61 62 63 64 65 66
     67 68 69 70 71 72 73 74 75 76 77 78
    Press any key to continue . . .
    
  12. 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 . . .
    
  13. Sum all the integer numbers stored in a file without knowing the contents of the file and find the largest integer in the file.
  14. Given a data file with integers generated by the C++ random function, find the largest, second largest, and smallest integers in the file, and calculate the mean, sum, and standard deviation of the integers. A sample code below shows how to randomly generate a list of integers and save them in a data file. Another function reads the integers from the file and calculates the statistics of the integers.
    void randomDatasetGenerator(ofstream & out, int size);
    void print(ifstream & in);
    int main() {
      ofstream outData;
    
      outData.open("output.txt");
      randomDatasetGenerator(outData, 100);
      outData.close();
    
      ifstream inData;
      inData.open("output.txt");
      print(inData);
      inData.close();
    
      inData.open("output.txt");
      print(inData);
      inData.close();
    
      return 0;
    }
    //The function randomly genereates a set of integer between 0 and 100 and
    //writes the integers into a file that has been opened by the variable "out"
    //The variable "size" is the number of the integers to be generated
    void randomDatasetGenerator(ofstream & out, int size)
    {
    /* Set a seed for random number generator using computer clock time */
      srand(time(NULL));
      int i = 0;
      while (i < size)
      {
        out << rand() % 100 << " "; //Genereate a random integer between 0 and 100
        i++;
      }
    }
    
    void print(ifstream & in)
    {
      int i = 1;
      int n;
      while (in >> n)
      {
        cout << setw(3) << n << " ";
        if (i % 25 == 0)
          cout << endl;
        i++;
      }
    }
    
    	

Study Tips

  1. Practice, practice, and practice.
  2. You are encouraged to review all the assignments and previous tests.
  3. Memorizing "facts" from textbooks or lectures is necessary for learning, but understanding, critical thinking, and problem-solving are more important in learning.
  4. Reading textbooks helps you understand the subjects.
  5. Group study would work well if you study before participating.
  6. Last but not least. Learn to ask for help whenever you need.
"You can ask a question and look stupid, or not ask a question and be stupid." --Anonymous