Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Indian Institute of Technology, Kharagpur

CS19001 Programming and Data Structures Laboratory, Autumn 2019

Assignment for Week 5 (September 2, 2019)

Total Marks: 40 Submission Deadline: 17:45

INSTRUCTIONS

1. Submit a separate C file for each of the problems. The solution for problem i should be named
[rollno]-probi.c where ‘[rollno]’ is your roll number.

2. For problems 2 and 3, you are allowed to use library functions such as strlen and strcmp.

3. You may consult your notes, books or manual pages.

PROBLEMS

1. Write a program that on input an array A[0, ..., n − 1] of integers, separates the negative and non-
negative integers. That is, you have to modify A such that all the negative integers are to the left
and the non-negative integers on the right. The order of the negative/non-negative integers need
not be retained. You could do this as follows: define two indices i, j indicating that elements in
A[0, . . . , i − 1] and A[j + 1, . . . , n − 1] are negative and non-negative respectively. (i, j are initialised
to 0, n − 1 respectively.) Repeat the following as long as i < j: if A[i] < 0, then increment i;
otherwise, A[i] is positive – swap A[i] and A[j] and decrement j. At the end, the array will have
negative integers on one side and non-negative integers on the other. [16]

Sample Output

Enter n: 9
Enter 9 integers: 4 -5 3 8 -9 -10 1 -2 7
Input array:
4, -5, 3, 8, -9, -10, 1, -2, 7
Modified array:
-2, -5, -10, -9, 8, 1, 7, 3, 4

2. A word s is called x-ish if all the letters of x are contained in s in some order. Write a recursive
program that, given two words s and x, detects if s is x-ish or not. For example, if x = ‘‘elf’’,
then the word ‘‘tasteful’’ is x-ish (or elf-ish) whereas the word ‘‘malfunction’’ is not. [16]

3. Write a program that takes a word (with less than 10 characters) as input and prints all permutations
of the same in lexicograpic (dictionary) order. Do not use any library functions for string manipulation
except for strlen.

1
Example

Input: cat
Output:
act
atc
cat
cta
tac
tca

Suppose the given word is stored as a string str. The first word in the list will be the characters of
str sorted in ascending order. Use a sorting procedure to sort the characters of the given word to
obtain the first entry in the list. Let str with length len denote the current permutation (initialised
to the first word with characters in ascending order). The next permutation in lexicographic order
can be generated from str as follows:

• find the rightmost character str[i] such that str[i] < str[i + 1]. If no such character exists,
then terminate (the last permutation is already reached).
• find the smallest character str[j] in str[i + 1, . . . , n − 1] such that str[i] < str[j]. Swap str[i]
and str[j] and reverse the substring str[i + 1, . . . , len − 1]. Print str

Repeat the above steps indefinitely. It will terminate eventually. (Convince yourself that the above
method always works!)
For example, say str = ‘‘strange’’. It must be followed by ‘‘streagn’’ in the list of permuta-
tions. Let us see if the above method works. The rightmost character as in step 1 is ‘a’ = str[3]
and the smallest character greater than it is ‘e’ = str[6]. After swapping str[3] and str[6], we
have str = ‘‘strenga’’. We then reverse str[4, 5, 6] = “nga” thus obtaining str = “streagn”.
Note: For a string of length n, the number of strings printed must be n!.
Below is the pseudocode for insertion sort algorithm for sorting a character array s of length n in
ascending order.

i = 1;
while (i < n){
j = i - 1;
t = s[i];
while (j>=0 && s[j] > t){
s[j+1] = s[j];
j--;
}
s[j+1] = t;
i = i+1;
}

[8]

You might also like