Programming in Modern C++ | Week 9
Session: JULY-DEC 2023
Course Name: Programming in Modern C++
Course Link: Click Here
These are Nptel Programming in Modern C++ Assignment 9 Answers
Programming
Question 1
Consider the following program that finds the minimum element in an array. Fill in the blanks
as per the instructions given below:
• Fill in the blank at LINE-1 with appropriate template declaration.
• Fill in the blanks at LINE-2 and LINE-3 with appropriate conditional statement.
The program must satisfy the given test cases.
Solution:
#include<iostream>
template<typename Itr, typename T>
void min(Itr first, Itr last, T& mv)
{
mv = *first++;
while (first != last)
{
if(*first < mv)
mv = *first;
++first;
}
}
These are Nptel Programming in Modern C++ Assignment 9 Answers
Question 2
Consider the following program that takes inputs a lower limit (l) and an upper limit (u) of
a vector. If all the elements of the input vector are within the given lower and upper limits,
the program prints “all in [l, u]”. Otherwise, the program prints the first element of the vector
which is not within the given limits. Fill in the blanks as per the instructions given below:
• at LINE-1 with appropriate header to overload function operator,
• at LINE-2 with appropriate template definition,
• at LINE-3 with appropriate condition,
such that the program will satisfy the given test cases.
Solution:
#include<iostream>
#include<vector>
struct Bound
{
Bound(int l, int u) : l_(l), u_(u){}
bool operator()(int n) const
{
return (n >= l_ && n <= u_);
}
int l_, u_;
};
template <typename Itr, typename Pred>
Itr search(Itr first, Itr last, Pred bd)
{
while (first != last)
{
if(!bd(*first))
return first;
++first;
}
return first;
}
These are Nptel Programming in Modern C++ Assignment 9 Answers
Question 3
Consider the following program, which computes the frequency of occurrence (histogram) of
each integer in a given vector. Fill in the blanks as per the instructions given below:
• at LINE-1 with appropriate statement to iterate over the given vector v,
• at LINE-2 with appropriate statement to iterate over the given map hi,
such that it will satisfy the given test cases.
Solution:
#include <iostream>
#include <map>
#include <vector>
std::map<int, int> histo(std::vector<int> v)
{
std::map<int, int> hi;
for (std::vector<int>::iterator it = v.begin(); it != v.end(); ++it)
hi[*it]++;
return hi;
}
void print(std::map<int, int> hi)
{
for (std::map<int, int>::iterator it = hi.begin(); it != hi.end(); ++it)
std::cout << it->first << ": " << it->second << ", ";
}
These are Nptel Programming in Modern C++ Assignment 9 Answers
More Weeks of Programming in Modern C++: Click here
More Nptel Courses: Click here
Course Name: Programming in Modern C++
Course Link: Click Here
These are Nptel Programming in Modern C++ Assignment 9 Answers
Quiz
Q1. Consider the program given below.
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) 101 65 41 A
Q2. Consider the program given below.
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) It copies the first line of file in. txt to file out. txt.
These are Nptel Programming in Modern C++ Assignment 9 Answers
Q3. Consider the following code segment.
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) !infile.is_open()
These are Nptel Programming in Modern C++ Assignment 9 Answers
Q4. Consider the following code segment.
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, kiArr([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
These are Nptel Programming in Modern C++ Assignment 9 Answers
Q5. Consider the following code segment.
What will be the output?
a) 00003.33333
b) 00003.3333
c) 0000000003.33333
d) 0000000003.00000
Answer: b) 00003.3333
These are Nptel Programming in Modern C++ Assignment 9 Answers
Q6. Consider the following code segment.
What will be the output?
a) 123456789
b) 678900000
c) 000006789
d) 000012345
Answer: c) 000006789
These are Nptel Programming in Modern C++ Assignment 9 Answers
Q7. Consider the following program to compute the inner product between the integers
in a vector vi and a list li.
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, operationi, operation2
b) 1li.begin(), 1li.end(), vi.begin(), 0, operationi, operation2
c) li.begin(), li.end(), vi.begin(), 0, operation2, operationi
d) vi.begin(), vi.end(), 1li.begin(), 0, operation2, operationi
Answer: a, b
Q8. Consider the following code segment.
Choose the appropriate option to fill in the balnk at LINE-1 so that it prints the values from list 11 which are divisible by 4. So the output should he 8 4
a) first != last || !pred(*first)
b) first != last &k !pred(*first)
c) first != last &k pred(*first)
d) first != last || pred(first)
Answer: b) first != last &k !pred(*first)
These are Nptel Programming in Modern C++ Assignment 9 Answers
Q9. Consider the following code segment.
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
10 : B
20 : C
Answer: b) 20 : C
10 : B
40 : B
30 : A
Programming Assignment of Programming in Modern C++
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.
Solution:
#include<iostream>
#include<algorithm>
#include<vector>
struct Compute : std::unary_function < double, void >{
int count;
double sum;
Compute() : count(0), sum(0.0) {}
void operator()(double x){count++; sum+=x;}
};
These are Nptel Programming in Modern C++ Assignment 9 Answers
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.
Solution:
struct total_expense
{
double operator()(double d, purchase& p)
{
return d + p.get_price() * p.get_qty();
}
};
double get_total_expense (std::list<purchase> li, total_expense tc)
{
double total = accumulate(li.begin(), li.end(),0.0,tc);
return total;
}
These are Nptel Programming in Modern C++ Assignment 9 Answers
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.
Solution:
for (auto it = str.begin(); it != str.end(); ++it)
if(*it == 'a' || *it == 'e' || *it == 'i' || *it == 'o'|| *it == 'u')
vFreq[*it]++;
return vFreq;
}
void print(std::map<char, int> vFreq){
for (auto it = vFreq.begin(); it != vFreq.end(); ++it)
These are Nptel Programming in Modern C++ Assignment 9 Answers
More Solutions of Programming in Modern C++: Click Here
More NPTEL Solutions: http://13.235.254.184/nptel/
