The University of Lahore
Department of CS & IT
Data Structure and Algorithms (Theory Class)
Assignment# 3
Instructor Name: Miss Kashmala Pervaiz 15th December, 2023
Course Title: Object Oriented Programming
Course Code:
Deadline: 22nd December, 2023 Total Marks: 10
Student Name: University ID:
1. Consider the following recursive function:
int mystery(int number) //Line 1
{ //Line 2
if (number == 0) //Line 3
return number; //Line 4
else //Line 5
return(number + mystery(number - 1)); //Line 6
} //Line 7
a. Identify the base case.
b. Identify the general case.
c. What valid values can be passed as parameters to the function mystery?
d. If mystery(0) is a valid call, what is its value? If not, explain why.
e. If mystery(5) is a valid call, what is its value? If not, explain why.
f. If mystery(-3) is a valid call, what is its value? If not, explain why.
2. Write a recursive a function to generate the following pattern of stars:
*
* *
* * *
* * * *
* * *
* *
*
Also, write a program that prompts the user to enter the number of lines in the pattern and uses
the recursive function to generate the pattern. For example, specifying 4 as the number of lines
generates the preceding pattern.
3. Assume the following list of keys: 16, 38, 54, 80, 22, 65, 55, 48, 64, 95, 5, 100, 58, 25, 36 This list is
to be sorted using quicksort as discussed in this chapter. Use pivot as the middle element of the
list.
a. Give the resulting list after one call to the partition procedure.
b. Give the resulting list after two calls to the partition procedure.
4. Assume the following list of keys: 18, 40, 16, 82, 64, 67, 57, 50, 37, 47, 72, 14, 17, 27, 35 This list is
to be sorted using quicksort as discussed in this chapter. Use pivot as the median of the first, last,
and middle elements of the list.
a. What is the pivot?
b. Give the resulting list after one call to the partition procedure.