Sample questions for Midterm 2: 1. Define the following terms: infinite loop backtracking base case (for recursion) recursive case big O best case analysis average case analysis worst case analysis 2. Translate the following for loop into a do-while loop: for (I = 0, tot = 0, cnt = 0; I < N; I += 2) { tot += I; cnt++; printf("%.2f\n",(float) tot / cnt); } 3. Write a piece of code to produce the following output using at most one newline character in your printf statements: * ** *** **** *** ** * 4. What is the output of the following piece of code: for (I = 0; I < 12; I++) { for (J = 4 - (I % 4); J > 0; J--) printf(" "); for (J = 0; J < (I % 4) + 1; J++) printf("X"); printf("\n"); } 5. Give a piece of code that will copy the characters from a text file file1.txt to a new file file2.txt. Furthermore, when copying the characters, any lower case characters in file1.txt should be written as a corresponding upper case character in file2.txt (if the character 'a' is read from file1.txt, the character 'A' should be written to file2.txt). 6. How do the fgetc and ungetc functions work? Indicate the arguments each takes and what values they return. Also, give an example of how each function is used. 7. Give code for: Sequential search of an array Sequential search of a sorted array Binary search of a sorted array Selection sort Insertion sort Merge of two adjacent segments of an array Mergesort given a predefined merge routine 8. Write a function that takes an array of characters S of length N and returns 1 if that array contains a palindrome and 0 otherwise. A palindrome is a sequence of characters that is the same written backwards. 9. Write a recursive solution to the previous problem. 10. Write a recursive function that sums up the squares of all the numbers from 1 to N (i.e., SumSqrs(N) returns 1^2 + 2^2 + 3^2 + ... + N^2). 11. Given a 2D character array A: 0 1 2 3 0 e e X X 1 X e e e 2 X e X X 3 e e e X and the function Visit: void Visit(char A[][4], int r, int c) { if (A[r][c] == 'e') { A[r][c] = '.'; printf("<%d,%d> ",r,c); if (r > 0) Visit(A,r-1,c); /* 1 */ if (c > 0) Visit(A,r,c-1); /* 2 */ if (r < 3) Visit(A,r+1,c); /* 3 */ if (c < 3) Visit(A,r,c+1); /* 4 */ } } the program will print: <0,0> <0,1> <1,1> <2,1> <3,1> <3,0> <3,2> <1,2> <1,3> if we call Visit(A,0,0) how could you reorder the statements labeled /* 1 */ to /* 4 */ to print: <1,1> <1,2> <1,3> <2,1> <3,1> <3,0> <3,2> <0,1> <0,0> if we call Visit(A,1,1)? How about: <1,1> <2,1> <3,1> <3,2> <3,0> <0,1> <0,0> <1,2> <1,3> 12. Give the Big-O notation (the growth-rate function) for each of the following formulas: 2 N + 3N + 4NlogN + 5/N 2 3 N 5N + 25N + 3N + 4 2 2 3N logN + 5N + NlogN + 2N 13. Give a situation where each of the sorts discussed in class (insertion, selection, and merge) is optimal. You should include a discussion of the performance characteristics of the sorting algorithm that backs up your answer.