CT2 1
CT2 1
Roll: Name:
h i
Write your answers in question paper. Answer all questions. All questions use C programming language.
Q1. Fill in the blanks for the following C function which returns the total number of duplicate elements
(i.e., the count of elements that appears more than once) in an array of integers. (4)
int countDuplicate ( int arr[] , int num ) {
int i, j, count = 0;
for ( i = 0 ; i < num ; ++i ) {
for ( j = i+1 ; j < num ; ++j ) {
if ( arr[i] == arr[j] ) { count++; break; }
}
}
return count ;
}
Q2. Fill in the blanks for the following C function changeToPalin so that it can return the minimum
number of character replacements required to turn a string into a palindrome as well as modify the
provided string into the desired palindrome internally. Note that, a palindrome is a string that reads
the same both forwards and backwards. Examples of palindromes are “noon” and “deified”. Hence,
– The minimum replacements required for “noon” and “deified” are zero character (each).
– The string “civil” can be turned into a palindrome by replacing minimum one character.
– The string “rastor” can be turned into a palindrome by replacing minimum two characters.
– The string “drawing” can be turned into a palindrome by replacing minimum three characters. (3)
#include <stdio.h>
#include <string.h>
int changeToPalin ( char *str, int len ) {
if ( len <= 1 ) return 0; /* base condition */
if ( str[0] == str[len-1] )
return changeToPalin ( str+1, len-2 ) ;
str[0] = str[len-1]; /* modifying the string into palindrome */
return 1 + changeToPalin ( str+1, len-2 ) ;
}
int main ( ) {
char S[100];
printf ("Enter String: "); scanf ("%s", S);
printf ( "%d ", changeToPalin(S, strlen(S)) ); printf ("%s", S);
return 0;
}
In case that your changeToPalin function can produce the desired result as specified in the problem
statement, what will be the output of the above C program when the input string is “neuralnet”? (2)
3 tenlalnet
— Page 1 of 3 —
Q3. You are given a set of nuts and bolts, each with unique sizes. However, the sizes are not directly
comparable with each other; you can only attempt to match a nut with a bolt to determine if one is
larger, smaller, or equal to the other. The following C program takes arrays of nuts and bolts as input,
matches each nut with its corresponding bolt, and prints out the matched pairs. The desired result of
the program is to correctly pair each nut with its corresponding bolt. Some sections of the code are
left blank. Fill in these blanks to ensure the code produces the desired results. (5)
#include <stdio.h>
#define MAX 6
void printArray ( char arr[] ) {
for(int i = 0; i < MAX; i++) printf ("%c ", arr[i]);
}
void swap ( char arr[], int i, int j ) {
int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp;
}
int splitArray ( char arr[], int low, int high, char pivot ) {
int i = low, j;
for( j = low ; j < high ; j++ ) {
if ( arr[j] < pivot ) {
swap ( arr, i, j ); i = i + 1 ;
}
else if ( arr[j] == pivot ) {
swap ( arr, j, high ); j = j - 1 ;
}
}
swap ( arr, i, high ) ;
return i;
}
void matchPairs ( char nuts[], char bolts[], int low, int high ) {
int pivot, tovip;
if ( low < high ) {
pivot = splitArray ( nuts, low, high, bolts[high] );
tovip = splitArray ( bolts, low, high, nuts[pivot] );
matchPairs ( nuts, bolts, low, pivot-1 );
matchPairs ( nuts, bolts, pivot+1, high );
}
}
int main ( ) {
char nuts[MAX] = { ‘@’, ‘#’, ‘$’, ‘%’, ‘^’, ‘&’ };
char bolts[MAX] = { ‘$’, ‘%’, ‘&’, ‘^’, ‘@’, ‘#’ };
matchPairs ( nuts, bolts, 0, MAX-1 );
printf ("Matched nuts and bolts are:\n");
printArray(nuts); printf("\n"); printArray(bolts);
return 0;
}
— Page 2 of 3 —
Q4. If the following C programs terminate, then write the output (when no output can be generated, write
NO-OUTPUT). Otherwise, say that the program does not terminate (write NO-TERMINATION).
Assume that, a pointer and an integer variables take 8 and 4 bytes of storage space, respectively. (2×3)
— Page 3 of 3 —
ROUGH WORK
— Additional Page 4 —