2021 Spring
2021 Spring
Instructions:
1. Write your answers on paper. Answers must be handwritten. Typed or written answers using an
electronic device are not allowed.
2. Write your name and roll number on each page. Write page numbers for each page.
3. Answer all five questions. All parts of a question must be answered in the same place.
4. Scan all pages and collate. Create a single PDF file (of size <= 10 MB). You could also take pictures of
different pages, combine them to make a single pdf file.
5. The name of the file should be <RollNumber>_LT2A. Ensure your roll number in the filename has only
digits and uppercase characters.
6. Upload/Attach your file. Make sure you click on the 'Turn in' button to submit your file.
7. 10:10 AM is a strict deadline after which no submissions will be allowed.
8. We will not accept submission by any other means.
1. What is the output of the following C program when the input is the last two digits of your roll
number? [6 marks]
#include <stdio.h>
int k=50;
int f(int k) {
k=k+1; return k;
}
int g(int x) {
return (x*k);
}
int main() {
int a, x[6], j, k;
scanf("%d",&k); printf("k=%d\n",k);
a = f(k);
printf("\n%d %d %d", k, a, g(k));
}
Solution:
If the input is Z, then the program prints Z + (Z+1), (Z+2) + (Z+3), and (Z+3) + (Z+4). For
example, if the input is 30, then the output is 30+31=61, 32+33=65, and 33+34=67.
In the next part, the output is Z, Z + 1, and 50 x Z. For example, if the input is 30, then the
output is 30 31 1500.
2. The following code shows the bubble-sort function and the associated function for swapping.
(a) Indicate what code should fill the blanks with the red letters in the function swap().
#include <stdio.h>
L1: void bubbleSort(int arr[], int n)
L2: {
L3: int i, j;
L4: for (i = 0; i < n - 1; i++)
L5: for (j = 0; j < n - i - 1; j++)
L6: if (arr[j] > arr[j + 1])
L7: swap(&arr[j], &arr[j + 1]);
L8: }
void swap(____A___)
{
int temp;
temp = ____B____;
___C___ = *b ;
___D___ = temp;
}
[4 marks]
(b) As you may know, the last element of the array is exchanged at most once by the function
bubbleSort(). For some reason we want to use the philosophy of the algorithm in a way
that the first element is exchanged at most once. This can be done by rewriting only the line
L5. How will you rewrite L5 to make this possible?
[2 marks]
Solution:
(a) Swap function:
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
3. What does the following program print when the input is the last digit of your roll number?
#include <stdio.h>
#include <stdlib.h>
struct node {
int key;
struct node * next;
};
int main()
{
struct node *x, *y, *t;
int N;
x = (struct node *) malloc(sizeof(struct node));
y = (struct node *) malloc(sizeof(struct node));
x->next = y; y->next = x;
scanf("%d",&N); printf("N=%d\n",N);
x->key = y->key = N+1;
The program generates a Fibonacci sequence seeded by f0 = N+1, f1 = N+1. The program prints
N and then f2, f3, … where each fk = fk-1 + fk-2. The program stops after two successive numbers
greater than or equal to 25 are printed.
Write a non-recursive function LINK reverse_unique (LINK head) that takes in the start pointer
to an existing linked list of elements of the given type mentioned above and returns the start pointer to
the same list with the elements reversed after removal of duplicate copies of elements. During deletion
in case of duplicate copies, the SECOND COPY of an element in the original forward list is to be
retained and the other copies deleted. If there is only one copy of an element, it is retained in its proper
order. For example, if the original list is <2, 3, 3, 2, 4, 5, 2, 4, 2, 3, 1> then the reversed list pointed to
by the returned pointer will be <1, 4, 5, 2, 3>. Free the deleted elements. You are not allowed to use
any additional array. You are also not allowed to dynamically allocate any new element. Do not write
any other auxiliary function nor define any new user-defined data type. You can use only at most 10
local variables in the function which could be pointers (of type LINK) or integers only. Use only
standard input output library. Write suitable comments in your code to explain the steps and method.
[16 marks]
Sample Solution:
#include<stdio.h>
#include<stdlib.h>
struct list
{
int data;
struct list *next;
};
typedef struct list ELEMENT;
typedef ELEMENT *LINK;
LINK create_node(int val)
{
LINK newp;
newp = (LINK)malloc(sizeof(ELEMENT));
newp->data = val;
newp->next = NULL;
return (newp);
}
else
{
first = ptr;
prev = ptr;
while (prev->next!=NULL) prev = prev->next;
prev->next = newp;
return (first);
}
}
}
else
{
prev = head;
ptr = head->next;
while(ptr->data!=value)
{
prev = ptr;
ptr = ptr->next;
}
prev->next = ptr->next;
ptr->next = NULL;
free(ptr);
}
prev = place;
ptr = place->next;
while(ptr!=NULL)
{
if(ptr->data == value)
{
prev->next = ptr->next;
ptr->next = NULL;
free(ptr);
ptr = prev->next;
}
else
{
prev = ptr;
ptr = ptr->next;
}
}
// REVERSE
if (head == NULL) return head;
tail = head;
while (tail->next != NULL) tail = tail -> next;
ptr = head;
while(ptr != tail)
{
temp = ptr-> data;
ptr -> data = tail -> data;
tail -> data = temp;
if (ptr-> next == tail) break;
qtemp = ptr;
while(qtemp->next != tail) qtemp = qtemp ->next;
tail = qtemp;
ptr = ptr->next;
}
return head;
}
main()
{
int n, i, val, m, result;
LINK pointer;
printf("Give number of Elements: \n");
scanf("%d", &n);
printf("Number of Elements = %d \n", n);
for (i = 1; i <=n; i++)
{
printf("Give the %d-th element: ", i);
scanf("%d", &val);
printf("%d-th value read = %d \n", i, val);
if (i == 1) pointer = create_node(val);
else pointer = insert(val, pointer);
}
printf("The Read List is: \n");
print_list(pointer);
pointer = reverse_unique(pointer);
printf("The Reversed List after duplicate removal is: \n");
print_list(pointer);
}
where lua, rua, lla and rla are respectively the four n×n left upper, right upper, left lower and right
lower sub-matrices of ‘a’.
Write a C program that would take a 2n × 2n matrix ‘a’ as input and print the following 2n × 2n
matrix
• Given n as input, only one 2n×2n matrix of integers must be dynamically allocated in the
program.
• Only two n × n matrices must be dynamically allocated in the entire program.
• In order to generate any of the 4 n×n submatrices calls to the function having prototype
where the n×n submatrix of the 2n×2n matrix a[][] (with uppermost leftmost entry a[I][J]),
would be copied into the n×n matrix b[][]. For instance the function call for computing rla
would be copysubmatrix(rla,a,n,n,n) and for computing lla would be
copysubmatrix(lla,a,n,0,n).
• In order to transform the 2n×2n matrix a[][] to
where the n×n matrix b[][] will be placed in the 2n ×2n matrix a[][] with the element b[0][0]
as a[I][J]. So, you must call revcopysubmatrix(rua,a,n,0,n) to place rua at the
correct destination in a[][].
• You must write the functions copysubmatrix and revcopysubmatrix, and the entire
main program. The main program must read a[][] in the beginning and print a[][] at the end.
[16 marks]
Sample Solution:
#include <stdio.h>
#include <stdlib.h>
int main() {
int **a,n;
int **lua,**rua,**lla,**rla;
scanf("%d",&n);
a = arraymalloc(2*n);
rua = arraymalloc(n);
lla = arraymalloc(n);
readarray(a,2*n);
printCall(a,2*n);
copysubmatrix(rua,a,0,n,n);
copysubmatrix(lla,a,n,0,n);
printCall(rua,n);
printCall(lla,n);
revcopysubmatrix(rua,a,n,0,n);
revcopysubmatrix(lla,a,0,n,n);
printCall(a,2*n);
}