0% found this document useful (0 votes)
72 views4 pages

PDS Lab Spring 23 Sec6 Week4

This document provides instructions for Assignment 4 of the PDS Lab Spring 23 Sec 6 course, which involves writing recursive functions to compute the number of unique collections of ducklings sitting in different configurations and to generate and check subsets of sets of numbers. It describes two questions, the first involving computing counts of unique duckling collections on a line and circle, and the second generating all subsets of a set of numbers and checking if a given set is a subset. Sample outputs are provided.

Uploaded by

Dchampion
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views4 pages

PDS Lab Spring 23 Sec6 Week4

This document provides instructions for Assignment 4 of the PDS Lab Spring 23 Sec 6 course, which involves writing recursive functions to compute the number of unique collections of ducklings sitting in different configurations and to generate and check subsets of sets of numbers. It describes two questions, the first involving computing counts of unique duckling collections on a line and circle, and the second generating all subsets of a set of numbers and checking if a given set is a subset. Sample outputs are provided.

Uploaded by

Dchampion
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

PDS Lab Spring 23 Sec 6 Assignment 4

Instructors: Satrajit Ghosh and Somak Aditya

Topic: Functions, Recursion

Important information and instructions:

● Only concepts learnt upto Functions, Recursion can be used. No


advanced concept (e.g., 2-D arrays, structures etc.) is allowed.
● No library function except printf and scanf is allowed.
● You must submit exactly one C source file for each question.
● Make sure that your programs match the given sample outputs as
closely as possible.
● Name your file as “A04_ROLLNO_q1.c”, “A04_ROLLNO_q2.c”

Q1. Let n be a positive integer. Suppose that n ducklings are sitting on a


straight line. Let us number them as 1, 2,..., n in the order they appear on
the line. The ducklings are start fighting whenever they are close to one
another.

a) [40 Points]
A subcollection of these n ducklings is said to be UTOPIC if the
subcollection does not contain any two consecutive ducklings, i.e.,
duckling i and i+1 for some value of i. The empty subcollection is
also treated as UTOPIC. Let Ln denote the number of UTOPIC
subcollections of n ducklings on a line. These collections together
with Ln for some small values of n are given in the table below:

n UTOPIC collections on a line Ln

1 Empty,{1} 2

2 Empty,{1},{2} 3
3 Empty,{1},{2},{3},{1,3} 5

4 Empty,{1},{2},{3},{4},{1,3},{2,4},{1,4} 8

5 Empty,{1},{2},{3},{4},{5},{1,3},{2,4},{3,5},{1,4},{2,5}, 13
{1,5},{1,3,5}

Write a recursive function in order to compute the value of Ln.

The function should take two arguments: 𝑛 and the last duckling 𝑙 chosen
so far. The next choice must be one from 𝑙 + 2, ... , 𝑛. For each such
choice m, pass n and m to a recursive call. Another possibility is that no
other duckling is chosen in the collection at all. Take a sum of the counts
from all possibilities and return the sum. The outermost call of the
recursive function in the main() function must pass a suitable value for
the second parameter.

b) [40 Points]
Next suppose that the ducklings are sitting on a circle, so that
duckling 1 and 𝑛 are now adjacent to one another. Let Cn denote the
number of UTOPIC subcollections of n ducklings in a circle. The
following table summarizes some small values.

n UTOPIC collections on a circle Cn

1 Empty,{1} 2

2 Empty,{1},{2} 3

3 Empty,{1},{2},{3} 4

4 Empty,{1},{2},{3},{4},{1,3},{2,4} 7

5 Empty,{1},{2},{3},{4},{5},{1,3},{2,4},{3,5},{1,4},{2,5} 11
Write a second recursive function for computing the value of Cn. Here
break the computation in two parts according to whether the first duckling
is chosen or not. In addition to 𝑛 and the last duckling 𝑙 chosen so far, you
should also pass an indicator (flag) about the choice of the first duckling.

Note that you are not asked to find out the UTOPIC collections
themselves. You are asked only to compute their counts. These counts
can be obtained in a variety of ways. Here we urge you strongly to follow
the recursive strategy outlined above. This will help you in understanding
recursive behavior of functions. Following other algorithms may defeat
this basic purpose.

Report the values of Ln and Cn for n = 19, 23, 29, 37.

Sample output
Enter number of ducklings : 10
Number of UTOPIC collections in a line = 144
Number of UTOPIC collections in a circle = 123

Enter number of ducklings : 15


Number of UTOPIC collections in a line = 1597
Number of UTOPIC collections in a circle = 1364
Q2. [20 Points]
A set is an unordered collection of numbers. For example, {1,2} and {2,1}
are the same. Write a program to do the following:
1. Ask the user to enter a set of numbers.
2. Write a recursive function to print all subsets of the set (without
repetitions)
3. [Bonus 10 points] Ask the user to enter another set of numbers and
check whether it is a subset of the entered set.

Sample output
Enter numbers: 1, 2, 4
Subsets: {}, {1}, {2}, {4}, {1,2}, {2,4}, {1,4}, {1,2,4}

Enter Set: 1,3


Entered set is not a subset
—-------------------
Enter numbers: 1, 3, 5
Subsets: {}, {1}, {3}, {5}, {1,3}, {3,5}, {1,5}, {1,3,5}

Enter Set: 5, 1
Entered set is a subset
—---------------------

You might also like