A23 cs101 Endsem
A23 cs101 Endsem
Roll Number
Division and Group
Name
TOTAL
• Write your roll number, name, and group number on this page in the space provided. A paper
without a roll number and name will NOT be graded.
• Write your answers neatly with a blue/black pen on this question paper itself in the space provided
for each question. At the end, you must submit this paper to the invigilator.
• Rough pages will NOT be provided. Use the empty space in the margins.
• Please note that your answers should NOT include any programming concept that hasn’t been
covered in the class so far. If such answers are found, they shall NOT be graded.
• No clarifications will be provided on any question. When in doubt, make suitable assumptions,
state them clearly, and proceed to solve the problem.
1
1. [15 points] The goal for the two code snippets below is to sort the array “data” containing N
integer elements in asending order using the Insertion Sort algorithm whose logic should be as
described in the practice examples covered in the lab sessions in CS-101.
• Each code snippet has some missing statements that you should fill in.
• Within each code snippet, each existing statement may or may not have a bug (compilation
or logical) that can lead to an incorrect answer. For each existing line, indicate whether any
modification is needed, and if so, what the modification is. You cannot remove any existing type
of statement (e.g., loop or assignment or “if”) from the program. Any line or expression that you
modify unnecessarily will lead to negative marking that will deduct 2 points for each such
modified instance.
Assume that the unsorted numbers have been read into the array “data” that is now to be sorted
using Insertion Sort.
Version 1:
Original Code Update Code (only the required lines)
for (unsigned int i = 0; i < N; i++) {
const int current = data[I];
unsigned int newPosition = I;
while (current < data[newPosition-1]
&& newPosition > 0) {
// Write 1 and only 1 statement →
blank A
newPosition–;
}
// Write 1 and only 1 statement →
blank B
}
Version 2:
Original Code Update Code (only the required lines)
for (unsigned int i = 1; i < N; i++) {
for ( blank A ) {
if ( blank B ) {
const int temp = data[j];
data[j] = data[j-1];
data[j-1] = temp;
} } }
2
2. [15 points] Consider a social network of students. Each student has a list of best friends. For a
given student, say A, we can define her/his best-friend network as follows:
A student X is in the best-friend network of student A if:
(i) X belongs to the list of best friends of A, or
(ii) X is a best friend of a student who is within the best-friend network of A.
Thus, for student A, her/his best-friend network comprises all her/his best friends, their best-
friends’ best-friends, their best-friends’ best-friends’ best-friends, and so on.
Your goal is to design an algorithm to determine whether, for a given student A within a given
social network, does another specified student Y belong to the best-friend network of A.
Each student record A contains variables storing the following information:
(i) the student’s roll number in integer “RollNum”,
(ii) the number of the student’s best friends in integer “BestFriendsCount”
(ii) the student’s list of best friends stored as pointers to other students’ structures within an array
of pointers “BestFriends”.
Consider a recursive function “Search” intended to do the job.
During the search, this function will utilize an array of bool variables named “Checked” to check
whether a given student’s roll number has been checked for a match or not.
• (5 points) The following code has some missing expressions that you should fill in.
#include <iostream>
using namespace std;
struct student {
unsigned int RollNum;
unsigned int BestFriendsCount;
student * BestFriends[3];
};
bool Search (student & Y, student & X, bool * Checked)
{
Checked[Y.RollNum] = ;
cout << "checking: " << Y.RollNum << endl;
if (Y.RollNum == X.RollNum)
{
return ;
}
bool found = false;
for (unsigned int k = 0; k < Y.BestFriendsCount; k++)
{
student Z = *(Y.BestFriends[k]);
if ( )
if ( )
return ;
}
return ;
}
3
int main ()
{
student A, B, C, D, E, F, G, H;
// network
A.RollNum = 0; A.BestFriendsCount = 3;
A.BestFriends[0] = &B; A.BestFriends[1] = &C; A.BestFriends[2] = &E;
B.RollNum = 1; B.BestFriendsCount = 3;
B.BestFriends[0] = &A; B.BestFriends[1] = &D; B.BestFriends[2] = &F;
C.RollNum = 2; C.BestFriendsCount = 2;
C.BestFriends[0] = &A; C.BestFriends[1] = &G;
D.RollNum = 3; D.BestFriendsCount = 1;
D.BestFriends[0] = &B;
E.RollNum = 4; E.BestFriendsCount = 2;
E.BestFriends[0] = &A; E.BestFriends[1] = &F;
F.RollNum = 5; F.BestFriendsCount = 2;
F.BestFriends[0] = &B; F.BestFriends[1] = &E;
G.RollNum = 6; G.BestFriendsCount = 1;
G.BestFriends[0] = &C;
H.RollNum = 7; H.BestFriendsCount = 2;
H.BestFriends[0] = &A; H.BestFriends[1] = &B;
//
bool Checked[8];
// search
for (int i = 0; i < 8; i++) Checked[i] = false;
cout << "---\n" << A.RollNum << "\t" << E.RollNum << "\t" << endl;
cout << Search (A, E, Checked) << endl;
// search
for (int i = 0; i < 8; i++) Checked[i] = false;
cout << "---\n" << A.RollNum << "\t" << H.RollNum << "\t" << endl;
cout << Search (A, H, Checked) << endl;
}
4
• (5 points) Trace the execution (including the recursive calls) for “Search(A,E)”, i.e., describe the
exact output of the program.
• (5 points) Trace the execution (including the recursive calls) for “Search(A,H)”, i.e., describe the
exact output of the program.
5
3. [15 points]
Consider a regression problem where the data is {(xi ∈ R, yi ∈ R) : i = 0, 1, · · · , 100} and you
want to learn a model to predict y for a given x.
You choose the model to be a quadratic model defined as follows: f (x) = ax2 + bx + c, where
a, b, c are the unknown model parameters.
Your goal is to fit the model to the data, in the same spirit as was covered in the lectures. For this
purpose, answer the sub-parts (I), (II), and (III) as directed:
(I). [3 marks] Design a mathematical quality-of-fit criterion (precisely state this in purely mathe-
matical terms), and
(II). [10 marks] Derive a set of equations that are needed to be solved to estimate the model
parameters.
Derive these equations and write them in the form of M v = b, where
(i) M is a matrix of size 3 × 3
(ii) v is a column vector of size 3 × 1 representing the variables a, b, c, and
(iii) b is a column vector of size 3 × 1.
6
hello
PN
(III). [2 marks] Now, if you were to fit a N -th degree polynomial of the form f (x) = n=0 an (x)n ,
with unknown model parameters {a0 , a1 , · · · , aN }, where N > 2, then:
Describe what algorithm that we’ve covered in class would you use to solve the resulting system
of equations.
7
4. [15 points] You want to write a program to simulate the average number that one would obtain
from 1000 repeated independent throws of an unfair die.
As you know, for a throw of a fair die, the probability of getting any integer (i.e., 1,2,3,4,5,6) is 1/6.
However, for the unfair die in this question, the probability of the die showing a number n is
proportional to n itself. Thus, for example, the probability of the die showing 4 is four times the
probability of the die showing 1.
In the following code, each line may have a single error or may have some blanks that need to
be filled to avoid compilation errors and logical errors.
• For each existing line, indicate whether any modification is needed, and if so, what the modifi-
cation is. You cannot remove any existing type of statement (e.g., loop or assignment or “if”) from
the program. Any line or expression that you modify unnecessarily will lead to negative marking
that will deduct 2 points for each such modified instance.
• For each blank, fill in the suitable code involving one and only one statement or expression.
Original Code Update Code (only required lines)
#include <iostream>
#include < >
using namespace std;
int main () {
rand (-1);
long int N = 1e8;
double average = 0;
while (N −− > 0) {
const double temp = rand() / RAND_MAX;
unsigned char draw = 0;
if ( ) draw = 1;
if ( ) draw = 2;
if ( ) draw = 3;
if ( ) draw = 4;
if ( ) draw = 5;
if ( ) draw = 6;
average = average + draw;
}
average = average / N;
cout.precision (10);
cout << average << endl;
}
8
5. [15 points]
In the following code, each line may have a single error or may have some blanks that need to
be filled to avoid compilation errors, logical errors, or other memory-related issues. The intention
of the class and associated functions should be clear from the code in the main() function.
For each line, indicate whether any modification is needed, and if so, what the modification is.
You cannot remove any existing type of statement (e.g., loop or return or assignment) from the
program. Any line or expression that you modify unnecessarily will lead to negative marking
that will deduct 2 points for each such modified instance.
For each blank, fill in the suitable code involving one and only one statement or expression.
Assume that the “new” command always executes successfully. Also note that the main program
is correct and there are no errors.
~Vector()
{ } // 1 and only 1 statement
template<class T>
void Vector<T>::copy (const Vector<T>& v)
{
const unsigned int num =
(size < v.size) ? size : v.size;
for (unsigned int i = 0; i < num; i++)
v.data[i] = data[i];
}
9
template<class T>
Vector<T>& Vector<T>::operator=
(const Vector<T>& v) const
{
// 1 and only 1 statement
data = new T[v.size];
v.size = size;
copy (v);
// 1 and only 1 statement
}
int main () {
Vector<int> v(10);
for (int i = 0; i < v.length(); i++) v[i] = i;
for (int i = 0; i < v.length(); i++) cout << v[i] << " "; cout << endl;
cout << "--------------------"<< endl;
Vector<int> w(v);
for (int i = 0; i < w.length(); i++) cout << w[i] << " "; cout << endl;
cout << "--------------------"<< endl;
Vector<int> x;
for (int i = 0; i < x.length(); i++) x[i] = -i;
for (int i = 0; i < x.length(); i++) cout << x[i] << " "; cout << endl;
cout << "--------------------"<< endl;
v = w = x;
for (int i = 0; i < v.length(); i++) cout << v[i] << " "; cout << endl;
for (int i = 0; i < w.length(); i++) cout << w[i] << " "; cout << endl;
cout << "--------------------"<< endl;
Vector<float> y(3);
for (int i = 0; i < y.length(); i++) cout << y[i] << " "; cout << endl;
Vector<float> z(0);
for (int i = 0; i < z.length(); i++) cout << z[i] << " "; cout << endl;
z = y;
for (int i = 0; i < z.length(); i++) cout << z[i] << " "; cout << endl;
}
10