Midterm Examination Review

(Last Mod: 27 November 2010 21:38:37 )

ECE-1021 Home


Bookmarks:


Topics Covered

A Review of the Course Syllabus shows that the following topics are fair game for the exam:

Binary representation of integers and floating point values.

ASCII representation of characters and strings.

Constants and Variables

Arithmetic, Logical, Relational, and Bitwise Operators

Assignment Statements and Abbreviated Assignment Statements

Simple I/O using printf() and scanf()

Use of math functions

Selection Structures - if(), if()/else, switch(), conditional expressions ( ()?: )

Repetition Structures - while(), do/while(), for()

Pseudocode and Flowcharting

Text Files and File Operations

Basic Programmer Defined Functions

Random Number Generation

Recursion

Basic Preprocessor Directives (#include and #define)

String Functions

Arrays (1D and 2D)

Exam Format and Guidelines

The exam will consist of a number of multiple choice or short answer questions followed by a few simple problem solving questions. As per the Course Policies, one fourth of the exam (in terms of points) will come from the odd numbered end-of-section exercises in the text verbatim, one fourth of the exam will come from the even numbered end-of-section exercises in the text verbatim, and one fourth of the exam will be taken from Quiz Prep Sheets and this Review Sheet verbatim.  

Flowcharts/Pseudocode

You may be asked to write a flowchart to solve a particular task or write a flow chart that represents a particular fragment of code. Conversely, you may be asked to write a code fragment that implements a particular flowchart fragment. You may be asked to examine a flowchart or code fragment and evaluate what result it would produce. For the purposes of the exam, flowcharts and pseudocode are NOT interchangeable. If asked for pseudocode, you are to write pseudocode. If asked for a flowchart, you are to draw a flowchart. If the option is yours, that will be made clear in the problem statement.

Default Variable Types

Remember the basic rules about assumed variable types for single-letter and similar variable names (assumed by humans - not by the compiler, which always requires explicit type declarations). If the name starts with a letter from the end of the alphabet, it is a double. If it begins with a letter from the middle of the alphabet it is assumed to be an integer. In particular, if you use {u,v,w,x,y,z} in your work and don't indicate otherwise, we will assume it to be a double. If you use {i,j,k,l,m,n} and don't indicate type, we will assume it is an integer. Other than those twelve letters - you MUST tell us what type of variable it is. If you use one type, say an 'x1', and state that it is an integer then we will grade the problem as though it is an integer but may still take off minor points for style violations (unless the use of x1 - as opposed to k1 - is reasonable for the problem even though x1 is an integer)..

Statements, Code Fragments, and Complete Code

For many problems, you will be expected to write a statement, a code fragment, or a complete program/function. These are just differing levels of completeness. This will be unambiguously stated - you will be asked to "write a C statement that....", or "write a code fragment that...." or "write a complete function to...." or "write a complete C program that...." Be sure you understand the difference otherwise you will lose points for not providing complete code when it was asked for or waste time writing complete code when it wasn't asked for.

Do NOT spend the time writing a complete program unless specifically asked to do so. Keep in mind that the problems were written with the intention that they could be completely answered within about five minutes - ten at the most. Likewise, don't spend time writing a code fragment if the only thing that was asked for was a statement. A "statement" is a single line of code, although if you need a couple of lines to answer the question, that is fine.

Always: Regardless of which level of completeness you are asked for, you must always do the following (or lose points). 

Statement: Just that - a single statement. You may write more than one statement (i.e., break it up) depending on how you YOU implement it. You don't need to write anything beyond the statement (other than the items you always need to include - see above). 

For example, if asked to write a C statement that computes the base b logarithm of x, you might write:

#include <math.h> // log()

double x, y, b;

get x, b

 

y = log(x)/log(b);

Notice that we did not include 'y' in the "get" line. If we had, we would have been saying that 'y' needs to have a meaningful value in it and, since that value would have been lost when the last line was executed, you would have lost points because you obviously felt that you needed a piece of information that you then proceeded to throw away.

Notice also that the statement that actually answers the question is set off from the others (and we recommend you underline it). Also, it is the only one that has to be a statement with proper syntax. It is to your advantage to use proper syntax on all other lines that happen to be actual C statement, but the only one you will lose points for syntactical problems are the actual code statements that are asked for.

Fragment: A code fragment is an incomplete program/function containing only the information necessary to answer the question. In most respects, asking for a fragment as opposed to a statement merely means that the code is more complex than can reasonably be done in a single statement  Therefore the same basic guidelines apply that did for statements.

For example, if asked to write a code fragment that computes the sum of the square roots of all the integers from one through ten, the following would be sufficient:

#include <math.h> // sqrt()
double sum;

sum = 0;

 

for(i = 1; i<= 10; i++)

{

    sum += sqrt( (double) i);

}

Notice, in particular, that we didn't use scanf() to get anything from the user and we didn't use printf() to print anything out. We weren't asked to do that. We were asked to compute a particular sum and that's what we did. Don't waste time writing a bunch of unasked for code.

Notice also that no main() declaration was included. It certainly could have, and doing so would allow you to indicate where the various statements in the fragment go relative to the function declaration, but you won't lose any points for not including them. What we are looking for is: do you know what steps need to be performed and what information you need in order to perform them? 

Complete: Just that - complete. We should be able to type in exactly what you have written and then compile and run it (unless you are asked for a complete function in which case it is understood that we would need to provide a main() function to drive it. This is an example of where just indicating what header file is needed is not sufficient - you must include the correct #include directive. 

For example, if asked to write a complete function called log_b() that takes two floating point arguments (of type double) and returns a floating point value (of type double) such that the return value is the logarithm of the first argument to the base of the second argument, you would need to write something like:

#include <math.h> // log()

 

double log_b(double x, double b)

{

    return( log(x) / (log(b) );

}

Notice that we did not include any main(), we did not go ask the user for any values, we did not print out any thing. None of that was asked for. You won't lose points if you do, but you won't get extra points and it's a waste of time to write a big main() function that uses this function unless you are asked to do so. Now, if you go and include extra code like that within the function you were asked to write, you WILL lose points. The problem statement for the function said nothing about the function asking the user for input or printing anything out and you don't have the freedom to put this in there, just as the person that wrote the log() function didn't have that option.

Notice also that we named this function log_b(). This is for a simple reason - that's what we were told to name it. We do not have the option to choose to name it something else.

Reference Sheet (taken from the Course Policies page)

Reference information will be provided on the course website and this same information will be provided, in largely the same format, as part of the exam packet.. This Reference Sheet consists of the following items:

If, during the exam, you feel that you need a function not on the sheet merely ask the proctor and the prototype for the function will be written on the board. 

Crib Sheet

You are allowed to write anything you desire one side of a (8.5" x 11") sheet of paper. This can contain whatever information you desire up to and including complete solutions for the problems on the Review Sheet. Be advised, this latter approach is not recommended and is likely to backfire. Your goal should be to be able to solve any of the problems on the Review Sheet without any information other than what is on the basic Reference Sheet. If you can do that, you will do fine on the exam.

Questions during exam

You may ask any question you would like during the exam - but we are not obligated to answer it. In general, we will try to clear up confusion about what a question is asking for unless the confusion appears to be directly related to what the question is getting at. For instance, if asked to compute the base b logarithm of x, we will not try to help you understand what is meant by the base b logarithm of x. Likewise, you are expected to know the syntax associated with all of the C statements and structures we have covered - questions about these will not be entertained.


Review Questions

Below are a number of possible questions. It is guaranteed that questions totaling a minimum of 25% of the points on the exam will be selected from either this list or the Quiz Prep Sheets verbatim. Be forewarned, some of these questions are not trivial. It may take you quite a while to answer them the first time. However, it is your own fault if the exam is the first time that you try to answer them.

(1) Two points define a straight line. One standard formula for such a line is y=mx+b where x and y are the variables and m and b are the parameters. However, for lines with nearly vertical slope, it is sometimes preferred to invert this expression and write it as x=ny+a. Write a code fragment that takes two points, (x1,y1) and (x2,y2) and determines the corresponding m and b or the corresponding n and a depending on the slope of the line. Set a flag (named verticalflag) to a true value if the angle of the line is within 10 degree of vertical. Assume that the coordinates of the points are of type double and have already been entered. Likewise, assume that all your code fragment has to do is set the already declared variables to the appropriate values. You only need to set the pair of variables corresponding to the value of verticalflag - the user is expected to consider the other pair undefined.

(2) The sine function, sin(x), may be approximated by the following power series:

    sin(x) = x - (x^3)/6  for sufficiently small values of x (it is accurate to within one part per million for |x| < 0.1).

Write a complete function called sinc(x) that returns a double such that:

    sinc(x) = sin(x) / x

Use the above approximation to avoid problems in the vicinity of zero.

(3) Given two points (x1,y1) and (x2,y2), write a code fragment that computes the y value corresponding to an x value (already entered by the user) assuming a linear relationship between y and x.

(4) Given two positive floating point values x and y, write a code fragment that outputs x^y. You may NOT use the pow() function.

(5) Given a positive floating point value x, write a complete function that outputs the logarithm of x to the base b (where b is a positive floating point value).

(6) What is the output of the following program:

int main()
{
  int i;
  for(i=0; i<5; i++);
  {
  printf("%i\n",i);
  }
return(1);
}

(7) What is the value of x at the end of the execution of the following code fragment:

int main()
{
  double x,y,z;
  ....
  x = 2.0;
  y = 8.0;
  z = x+y/((y>x)?(x):(y));

(8) Draw a flowchart for the following while loop

int main()
{
  int i;
  ....
  while(i++<42)
  {
  printf("%i\n",i);
  }

}

(9) How should two doubles be compared for equality? Write a code fragment that performs this check without using any functions from the math library.

(11) Each member of the Fibonacci Sequence is obtained by adding the two prior members. The first two members are defined as themselves. Therefore, the sequence looks like this: {1,2,3,5,8,13,21,34,....}. Write a complete C function that takes an integer n and that uses recursion to return an int that is the nth member of the Fibonacci Sequence. So fib(3) should return 3 while fib(7) should return 21. What is the largest value of n such that fib(n) can be represented as a 16-bit signed integer. 

(11) Each member of the Fibonacci Sequence is obtained by adding the two prior members. The first two members are defined as themselves. Therefore, the sequence looks like this: {1,2,3,5,8,13,21,34,....}. Write a complete C function that takes an integer n and that does not use recursion to return an int that is the nth member of the Fibonacci Sequence. So fib(3) should return 3 while fib(7) should return 21. What is the largest value of n such that fib(n) can be represented as a 16-bit signed integer. 

(12) Write a complete C function if type int that accepts an integer k and returns k! (k factorial).

(13) What are some of the ramifications of not checking whether a file open operation was successful?

(14) Write a complete C program that prints "Hello World!" to a text file called world.txt in the current directory.

(15) Write a complete C program that implements the following flowchart. What is the value of i and j upon completion?

(16) What, if anything, is wrong with the following code fragment? What are the likely ramifications?

FILE *datfile; datfile = fopen("A:\myfile.dat", "wt"); if(datfile=NULL)   exit(-1); fprintf(datfile, "My output file.\n");

(17) The series expansion for the sine function is:

sin(x) ~= x - (x^3/3!) + (x^5/5!) - (x^7/7!) + (x^9/9!) - .......

Write a flowchart for a code fragment that uses the above series (just the five terms shown) to calculate the sine of x.

Write a function called my_sin() that uses the above code fragment to return sin(x).

(18) Write a complete C program that generates a table of trigonometric values from 0 to 90 degrees in increments of 0.1 degree. The table is to contain the following columns:

deg sine cosine tangent

The trig values should have four places after the decimal point. There should be several blank lines every ten degrees and the above header should appear at the top of every group of ten degrees.

In other words, the output should resemble the following:

deg sine cosine tangent 0.0 0.0000 1.0000 0.0000 .... 9.9 0.1719 0.9851 0.1745   deg sine cosine tangent 10.0 0.1736 0.9848 0.1763

(19) The figure below represents a rectangular window with a circular arc above it. Write a code fragment that uses W and H (previously entered and of type double) to compute the radius of the arc (to aid in laying out the arc on the wall), the arclength of the curved upper edge (to determine how much trim is needed) and the area of the arc (to use in heat-loss calculations). The units of H and W are consistent (i.e., the same).

(EXTRA CREDIT) This WILL be the extra credit (ten points) problem on the midterm.

For large values of n, the factorial of n can be closely approximated by the following equation:

For instance, for n = 10 the above equation, known as the Stirling Formula, yields a value that is within 1% of the correct answer.

(8 pts) Write a complete C function called fact_digits() that takes an integer n as the argument and returns a floating point (double) value that is the number of digits in n!. Your function needs to be able to accept values of n for which n! would be far to large to accommodate even in a variable of long double. For instance, the user might wish to use your function to discover what the first value of n! is that has more than one trillion (1012) digits. A long double, on most compilers, can't even represent a number with 15,000 digits. Hint: How many digits are in 12345? What is the base ten log of 12345?

(2 pts) Within 1%, what IS the first value of n for which n! has more than a trillion digits. Hint: You can do this reasonably quickly on your calculator - but be sure to be finished with the rest of the exam first. Show your work for full credit.