CS 2073 Engineering Programming
Topics and Review Questions for
Final Exam, Fall 2005



Date and length: Thursday, 8 December 2005, 1:30 pm - 4:15 pm.
Note: Answers are at: Selected Answers.


Reviews for past exams:


Topics: Check out the previous reviews and the calendar for material before the second exam. Material after the second exam:

You should also review your work for the programming assignments:


Sample Questions: I will pick simple questions about many of the topics from the web pages and from class. Often it will involve giving you some code and asking you to explain, modify, or complete the code. Here are sample questions.


basics:

    Write C code that will start with a certain number of seconds, and will convert to hours, minutes, and seconds, where "hours" and "minuters" are as large as possible:


if-else:

  1. Write a short if statement (no else) that will check if an int size is inside the range from 2 to 10 (inclusive) and will print "In range" if this is so.

  2. Write another short if statement (no else) that will check if an int size is not inside the range from 2 to 10 (inclusive) and will print "Out of range" if this is so.


functions:

  1. Write a function that will take double x and double y as input formal parameters. Then the function should return the quadrand number that the point(x, y) lies in (1-4). In case the point is on an axis, return 0. Try this out with inputs: (1, 2), (-1, 3), (3, -2), (-1, -4), (0, 5), (-3, 0), (0, 0).


Loops:

  1. Translate the following while loop to a for loop that does the same thing:

  2. Translate the following for loop to a while loop that does the same thing:

  3. Use a for loop to calculate 100000 terms of the following sum. Compare the result with pi*pi/6.0.

  4. Write a code segment in C that will start with an amount of money (a double amount), and a number of years (an int years), and an interest rate (a double rate). Use a loop to get the amount of money at the end of this time, applying the interest each year. Compare this amount with the amount you would have if you applied the interest 4 times a year. (You apply 0.25*rate a number times equal to 4*years.)

  5. Write a code segment that will use the following equations to approximate pi to 15 digits:

    Then replace a1 and b1 by a0 and b0, and repeat the last two equations. Keep repeating until a1 and b1 are within 1.0e-15 of one another. The final answer should be the average of a1 and b1.


Arrays:

  1. Write a C function that will take two 1-dimensional int arrays and their size as formal parameters, and will compute and return their inner product. If int a[] = {1, 2, 3}; and int b[] = {6, 7, 8};, then the inner product (a*b) is 1*6 + 2*7 + 3*8. Execute your function with the above example and with another longer example.

  2. Start with a 2-dimensional array int m[N][N], where there is a #define N 16, say. For each location m[i][j], put the number i*j into it. Then use a separate function (and pass the array as a parameter) to print the array in the form of a multiplication table, leaving off row 0 and column 0. [Oops. This was in the Review for Exam 2. The answer is there also.]

  3. Consider the declaration:

    In this case, months[1], months[2], ... are each strings that can be printed with a %s format. First use a for loop to print them.

    Now create a array of random numbers of the same size as months:

    Finally, sort the array ran into order, carrying the array months along with it. The result is to randomize the order of the months.


Pointers:

  1. For these questions you should study the examples in Pointer Page. These examples emphasize how to use the & operator (address of operator) to pass the address of a variable (pass a pointer to the variable) as an actual parameter.

    Then the formal parameter uses a * to declare the formal parameter as a pointer. Inside the code, use is often made of the * operator (dereference operator) to refer to the value that the pointer is pointing to.

    Notice the very confusing aspect: a * is used both to declare a variable as a pointer and to retrieve the value pointed to.


structs:

  1. You should especially look at the first example in: Structs and Fractions. Possible questions: I give you the definition of a struct, such as lines 2-6 in the example, and then ask you do declare several such structs (lines 12-14), how you can initialize them in two ways (line 12 and lines 15-17), how to reference fields in the structs (lines 15-17, and lines 27-29), how to pass a struct as an actual parameter (lines 20-22), and how do declare a struct as a formal parameter(line 8 and line 26).

  2. In class we covered an example that used a struct and also an array of structs: Letter Frequency Example.