please do work in c!!! CS 2123 Data Structures Fall 2016- Midterm 2 -Oct 27,2016
ID: 3735237 • Letter: P
Question
please do work in c!!!
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, Question is too long, as per the Chegg guideline, Please post one Question per post.
Please find my answer for Q2.
// 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]);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.