COP 3223 Fall 2006 Section 1 Exam 2 Information

Total Page:16

File Type:pdf, Size:1020Kb

COP 3223 Fall 2006 Section 1 Exam 2 Information

COP 3223 Fall 2007 Section 3 Exam 2 Information

Date: October 23, 2007 Time: 1:30pm - 2:45pm Place: HPA-119

Sections of the textbook covered: 3.10-3.13, 3.16, 3.18, 4.1-4.5, 4.7-4.9, 4.12, 4.14-4.15, 5.1-5.8, 8.1-8.4, 8.9, 8.12-8.13, 13.4-13.6

Exam Format: Some multiple choice questions and functions/code segments to write. I haven't decided whether the points will be allocated 40/60, 50/50 or 60/40, but it will be one of those three ratios.

Example Questions: A previous semester's exam will be posted off my website as well as today's review questions. Both have excellent examples of questions I feel are important. However, the types of questions I will ask on Tuesday's exam are not limited by the types of questions shown in these two samples.

Note: The multiple choice section will be CLOSED BOOK. The rest of the exam (free response) will be OPEN BOOK. Neither a calculator nor notes will be allowed for use on either part of the exam. YOU MAY ONLY USE YOUR BOOK IF YOU BRING IT. Outline of Exam Material

I. Loops a. For Loop b. Danger of Infinite Loops c. Nested Loops d. break e. continue II. Functions a. How to call a function b. Pass by value parameters c. Pass by reference parameters d. The difference between actual and formal parameters e. Function prototypes f. Void functions g. Functions that return values h. How to design a function and integrate calls to it in a program III. Character Processing a. getchar b. putchar c. ascii values d. ctype.h e. EOF IV. Files a. How to declare an input file b. How to declare an output file c. How to read from an input file d. How to write to an output file For Loop

Here's the general construct for a for loop (with a block of statements): for (; ; ) { stmt1; stmt2; ... stmtn; } stmtA;

Here is how the computer executes the for statement:

1) Execute the initial statement. 2) Evaluate the boolean expression. 3) If it's true, execute statements 1 through n. 4) Do the increment statement. 5) Then go back to step #2 6) If it's false, skip over the loop body and continue execution with stmt A.

The key idea here is that a for loop can be used to run a block of code for a fixed number of iterations very easily. Furthermore, it can be advantageous to use the for loop index variable within the loop for various calculations, or even as a bound to control another loop! Functions

Here are some issues to keep straight about functions:

1) Rules for calling a function 2) Rules for writing a function 3) Layout of a program with functions 4) The difference between actual and formal parameters.

Here's a brief summary for each of these:

Rules for calling a function:

1) Must use its name 2) Must use parenthesis 3) Must place ACTUAL parameters inside the parentheses separated by commas 4) Must place the expression where an expression of the return type of the function would be expected. 5) No types are specified

Rules for writing a function

1) Return type must be specified 2) The name of the function must be specified 3) The name and type of each formal parameter must be specified 4) All variables used must either be formal parameters or local variables declared inside the function, (except globals, but we are trying to minimize our use of these!) 5) There's NO need to read in the value of, or prompt the user for the formal parameters - these ALREADY have values. 6) Must return a value/expression of the return type. 7) Mostly like writing a mini-program with a specific task. Layout of a program with functions

1) First have the #includes 2) Next, the function prototypes 3) Then the #defines 4) main 5) all the functions, one after another 6) Make sure no function is declared in another.

Difference between actual and formal parameters

1) Actual parameters are the ones in the function CALL. 2) Formal parameters are the ones in the function definition.

3) Actual parameters can be any expression of the correct type. 4) Formal parameters must be variables.

5) Actual parameters aren't written with their types in the function call. 6) Formal parameters are written with types in the function header.

7) The actual parameter doesn't really have a name in all cases because its an expression. 8) The name of a formal parameter is completely independent of any variable names in any other functions. They may be the same, but may not be the same as variables in other functions.

A function with just pass-by-value parameters can NOT change the value of a variable in another function.

Finally, make sure you can use the functions in the math library and the random number functions. Character Processing

Each character is stored internally by its ascii value. All uppercase letters are stored contiguously as are all lowercase letters and all digits. (You don't need to memorize the actual ascii values, but you do need to be able to utilize the fact that they are stored contiguously.)

When reading a stream of characters, in order to ensure that each one gets read, use the function getchar(). To place characters into a stream use the function putchar().

Also, macros in ctype.h can be helpful in processing characters. Files

To declare a FILE pointer:

FILE *ifp; FILE *ofp;

To open a file perform the following function call: ifp = fopen("filename","r"); // This is for reading... opf = fopen("filename", "w"); // This is for writing

To read from a file: fscanf(ifp, "%d", &variable); c = fgetc(ifp);

To write to a file fprintf(ofp, "Whatever I want to print %d\n", variable); fputc(c, ofp);

(Assume c is a declared as a char.) Sample Free Response Questions

1) The Caesar shift is the name given to a secret code where each letter gets substituted by the letter three "spaces" ahead of it. For example, in the Caesar shift, the letter 'A' is substituted by the letter 'D'. For letters at the end of the alphabet, we wrap around back to the beginning, thus, 'X' is substituted by 'A', 'Y' is substituted by 'B' and 'Z' is substituted by 'C'. Write a program that reads in characters from the standard input stream until EOF is encountered and prints out to the screen the same contents coded with the Caesar shift. If any non-alphabetic characters are encountered, print them out unchanged. Also, make sure to maintain the case of each coded letter. Thus, if the input letter is 'g', the corresponding output letter must be 'j' and NOT 'J'. Make sure to use getchar and putchar instead of printf and scanf. Complete the program below to perform this task:

#include int main(void) {

char c; while ((c = getchar()) != EOF) {

} return 0; } The Fibonacci sequence starts with the values 1, 1, 2, 3, 5, and 8. To get subsequent values in the sequence, you must add the two previous values. Thus, the term after 8 is 13, since 5+8 = 13. (And the term after that is 8+13 = 21.) In particular, the first number in the sequence is one, the second number is also one, the third number is two, the fourth number is three, the fifth number is five, etc. Write a function that calculates the nth Fibonacci number below. Note: In order to receive full credit for this question you MUST use a loop and you CAN NOT use recursion. int fibonacci(int n) {

int fib1=1, fib2=1, fib3, index;

return fib3; }

Recommended publications