Shiv Nadar University CSD101: Introduction To Computing and Programming Lab #4 Expressions, Loops, Functions, 1D Arrays - 1
Shiv Nadar University CSD101: Introduction To Computing and Programming Lab #4 Expressions, Loops, Functions, 1D Arrays - 1
1. A perfect number is a positive integer that is equal to the sum of all its divisors excluding itself. For example,
6 = 1 + 2 + 3, 28 = 1 + 2 + 4 + 7 + 14. We do not know whether or not there are infinitely many perfect
numbers. Also, not known is whether odd perfect numbers exist.
(a) Write a C function that given an integer argument, say n, returns true if n is perfect and false otherwise.
(b) Using the above function write a main function that reads in a positive integer m and prints out the
mth perfect number. Don’t try this for large values of m since perfect numbers grow large very fast. For
example, the fifth perfect number is: 33550336. So, even for m=5 the program will run for quite some
time.
For more information on perfect numbers look at the Wikipdedia entry.
[15,10=25]
2. The following questions ask you to arrange numbers in a sequence in different ways. You should first read n,
the length of the sequence. Then read n integers that are part of the sequence into a 1-dimensional array.
Arranging the elements of a sequence in ascending or descending order is called sorting. In all programs below
you are not allowed to use an extra array. You can manipulate only the array into which you have read in the
numbers.
(a) Arrange the sequence in ascending order as follows. Make a pass over the array and interchange neigh-
bouring numbers that are not in the right order. Repeat such passes till the array is sorted. An example
below shows the starting array and the array after the first, second, third passes and the final sorted
array.
{4,2,5,7,8,1} → {2,4,5,7,1,8} → {2,4,5,1,7,8} → {2,4,1,5,7,8} . . . → {1,2,4,5,7,8}.
(b) Here is another way to arrange it in ascending order. Find the index of the minimum element in the
array (use a function for this) and exchange contents of the 1st element and the minimum element index.
Now the minimum element is in the first position. Now repeat this process for the second, third etc. till
the last position. Then the array is sorted. The example below shows the starting array followed by the
first three rounds and the final array.
{4,2,5,7,8,1} → {1,2,5,7,8,4} → {1,2,5,7,8,4} → {1,2,4,7,8,5} . . . → {1,2,4,5,7,8}.
(c) Arrange the elements in the 1-dimensional array as follows. Pick the smallest element and put it in the
middle of the array, then pick the second smallest element and put it immediately to the left of the middle
element, pick the third smallest element and put it to the right of the middle element, continue placing
elements alternately to the left and right till the arrangement is done. Here are two examples (the initial
middle index is length(s)/2 where s is the sequence) where the first sequence is the initial sequence and
the last is the final sequence:
{3,1,4,2,7} → {4,2,1,3,7}
{4,2,5,7,8,1} → {8,5,2,1,4,7}
[10,10,25=45]
3. This question deals with sequences of characters. Store sequences as one-dimensional arrays.
(a) Read in two sequences of characters, say l1 and l2 , and print l2 − l1 if l1 is a subsequence of l2 and l1 − l2
if l2 is a subsequence of l1 . For example, if l1 ={’a’,’2’,’c’,’d’,’e’} and l2 ={’2’,’c’,’d’} then
it should print {’a’,’e’}.
(b) Read in two sequences l1 and l2 and print the earliest, longest common subsequence. For example, If
l1 ={’a’,’b’,’c’,’d’,’e’,’f’,’b’,’b’,’d’,’e’,’f’,’a’} and
l2 ={’b’,’c’,’d’,’e’,’f’,’a’,’b’,’d’,’e’,’f’,’a’,’b’,’c’} then there are two longest common
subsequences {’b’,’c’,’d’,’e’,’f’} and {’b’,’d’,’e’,’f’,’a’} both of length 5. The output
should be {’b’,’c’,’d’,’e’,’f’} since it is the earliest, that is starting with the least index (in either
sequence).
[15,20=35]
Page 2