0% found this document useful (0 votes)
15 views

Assignment 9 Solution

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Assignment 9 Solution

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Programming in Modern C++: Assignment Week 9

Total Marks : 27

Partha Pratim Das


Department of Computer Science and Engineering
Indian Institute of Technology Kharagpur, Kharagpur – 721302
[email protected]

March 21, 2023

Question 1
Consider the program given below. [MCQ, Marks 2]

#include <iostream>
#include <iomanip>
using namespace std;

int main () {
int i = 65;
cout << setbase(8)<< i << " ";
cout << setbase(10)<< i << " ";
cout << setbase(16)<< i << " ";
cout << static_cast<char>(i) << endl;
return 0;
}

What will be the output?

a) 65 65 65 65

b) 65 65 65 A

c) 101 65 41 65

d) 101 65 41 A

Answer: d)
Explanation:
The function setbase(b) sets the base of an integer as b. Therefore, the statmenet:
cout << setbase(8)<< i << " ";
prints the value of i as octal (base 8) value.
The statmenet:
cout << setbase(10)<< i << " ";
prints the value of i as decimal (base 10) value.
The statmenet:
cout << setbase(16)<< i << " ";
prints the value of i as octal (base 16) value.
Finally, the statement static cast<char>(i) type-casts i to integer.

1
Question 2
Consider the program given below. [MCQ, Marks 2]

#include<cstdio>
using namespace std;
int main(){
FILE *infp, *outfp;
int c;
if((infp = fopen("in.txt", "r")) == NULL)
return -1;
if((outfp = fopen("out.txt", "w")) == NULL)
return -2;
while((c = fgetc(infp)) != EOF && c != '\n')
fputc(c, outfp);
fclose(infp);
fclose(outfp);
return 0;
}

Choose the statement that is true about the output of the program.

a) It copies the entire content file in.txt to file out.txt.

b) It copies the content file in.txt to file out.txt without the newlines (i.e. the entire content
of in.txt would be copied to a single line of put.txt.

c) It copies the first line of file in.txt to file out.txt.

d) It copies the content file in.txt to file out.txt if file in.txt has only one line; otherwise,
it generates an error in runtime.

Answer: c)
Explanation:
The while loop in the program gets terminated either when fgetc(infp) return EOF or c ==
’\n’ . Therefore, if the file in.txt has only one line, the EOF will be encountered at the end of
the line. In case of multiple lines, it reads the first line then encounter a ’\n’, and terminates.

2
Question 3
Consider the following code segment. [MCQ, Marks 2]

#include <iostream>
#include <fstream>

int main () {
std::ifstream infile("input.txt");
std::string line;
if (___________________________) { //LINE-1
std::cout << "file does not exists";
}
else{
while (getline(infile, line))
std::cout << line << std::endl;
infile.close();
}
return 0;
}

Choose the appropriate option to fill in the blank at LINE-1 such that it checks if the file
input.txt does not exist.

a) infile.is open()

b) !infile.is open()

c) !infile.open()

d) fopen(infile) == NULL

Answer: b)
Explanation:
Since the program attempts to read from the file, if the file exists or not can be verified by
is open() function. Thus, the correct option is b).

3
Question 4
Consider the following code segment. [MSQ, Marks 2]

#include<iostream>

template<class Itr, class T>


int findmax(Itr first, Itr last, T& mval) {
int maxpos = 0, i = 0;
mval = *first++;
while (first != last) {
if(*first > mval){
mval = *first;
maxpos = i + 1;
}
++first;
++i;
}
return maxpos;
}

int main(){
int iArr[] = { 3, 2, 6, 1, 6, 8, 7};
double mVal = 0.0;
________________________________________________; //LINE-1
std::cout << pos << ", " << mVal;
return 0;
}

Choose the appropriate options to fill in the blank at LINE-1 such that the program finds out
the maximum element of the array iArr and the output is 5, 8.

a) findmax(iArr, iArr + sizeof(iArr) / sizeof(*iArr), mVal)

b) int pos = findmax(iArr, &iArr[sizeof(iArr) / sizeof(*iArr)], mVal)

c) int mVal = findmax(iArr, iArr + sizeof(iArr) / sizeof(*iArr), mVal)

d) int pos = findmax(iArr, iArr + sizeof(iArr) / sizeof(*iArr), mVal)

Answer: b), d)
Explanation:
The formal parameters of the function findmax are as follows:

• first – the base address of the array, which is iArr

• last – the end address of the array, which is iArr + sizeof(iArr) / sizeof(*iArr)
or &iArr[sizeof(iArr) / sizeof(*iArr)]

• mval – a reference variable to store the maximum element of the array, which is mVal.

And it returns the index of the maximum element. Therefore, option b) and c) are the correct
options.

4
Question 5
Consider the following code segment. [MCQ, Marks 2]

#include <iostream>
#include <iomanip>
using namespace std;
int main () {
cout << setprecision(5) << setfill('0') << setw(10) << 10/3.0;
return 0;
}

What will be the output?

a) 00003.33333

b) 00003.3333

c) 0000000003.33333

d) 0000000003.00000

Answer: b)
Explanation:
10/3.0 = 3.3333...
setprecision(5) makes it to ]tt 3.3333
setfill(’0’) and setw(10) make it to 00003.3333

5
Question 6
Consider the following code segment (in C++11). [MCQ, Marks 2]

#include <iostream>
#include <algorithm>
#include <vector>
#include <list>

int main() {
std::list<int> li= { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
std::vector<int> vi(li.size());
std::list<int>::iterator it1 = li.begin();
std::vector<int>::iterator it2 = vi.begin();
for(int i = 0; i < 5; i++){ //LINE-1
it1++; it2++;
}

copy(it1, li.end(), it2);

for(it2 = vi.begin(); it2 != vi.end(); ++it2)


std::cout << *it2;
return 0;
}

What will be the output?

a) 123456789

b) 678900000

c) 000006789

d) 000012345

Answer: c)
Explanation:
The for loop at LINE-1 sets it1 and it2 at the fifth position of li and vi, respectively.
Therefore, the copy function copes the last 4 elements from li to last 4 positions of vi.

6
Question 7
Consider the following program (in C++11) to compute the inner product between the integers
in a vector vi and a list li. [MSQ, Marks 2]

#include <iostream>
#include <list>
#include <vector>
#include <numeric>

int operation1(int i, int j){ return i + j; }


int operation2(int i, int j){ return i * j; }

int main() {
std::vector<int> vi { 1, 2, 3, 4, 5 };
std::list<int> li { 50, 40, 30, 20, 10 };

int n = inner_product(______________________________); //LINE-1


std::cout << n;
return 0;
}

Choose the correct option to fill in the blank at LINE-1 so that output becomes 350.

a) vi.begin(), vi.end(), li.begin(), 0, operation1, operation2

b) li.begin(), li.end(), vi.begin(), 0, operation1, operation2

c) li.begin(), li.end(), vi.begin(), 0, operation2, operation1

d) vi.begin(), vi.end(), li.begin(), 0, operation2, operation1

Answer: a), b)
Explanation:
The implementation of inner product function is similar to:

template<class In, class In2, class T, class BinOp, class BinOp2 >
T inner_product(In first, In last, In2 first2, T init, BinOp op1, BinOp2 op2) {
while(first!=last) {
init = op1(init, op2(*first, *first2));
++first; ++first2;
}
return init;
}

Thus, a) and b) both are the correct options.

7
Question 8
Consider the following code segment (in C++11). [MCQ, Marks 2]

#include<iostream>
#include<list>

struct divisible{
int d_;
divisible(int d = 1) : d_(d) { }
bool operator()(int i){ return (i % d_ == 0); }
};

template<class T, class P>


T find_if(T first, T last, P pred) {
while (_________________________) ++first; //LINE-1
return first;
}

void print(std::list<int> li, int d){


divisible divi(d);
std::list<int>::iterator it = find_if(li.begin(), li.end(), divi); //LINE-3
while(it != li.end()){
std::cout << *it << " ";
it = find_if(++it, li.end(), divi);
}
}

int main(){
std::list<int> li {7, 8, 1, 4, 2, 5, 6, 3};
int d;
print(li, 4);
return 0;
}

Choose the appropriate option to fill in the balnk at LINE-1 so that it prints the values from
list li which are divisible by 4. So the output should be 8 4

a) first != last || !pred(*first)

b) first != last && !pred(*first)

c) first != last && pred(*first)

d) first != last || pred(first)

Answer: b)
Explanation:
At LINE-1, the while loop condition must be:
first != last && !pred(*first)
such that the loop stops either when it reaches the end of the list or the given predicate is
satisfied.

8
Question 9
Consider the following code segment (in C++11). [MCQ, Marks 2]

#include<iostream>
#include<algorithm>
#include<vector>

class student{
public:
student(int roll, char grade) : roll_(roll), grade_(grade){}
int get_roll(){ return roll_; }
char get_grade(){ return grade_; }
private:
int roll_;
char grade_;
};

struct comparator{
bool operator()(student s1, student s2){
if(s1.get_grade() == s2.get_grade())
return s1.get_roll() < s2.get_roll();
return s1.get_grade() > s2.get_grade();
}
};
int main() {
student s[] = { student(30, 'A'), student(10, 'B'),
student(20, 'C'), student(40, 'B') };
std::vector<student> s_list(s, s + sizeof(s) / sizeof(*s));
std::sort(s_list.begin(), s_list.end(), comparator());
for(std::vector<student>::iterator it = s_list.begin(); it != s_list.end(); it++)
std::cout << it->get_roll() << " : " << it->get_grade() << std::endl;
return 0;
}

What will be the output?

a) 30 : A
10 : B
40 : B
20 : C

b) 20 : C
10 : B
40 : B
30 : A

c) 20 : C
40 : B
10 : B
30 : A

d) 30 : A
40 : B

9
10 : B
20 : C

Answer: b)
Explanation:
As per the functor comparator, the vector s list must be first sorted by the grade in de-
scending order. In case grades are equal, the vector will be sorted by roll in ascending order.
Therefore, the correct option is b).

10
Programming Questions

Question 1
Consider the following program.

• Fill in the blank at LINE-1 to inherit from unary function.

• Fill in the blank at LINE-2 with appropriate initializer list.

• Fill in the blank at LINE-3 to override function call operator.

The program must satisfy the sample input and output. Marks: 3

#include<iostream>
#include<algorithm>
#include<vector>

struct Compute : ___________________________ { //LINE-1


int count;
double sum;
Compute() : __________________________ {} //LINE-2
__________________________________________ //LINE-3
};

int main(){
std::vector<double> vd;
int n;
double d;
std::cin >> n;
for(int i = 0; i < n; i++){
std::cin >> d;
vd.push_back(d);
}
Compute com = for_each(vd.begin(), vd.end(), Compute());
std::cout << "avg = " << com.sum / com.count;
return 0;
}

Public 1
Input: 5 1.2 2.3 3.4 4.5 5.6
Output: avg = 3.4

Public 2
Input: 6 4.3 -9 -1.3 5.4 2.3 4.3
Output: avg = 1

Private
Input: 7 1 2 3 4 5 6 8
Output: avg = 4.14286

11
Answer:
LINE-1: public std::unary function<double, void>
LINE-2: count(0), sum(0.0)
LINE-3: void operator() (double x) { sum += x; ++count; }
Explanation:
The functor Compute inherits the unary function as:
struct Compute : public std::unary function<double, void>
,since it accepts an int as input as double without any return.
The constructor must initialize sum = 0.0 and count = 0. Thus, it must be:
Compute() : count(0), sum(0.0) {}
The function call operator must be overloaded to find the count of the elements of the array,
and to get the sum of the elements. Thus, it must be:
void operator() (double x) { sum += x; ++count; }

12
Question 2
Consider the following program (in C++11) that considers a number of purchases as input
and find out the total expense. Fill in the blanks as per the instructions given below:

• Fill in the blank at LINE-1 with appropriate header to overload function operator.

• Fill in the blank at LINE-2 with an appropriate return statement to implement the
overload function operator.

• Fill in the blank at LINE-3 with the appropriate parameter list for calling the function
accumulate,

The program must satisfy the sample input and output. Marks: 3

#include <iostream>
#include <list>
#include <numeric>

class purchase{
public:
purchase(int item_no, double price, int qty) : item_no_(item_no),
price_(price), qty_(qty){}
double get_price(){ return price_; }
int get_qty(){ return qty_; }
private:
int item_no_;
double price_;
int qty_;
};

struct total_expense{
______________________________________ { //LINE-1
__________________________________; //LINE-2
}
};

double get_total_expense(std::list<purchase> li, total_expense tc){


double total = accumulate(__________________________); //LINE-3
return total;
}

int main(){
int n, a, b;
double c;
std::list<purchase> li_pur;
std::cin >> n;
for(int i = 0; i < n; i++){
std::cin >> a >> c >> b; //read item_no, price and quantity
purchase od(a, c, b);
li_pur.push_back(od);
}
total_expense tc;
std::cout << get_total_expense(li_pur, tc);

13
return 0;
}

Public 1
Input:
2
101 50.0 4
102 100.5 5
Output: 702.5

Public 2
Input:
4
101 20.0 10
102 50.5 10
103 100.0 1
104 33.0 5
Output: 970

Private
Input:
3
101 50.5 3
102 40.5 5
103 10.5 3
Output: 385.5

Answer:
LINE-1: double operator()(double d, purchase p)
LINE-2: return d + p.get price() * p.get qty()
LINE-3: li.begin(), li.end(), 0.0, tc
Explanation:
The header to overload function operator can be:
double operator()(double d, purchase p)
The implementation of function operator at LINE-2 can be written as:
return d + p.get price() * p.get qty()
At LINE-3, the function accumulate call can be made as:
double total = accumulate(li.begin(), li.end(), 0.0, tc);

14
Question 3
Consider the following program (in C++11), which finds the frequency of each vowel in a given
string. Fill in the blanks as per the instructions given below:
• Fill in the blank at LINE-1 with an appropriate statement to iterate over the given string
str.
• Fill in the blank at LINE-2 with conditional statement to extract the vowels.
• Fill in the blank at LINE-3 with an appropriate statement to iterate over the given map
vFreq, and print it.
The program must satisfy the sample input and output. Marks: 3
#include <iostream>
#include <map>
#include <string>

std::map<char, int> findVowelFrequency(std::string str){


std::map<char, int> vFreq;
for (________________________________________) //LINE-1
if(______________________________________) //LINE-2
vFreq[*it]++;
return vFreq;
}

void print(std::map<char, int> vFreq){


for (_______________________________________) //LINE-3
std::cout << it->first << " -> " << it->second << std::endl;
}

int main() {
std::string s;
std::cin >> s;
std::map<char, int> vFreq = findVowelFrequency(s);
print(vFreq);
return 0;
}

Public 1
Input: hippopotamuses
Output:
a -> 1
e -> 1
i -> 1
o -> 2
u -> 1

Public 2
Input: vowels
Output:
e -> 1
o -> 1

15
Private
Input: multimillionaire
Output:
a -> 1
e -> 1
i -> 4
o -> 1
u -> 1

Answer:
LINE-1: std::string::iterator it = str.begin(); it != str.end(); ++it
LINE-2: *it == ’a’ || *it == ’e’ || *it == ’i’ || *it == ’o’ || *it == ’u’
LINE-3: std::map<char, int>::iterator it = vFreq.begin(); it != vFreq.end(); ++it
Explanation:
The std::string::iterator type can be used to iterate through the given string str, so the
statement at LINE-1 must be
for (std::string::iterator it = str.begin(); it != str.end(); ++it)
The conditional statement for vowels can be:
if(*it == ’a’ || *it == ’e’ || *it == ’i’ || *it == ’o’ || *it == ’u’)
Similarly, the std::map<char,int>::iterator type can be used to iterate through the given
map vFreq, so the statement at LINE-3 must be
for (std::map<char, int>::iterator it = vFreq.begin(); it != vFreq.end(); ++it)

16

You might also like