QMCS 130

Fall 2007

Final Exam

Name:

You will have 120 minutes to complete this examination.

Write your name on the line above.

Do not begin until instructed to do so.

All answers must be written on the pages of this exam.

You may not use a calculator or other similar device.

This exam is worth one hundred (100) points.

No credit will be given for answers that do not fully and completely answer the question. The programs in this exam do not contain any syntax errors. They may contain logic errors, but do not violate any of the C language syntax rules. If you think a program contains a syntax error, come and tell me. It may be a typo.

The last page of this exam contains a short list of some of the standard C functions we have used. It may help refresh your memory.

By signing your name in this box you are requesting that your mid-semester exam not be included in the calculation of the final grade for this course. This means that the exam portion of your final grade will consist only of the points on this exam plus the Matlab exam.


Part I – Short Answers – 2 points apiece – 40 total points

One or two sentences will answer most of these questions.

1.) Give the definition of a computer.

2.) Under what circumstances can the clock speed be used to accurately compare the performance of two different CPUs?

3.) There are several factors that contribute to the delay in accessing the information on a hard drive. Which factor is most influenced by the speed at which the disk spins?


4.) When is casting required?

5.) Where is the “value” of a variable stored?

6.) Explain what the phrase “pass by value” means.

7.) What is the purpose of memory (RAM) in a computer?

8.) Give the definition of an algorithm.

9.) What are the two requirements for writing a correct while loop (as discussed in class)?

10.) When is it necessary to pass the address of a variable to a function written in C?

11.) What is an array? (Not what is an array declaration, but what is an array itself).

12.) Describe the relationship between actual parameters and formal parameters.

13.) Show all the steps that will produce the result of subtracting the binary number 001001 from the binary number 011100. Label each step.

14.) Explain why the computer uses binary numbers.

15.) Explain how letters of the alphabet are represented in the computer.

16.) Write the C struct that would model the following information for a person: last name (maximum of 30 characters), year of birth, amount owed on credit card.

17.) Write the flowchart for the sequential file processing algorithm.

18.) What value will be stored in the variable x after the following statements are executed?

x = 21;

x = (x - 3 * 4 + x / 7) % 10;

19.) Below are two logical expressions that use the variable y. For each expression, indicate what integer values of y will cause the logical expression to be true.

y < 3 & y > 5

y < 3 || y > 5

20.) How many times will a loop execute if it has this loop statement:

for(int i=0; i<21; i++)


Part II – Flowchart Conversion – 5 points apiece – 10 points total

For each of the following flowchart segments, convert the logic in the flowchart into C program statements that have the identical logic – not equivalent logic, identical logic.


Part III – Finding Logic Errors – 5 points apiece – 10 points total

Each of the following functions contains one error. Identify the error and explain how to correct it. There are no syntax errors in these programs.

The following function is passed two integer values and should return the sum of all the odd integers in that range.

int sum (int start, int end)

{

int flag;

int total;

total = 0;

while(start <= end)

{

flag = start % 2;

if(flag = 1) total = total + start;

start = start + 1;

}

return total;

}

The following function should add together the digits of a positive integer number and returns the result. For example, if the number passed to it was 1891, the function should return 19 (the result of adding: 1+8+9+1).

int sumDigits (int number)

{

int sum;

sum = 0;

while(number > 0)

{

sum = sum + (number % 10);

number = number / 10;

}

return number;

}


Part IV – Writing Functions – 10 points apiece – 40 points total

Write the function header and function body for each of the following problems.

1.) Write a function called readInteger that is passed a string prompting message. The function prints the prompting message on the monitor and processes the input typed at the keyboard. The function repeats this print-read process until the user has successfully entered an integer value. This integer value is returned by the function. Each time the user enters something that is not an integer the function will display this error message:

Data entered was not an integer. Try again.

2.) Write a function called searchArray that is passed an integer array, an integer that contains the number of elements in the array, and an integer value called searchForThis. The function will return the number of the element that contains the value searchForThis if searchForThis is found in the array and will return -1 if searchForThis is not found in the array.

3.) Write a function called sumPositive that is passed an integer array and an integer that contains the number of elements in the array. The function returns the sum of the positive values in the array.


4.) Write a function called isItSorted that is passed an integer array and an integer that contains the number of elements in the array. The function returns -1 if the values in the array are in non-ascending sorted order (i.e.., they are in high to low order), 1 if the values in the array are in non-descending sorted order (i.e., they are in low to high order), and zero if the values in the array are not in either sorted order.


Standard C Functions

Here are some of the more common standard C functions we have used. Each function is shown with the type(s) of formal parameters it expects and a comment about what, if anything, the function returns.

gets(char buffer[])

stores into buffer all characters entered from the keyboard up to the newline character and returns a pointer to buffer.

rand()

returns a pseudo random integer in the range 0 through 32767

scanf(char * formatExpression, comma separated list of addresses)

returns the integer number of items successfully read using the formatExpression

strlen(char string[])

returns the number of characters in the passed string.

tolower(char c)

returns c converted to lower case

toupper(char c)

returns c converted to upper case

QMCS130 Final Exam 11 Patrick L. Jarvis, PhD