Você está na página 1de 6

COP 3223 Section 4 Final Exam Name:______________________________

1. (3 Pts) Write the value of each of the following expressions: a) 10 + 24%5 b) 10 + 5*(3/4) c) 10 + (5*3)/4 _____________________ _____________________ _____________________

2. (10 Pts) The function detect_substring will return 1 if the string substring is contained in the string string. For example, detect_substring(My dog is red,red) evaluates to 1 and detect_substring(My dog is red,cat) evaluates to 0. It is casesensitive. Fill in the code to make this function operative: int find_substring(char string[], char substring[]) { int i,j; int substring_length = strlen(substring); int num_possible_substrings = strlen(string) - substring_length; for(i = 0; i < num_possible_substrings; i++) { int flag = 1; for(j = 0; j < __________________________; j++) { if(string[__________] != substring[________]) { flag = 0; break; } } if(flag) return 1; } return 0; }

3. (12 points) Write a function join_atring that takes two strings as arguments. The function takes two strings as arguments. The second string is copied at the end of the first string. (Make sure the NULL character is inserted correctly!) void join_string(char dest[], char src[]) {

} 4. (3 points) What are the pre-conditions that should be documented for this function?

5. (12 points) Write the code that prints the numbers from 1 to 100. If the number is a multiple of 3, print Fizz. If it is a multiple of 5, print Buzz. If it is a multiple of 3 and 5, print FizzBuzz. (Google FizzBuzz after the test!) #include <stdio.h> int main() {

return 0; } 6. (8 points) Write the declaration for a struct type called class_t that has three integer members: course_num, students_enrolled, and year.

7. (5 points) Assume that there is a variable, new_class, that is a struct of type class_t. Write the line of code to set the entry course_num to the value 3223.

8. (5 points) Assume that there is a pointer to a struct of type class_t called new_class. Write the line of code to set the entry course_num to the value 3223.

9. (12 points) Write the code that takes a single parameter num_rows and prints a triangle that looks like this * ** *** ***** ****** #include <stdio.h> int main() {

return 0; }

10. (12 points) Write a function that takes an array of integers as a parameter and the length of the array. The function should add one to every element.

11. (3 points) What are the preconditions that you should document to make sure that this code does not cause a problem?

12. (12 points) Write a function called powers_of_two, that takes an array as a parameter and an integer N as a parameter. The function should fill the first N elements that array with powers of 2. At index i, the value of the array will be 2i. You should define the function header. You can assume math.h is included if you need it.

13. (3 points) What are the preconditions that you should document to make sure that this code does not cause a problem

Você também pode gostar