





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






Maximum Points: 100 Time: 120 minutes
Problem 1 ________ (20 pts.) Problem 2 ________ (20 pts.) Problem 3 ________ (25 pts.) Problem 4 ________ (15 pts.) Problem 5 (25 pts.) Total: ________ (105 pts.)
Instructions:
Problem 1. (20 points) Answer the following questions briefly.
(a) If p is defined to be an integer pointer and i is an integer, after the statement p = &i, what is the difference between *p and &p?
*p refers to the value of variable I while &p refers to the address of the pointer variable p.
(b) Is it semantically correct to return a locally defined (in the function) pointer from a function? Why or why not?
It is not semantically incorrect to do this because the moment a function returns, all its local variable go out of scope and we thus in the above case we end up with a pointer that points to nothing meaningful.
(c) If str is defined to be a character array of size 10, and it has been initialized to value ”abcdefghi”, what is the value of the expression *(str+9)?
Null
(d) If str is an array of character of size 2 and we define it is as char str[2] = {’\0’};
What are the contents of str[0] and str[1]?
Null and Null
(e) How many times would the statement for(;;){printf(“a”);} print a? Write a while statement that has the same effect.
a would be printed infinitely many times, while(1) { printf(“a”); }
(f) Let part be a structure as follows: struct part{ int number; int second_numer; };
How would you instantiate this structure to use it in your program? Assume the instance of the structure is to be named part1.
Problem 2. (20 points) Write down the EXACT output generated by following C program.
int main() { int i[2]={10,20}; int *p;
p = i;
printf("%d\n",p); printf("%d\n",++p); printf("%d\n",++p); printf("%d\n",(p-1)); printf("%d\n",*(--p)); }
Output: 10 11 20 11 11
Problem 3. (25 points) Write a C program that takes at most 10 strings as input from the user. The program removes duplicates, and concatenates them into a single string where each constituent string is separated from the next by the character ‘*’. The user indicates the end of the string list with a -1. Your program need not enforce the limit (indicated below) of 50 characters per string.
Example 1: Enter 10 or fewer strings (max 50 characters each) abc xyz aa bb cc aa
abcxyzaabbcc*
Solution: #include<stdio.h> #include<string.h> int main() { char name[10][50]; char lname[600]; int i=0,j,k; int flag; printf("Enter 10 or fewer strings (max 50 characters each)\n"); do { gets(name[i]); i++; }while((i!=10) && (strcmp(name[i-1],"-1")));
strcpy(lname,"\0"); for(j=0;j<i-1;j++) { flag = 0; for(k=0;k<j;k++) { if(!strcmp(name[k],name[j])) { flag = 1; break; } } if(!flag) { strcat(lname,name[j]); strcat(lname," "); } }
printf("%s\n",lname); }
Problem 5. (25 points) The Quicksort algorithm sorts an array of numbers as follows:
Example (before splitting): The pivot is 7.
7 2 8 5 11 10 4 1 9 6 3
After splitting:
2 5 4 1 6 3 7 9 10 11 8
After sorting recursively segments a[0] … a[5] and a[7] … a[10]:
1 2 3 4 5 6 7 8 9 10 11
After step 1, the element e=7 is in its proper location. The elements to the left of e are all less than or equal to e; they’ll be in their proper places once they’ve been sorted in step 2. Similar reasoning applies to the elements to the right of e. Notice that the pivot doesn’t necessarily end up at the mid-point of the array.
Now the two segments of the array, [2,5,4,1,6,3] (from index 0 to 5), and [9,10,11,8] (from index 7 to 10) can be sorted recursively.
Below is the code which implements the above algorithm. quicksort is the function that calls itself recursively after appropriately setting low and high values. split is the function that implements the above splitting of the values to the left and right of the pivot.
Your job is to write the split function. This function takes the array a, and indices low and high as arguments. The pivot value is a[low]. The function splits the elements into three groups (lower than the pivot, the pivot, and higher than the pivot) and returns the new location of the pivot to the quicksort function.
Hint: You may use an additional array as temporary storage to split the array segment.
Extra credit (up to 10 extra points) : Do the splitting WITHOUT using an additional array.
#include<stdio.h>
#define N 10
void quicksort(int a[], int low, int high); int split(int a[], int low, int high);
int main() { int a[N],i; printf("Enter %d numbers to be sorted: ",N); for(i=0;i<N;i++) scanf("%d",&a[i]);
quicksort(a,0,N-1);
printf("In sorted order: "); for(i=0;i<N;i++) printf("%d ",a[i]); printf("\n");
return 0; }
void quicksort(int a[], int low, int high) { int middle; if(low >= high) return; middle = split(a,low,high); quicksort(a,low,middle-1); quicksort(a,middle+1,high); }
int split(int a[], int low, int high) { /* YOU FILL in the code HERE */ }
Solution: int split(int a[], int low, int high) { int part_element = a[low];
for(;;) { while(low < high && part_element <= a[high]) high--; if(low >= high) break; a[low++] = a[high];
while(low <high && a[low] <= part_element)