Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CS 2123 Data Structures Fall 2016- Midterm 2 -Oct 27,2016 You have 75 min. Good

ID: 3735272 • Letter: C

Question

CS 2123 Data Structures Fall 2016- Midterm 2 -Oct 27,2016 You have 75 min. Good luck. This exam has 5 questions in 8 pages You can use the 2-page C reference card posted in the class web page Section.... SURVEY (2pt bonus credit) A. Within the last 5 weeks (after midterm 1) How many TUTORING sessions did you attend? How many times did you get help from COMMON TAs in the Main CS lab?: How many times did you get help from the PROFESSOR during his office hours? B. Which one you do you think is more useful; thus should be increased? (circle one) BOTH(3) NONE(0) TUTORING(1) COMMON TA(2) C. What other help do you think the Department should provide? 1. (20 pt) Review Questions: (4pt) Suppose one of your friends tries to implement a function that can dynamically create a new copy of a given string. He/she came up with the following one: a. char *CopyString (char *s) char *ns; ns (char *) malloc (sizeof (s) ) ; if(ns==NULL) return NULL ; strcpy (ns, s); return ns; It compiles OK and works sometimes (when the original string is very short). But most of the time (when the original string is longer) it fails and gives a segmentation fault. What is the problem, why does it work sometimes, and how would you fix it? Give your answer in next page..

Explanation / Answer

Hi friend, Questions is too long. Please post one question in one post as per the chegg guideline.

I have answered B and C.

B)

// helper method

int max(int n1, int n2)

{

return n1 > n2 ? n1 : n2;

}

int arr_max_recursive(int arr[], int n) {

// base case

if (n == 1)

return arr[0];

// recursive case

return max(arr_max_recursive(arr, n - 1), arr[n - 1]);

}

C)

i)

Number of lines printed:

32 + 31 + 30 + .... + 2+1

= 528

Big-O = O(N^2)

ii)

Number of times outer loop runs:

logN

Number of times inner for loop runs:

logN

So total number of times line printed:

logN x logN

= log32 x log32

5x5

= 25

Big-O = (logN)^2