CSD101: Introduction To Computing and Programming: Marks Obtained 40
CSD101: Introduction To Computing and Programming: Marks Obtained 40
1. You should upload your code and sample input.txt file in a zip archive on Blackboard.
2. You get 10 bonus marks if you complete both the iterative and recursive versions. The normalized
score will be calculated by ( Marks 40
obtained
× 10). So, if you do any one out of iterative or recursive
correctly you get 40 marks (the max marks). You get bonus marks only if you do both iterative and
recursive correctly.
1. This lab quiz is slightly different from the earlier one. You have been given a code template and you
have to fill in code for the missing parts that are indicated via comments in the code so that the entire
program works correctly.
The C program given implements merging of two sorted lists of integers that have been sorted in
ascending order. For example, if the two lists are:
1 4 5 7 9 80
2 3 21 33 45 58
[ 1 4 5 7 9 80 ]
[ 2 3 21 33 45 58 ]
Recursive merge = [ 1 2 3 4 5 7 9 21 33 45 ]
Iterative merge = [ 1 2 3 4 5 7 9 21 33 45 ]
Sample 2:
-2 5 9 22 34
-8 -6 4 27 28 30 45
[ -2 5 9 22 34 ]
[ -8 -6 4 27 28 30 45 ]
Recursive merge = [ -8 -6 -2 4 5 9 22 27 28 30 34 45 ]
Iterative merge = [ -8 -6 -2 4 5 9 22 27 28 30 34 45 ]
The code is given below. It includes some descriptive comments and comments that asks you to fill in
code - shown by /* FILL IN CODE HERE */. You have to fill in the code at the places indicated so
that the whole program works correctly. If you only want to do one of iterative or recursive comment
out or delete the parts that are not relevant.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define N 101 //max length of line in input.txt is N-1
struct Node {
int data;
struct Node *next;
};
struct QNode {
int data;
struct Node *head, *tail;
};
Page 2
struct QNode *createQN(void) {
//creates an empty QNode
struct QNode *qp=malloc(sizeof(struct QNode));
qp->head=qp->tail=NULL;
return qp;
}
Page 3
int main(void) {
FILE *infile=fopen("input.txt", "r");
struct QNode *qp1=createQN(), *qp2=createQN();
char str[N], s[10];
int ind;
// Creates first list
fgets(str, N, infile);//reads line
ind=0;
while (isspace(str[ind])) ind++;//purge initial white space
while (str[ind]!=’\0’) {//enters with non-whitespace char
int i=0;
while (!isspace(str[ind+i])) {s[i]=str[ind+i]; i++;}//read digits
s[i]=’\0’;
qp1=addLast(qp1, atoi(s));//add integer to list
ind+=i;
while (isspace(str[ind])) ind++;//purge whitespace
}
// Creates second list
fclose(infile);
//print list 1
printList(qp1->head);
//print list 2
printList(qp2->head);
printf("Recursive merge = ");
printList(/* FILL IN CODE HERE */);
printf("Iterative merge = ");
printList(/* FILL IN CODE HERE */);
return 0;
}
[40+10 Bonus]
Page 4