0% found this document useful (0 votes)
14 views3 pages

Part B - Solutions

The document is a comprehensive exam for a Computer Programming course at Birla Institute of Technology & Science, Pilani, covering various programming concepts and functions. It includes questions on character arrays, student admission criteria based on subject marks, and operations on sparse arrays using linked lists. Each question provides partially implemented code that students are required to complete and analyze.

Uploaded by

Harshul Singal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views3 pages

Part B - Solutions

The document is a comprehensive exam for a Computer Programming course at Birla Institute of Technology & Science, Pilani, covering various programming concepts and functions. It includes questions on character arrays, student admission criteria based on subject marks, and operations on sparse arrays using linked lists. Each question provides partially implemented code that students are required to complete and analyze.

Uploaded by

Harshul Singal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Birla Institute of Technology & Science, Pilani

Second Semester 2014-2015, CS F111 Computer Programming


Comprehensive Exam (Open Book) PART B
Date: May 16, 2015 Time: 2.00 - 4.00 PM Max. Marks: 45
NOTE: Each question is partially implemented. Write the missing parts as your answers at the designated place in the
separate answer sheet provided. Write only one statement in each box.
Q1. Consider a two dimensional character array named as Arr[row][col]. [1+2+2+1+2+2+2]
A sequence S1 in the ith row of the array is defined as a void Find_Sequence(int col, char Arr[][col], int row) {
combination of two characters (Arr[i][j], Arr[i][k]) int i,j,k,m, count=0;
where j<k. In the example array, Arr, shown below char f_char_tocheck, s_char_tocheck,s_char, f_char;
(Figure 2) : < ae, ac, ad, ec, ed, cd> are all the possible for (i=0;i<(col-1);i++)
S1 sequences of the 0th row. {
th
A sequence S2 of i row is defined as a combination of for (j=i;j<(col-1);j++)
{ //Characters from first row to search
two characters (Arr[i][j], Arr[i][k]) where j<k AND
f_char_tocheck = Arr[0][i];
k=j+1. In the example array, Arr, shown below (Figure s_char_tocheck = Arr[0][j+1];
2): <db, ba, ac> are all the possible S2 sequences of for (k=1;k<row;k++)
the 1st row. {
The function Find_Sequence() (Figure 1) takes a 2-D for (m=0;m<(col-1);m++)
character array Arr, number of columns and rows as { //Characters from remaining rows to compare
an input. It finds for each possible S1 sequences of the f_char = Arr[k][m];
th s_char = Arr[k][m+1];
0 row of Arr, the number of occurrences of S2
sequences in the remaining rows of the Arr. if ((f_char_tocheck == f_char) && (s_char_tocheck == s_char))
count++;
Example array Arr[] Output of Find_Sequence() } //innermost for loop
a e c d ae occurs 0 times.
}
d b a c ac occurs 1 times.
printf ("%c %c occurs %d times\n",
e c a d ad occurs 2 times.
f_char_tocheck, s_char_tocheck, count);
ec occurs 1 times.
a d b c count=0;
ed occurs 0 times.
}
Figure 2 [Q1] cd occurs 0 times.
} //outermost for loop
Q2. Assume that a particular college for its admission } Figure 1 [Q1]
considers a total of best 4 out of 6 float compute_marks_college (student s){ typedef struct{
subjects. For this, a candidate specifies int i,j,min; char ID[12];
the best 4 subjects whose total marks float sum=0; struct{
are to be considered. Function for (i=0;i<2;i++) float sub[6];
compute_marks() (Figure 4) is { } marks;
implemented for it. min = 0; int choice;
for (j=1;j<6;j++) float bfm;
However, college also validates this by
{ } student; Figure 5 [Q2]
adding the marks of best 4 subjects. if (s.marks.sub[j] < s.marks.sub[min])
This is done based on actual marks of void compute_marks (student *s)
min = j;
6 sub's without considering students {
}
choice. In figure 3, function float sum=0;
s.marks.sub[min] = 999;//hint
compute_marks_college() is int bit, pos=5;
}
while (pos >= 0) {
implemented for it. for (i=0;i<6;i++)
bit = (s->choice) % 10;
To represent each student, a structure {
s->choice /= 10;
named student is defined. Its fields are if (s.marks.sub[i] != 999)
switch (bit)
as follows: (a) ID is a unique ID number sum += s.marks.sub[i];
{
}
of student, (b) array "sub" stores the case 0: break;
return sum;
marks of 6 subjects, where sub[0] is a } Figure 3 [Q2]
case 1: sum += s->marks.sub[pos];
first subject. Variable "choice" is a six }
digit integer, where each of its digits is either 0 or 1. It represents the choices of pos --;
subjects given by student; 0 means don't consider the subject among best 4 and }
1 means consider it. The most significant digit represents the first subject and s->bfm = sum;
} Figure 4 [Q2]
least significant one represents the sixth subject. For example, if choice =
101101, it means 2nd and 5th subject (whose marks are in sub[1] and sub [4]) should not be considered among best 4.
Assume marks in a subject can be between 0 and 100, both inclusive. "bfm" stores the marks of best four subjects.
Q3. An integer type sparse-array is a one dimensional array whose most struct node { struct head {
of the elements are 0's (zero). In order to save memory required to store int index; int size;
such array, the non-zero elements of sparse-array can be stored in the int val; int nze;
form of a singly linked list. The node of a list (Figure 6) contains (a) index struct node *next; NODE link;
of non-zero element, (b) element itself, and (c) pointer to next node of }; };
the list. Moreover, the nodes of the list are sorted with respect to index. typedef typedef
struct node * NODE; struct head * HEAD;
The head node of the list (Figure 7) contains (a) size of original array, (b)
total number of non-zero elements in sparse array (means total number
Figure 6 [Q3] Figure 7 [Q3]
of nodes in linked list), and (c) pointer to first node of the list. For
example, consider the following sparse array:
index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
element 0 5 0 2 9 0 0 0 0 0 0 0 0 0 0 0 0 2 0 1
The linked list representation of the above array is as follows:
1 5 3 2 4 9 17 2 19 1 NULL
20 5
HEAD createList(FILE *fp1) {
Q3 (a) The function, createList() (Figure 8), takes a FILE pointer of an already int num1,num2,i,hsize,hval;
opened file as input argument. Each row of this file contains an integer NODE temp,cur;
number. The first two integer entries of the file indicate respectively the size HEAD h;
of sparse-array and number of non zero elements (nze). It is followed by nze fscanf(fp1,"%d %d",&hsize,&hval);
number of integer pairs; where first number of this pair represent index of h=(HEAD)malloc(sizeof(struct head));
non-zero element in sparse array, and second number represent the non-zero h->size=hsize;
h->nze =hval;
element itself. This function creates a list by reading elements from this file
h->link=NULL;
and returns a pointer of its head node. Assume that integer pairs in file are in for(i=2;i<=h->nze+1;i++)
sorted order wrt to index. {
HEAD add_merge(HEAD list1,HEAD list2) { else if(i->index == j->index) fscanf(fp1,"%d %d",&num1,&num2);
HEAD newlist; { temp=(NODE)malloc(sizeof(struct node));
NODE temp, i, j, k, c; temp= (NODE) temp->index=num1;
newlist=(HEAD)malloc(sizeof(struct malloc(sizeof(struct node)); temp->val=num2;
head)); temp->val=i->val + j->val; temp->next=NULL;
if(list1->size > list2->size) temp->index =i->index; if (h->link==NULL)
newlist->size=list1->size; k->next=temp; h->link=temp;
else i=i->next; else {
newlist->size=list2->size; j=j->next; cur=h->link;
newlist->nze =0; newlist->nze =newlist->nze+1; while(cur->next!=NULL)
newlist->link=NULL; list1->nze = list1->nze-1; cur=cur->next;
i=list1->link; list2->nze = list2->nze-1; cur->next=temp;
j=list2->link; } }
k = c =(NODE)malloc(sizeof(struct node)); k=k->next; }
while(i!=NULL && j!=NULL) } //end of while loop return(h);
{ if(i==NULL) } Figure 8 [Q3 (a)]
if(i->index < j->index) {
{ k->next=j; Q3 (b) The function, add_merge(), (figure
k->next=i; newlist->nze= 9), takes two lists as argument. It add and
i=i->next; newlist-> nze+list2->nze;
merge them into one list and returns head
newlist->nze =newlist->nze+1; }
pointer of this newly created list.
list1->nze = list1->nze-1; else
} {
To add and merge, start traversing the two
else if(i->index > j->index) k->next=i; lists simultaneously. If index stored in both
{ newlist->nze= the nodes of the list is same, then insert a
k->next=j; newlist->nze+list1-> nze; new node in the new list with its value val
j=j->next; } set to addition of val values of two nodes.
newlist->nze =newlist->nze+1; newlist->link=c->next; Rather, if they are not same, then insert
list2->nze = list2->nze-1; return(newlist); the node of the corresponding list into
} } Figure 9 [Q3 (b)]
new list such that the nodes of the new list
are in sorted order wrt index. The size of head node of new list becomes the larger of two sizes of list1 and list2. Example
of add and merge is shown in at one of the page of your answer sheet. [1+]
Example: List 1:
1 5 3 2 4 9 5 2 NULL
20 4

List 2:
2 6 3 2 4 9 6 12 NULL
15 4

After add and merger operation, new list becomes as follows:

1 5 2 6 3 4 4 18 5 2 6 12 NULL
20 6

You might also like