Programming in Modern C++ | Week 9

Session: JAN-APR 2024

Course Name: Programming in Modern C++

Course Link: Click Here

For answers or latest updates join our telegram channel: Click here to join

These are Nptel Programming in Modern C++ Assignment 9 Answers


Quiz

Q1. Consider the following program.
What will be the output/error?
a) 43 43 43 43
b) 67 103 43 C
c) 67 103 43 67
d) Compiler error at LINE-1: type cast from int -> char is invalid

Answer: b) 67 103 43 C


Q2. Consider the code segment given below.
Choose the correct statement about execution of the given code.

a) It copies all the lines from a.txt to b.txt.
b) It copies all the lines from a.txt to b.txt; however, merge all the lines to a single line.
c) It copies all newline characters of a.txt to b.txt.
d) It copies all lines except the last line of a.txt to b.txt.

Answer: b) It copies all the lines from a.txt to b.txt; however, merge all the lines to a single line.


For answers or latest updates join our telegram channel: Click here to join

These are Nptel Programming in Modern C++ Assignment 9 Answers


Q3. Consider the following code segment.
What will be the output?

a) 3.333333
b) 3.333000
c) 0003.333
d) 003.3333

Answer: c) 0003.333


Q4. Consider the code given below that verifies existence of a file myfile.txt in given path.
Identify the appropriate option to fill in the blank at LINE-1 such that it checks if the file does. not exist.

a) fl.is_open()
b) !fl.is_open()
c) !fl.open()
d) fopen(fl) == NULL

Answer: b) !fl.is_open()


For answers or latest updates join our telegram channel: Click here to join

These are Nptel Programming in Modern C++ Assignment 9 Answers


Q5. Match the appropriate descriptions about the fseek function calls.
Here, infp is a file pointer pointing to the beginning of a file in read-write mode.

a) 1-C, 2-A, 3-B, 4-D
b) 1-C, 2-B, 3-A, 4-D
c) 1-B, 2-C, 3-D, 4-A
d) 1-B, 2-D, 3-C, 4-A

Answer: c) 1-B, 2-C, 3-D, 4-A


Q6. Consider the following code segment.
Identify the appropriate call to copy function to fill in the blank at LINE-1 such that the output is:
345

a) std::copy (ai 3, ai 6, li.begin())
b) std::copy (ai+2, ai 5, li.begin())
c) std::copy (&ai [0], &ai [6], li.begin() + 3)
d) std::copy (&ai [2], &ai [5], li.begin())

Answer: b), d)


For answers or latest updates join our telegram channel: Click here to join

These are Nptel Programming in Modern C++ Assignment 9 Answers


Q7. Consider the code segment below that computes the sum of difference between two vectors.
Choose the correct option to fill in the blank at LINE-1 such that the output is 60.

a) std::inner_product(v1.begin(), vi.end(), v2.begin(), operation, init, operation2)
b) std::inner_product (v1.begin(), vi.end(), v2.begin(), init, operation1, operation2)
c) std::inner_product(v1.begin(), vi.end(), v2.begin(), init, operation2, operation1)
d) std::inner_product (v1.begin(), vi.end(), v2.begin(), init, operation2 (operation1))

Answer: c) std::inner_product(v1.begin(), vi.end(), v2.begin(), init, operation2, operation1)


Q8. Consider the following code segment.
Identify the correct function header(s) for findmin to fill in the blank at LINE-1 such that the program finds out the minimum element of the vector vi and the output is 3: 1.

a) int findmin (Itr first, Itr last, T mval)
b) int findmin (Itr first, Itr last, T& mval)
c) int findmin(T first, T last, Itr& mval)
d) int findmin (Itr first, T last, T& mval)

Answer: b), c)


For answers or latest updates join our telegram channel: Click here to join

These are Nptel Programming in Modern C++ Assignment 9 Answers


Q9. Consider the code segment below.
What will be the output?

a) 0
b) 600
c) 120000
d) 240000

Answer: c) 120000


For answers or latest updates join our telegram channel: Click here to join

These are Nptel Programming in Modern C++ Assignment 9 Answers


Programming

Question 1

Consider the following program which takes name and rank of some players, followed by the type of sorting (1 for sort by the length of name and 2 for sort by rank) as input. It prints the players’ information in the corresponding sorted order. Complete the program with the following instructions.
• Fill the missing code segments at code segment-1 and code-segment-2 with the ap- propriate overriding of function operator.
The program must satisfy the sample input and output.

Solution:

struct cmpByName{
  bool operator()( player& e1,  player& e2) {
    return e1.get_name().length() < e2.get_name().length();
  }
};
 
struct cmpByRank{
  bool operator()( player& e1,  player& e2) {
    return e1.get_rank() < e2.get_rank();
  }
};

For answers or latest updates join our telegram channel: Click here to join

These are Nptel Programming in Modern C++ Assignment 9 Answers


Question 2

Consider the following program, which takes 10 numbers as input, and computes the frequency of each number. Fill in the blanks as per the instructions given below:
• at LINE-1 with appropriate statement to itcrate over the given list 11,
• at LINE-2 with appropriate statement to iterate over the given map fq.
such that it will satisfy the given test cases
.

Solution:

#include <iostream>
#include <map>
#include <list>
 
std::map<int, int> findFrequency(std::list<int> li){
    std::map<int, int> fqMap; 
    for ( std::list<int>::iterator it = li.begin(); it != li.end(); ++it)   //LINE-1
        fqMap[*it]++;
    return fqMap;
}
 
void print(std::map<int, int> fq){
    for (std::map<int, int>::iterator it = fq.begin(); it != fq.end(); ++it)    //LINE-2
        std::cout << it->first << " => " << it->second << ", ";
}

For answers or latest updates join our telegram channel: Click here to join

These are Nptel Programming in Modern C++ Assignment 9 Answers


Question 3

Consider the following program that filters the elements from a given list li within a given upper and lower bounds and prints them.
• Fill in the blanks at LINE-1 with appropriate template declaration.
• Fill in the blanks at LINE-2 with an appropriate condition of the while loop.
• Fill in the blanks at LINE-3 with an appropriate call to function find if such that it returns an iterator object to the first element from li that match the given predicate with upper and lower bounds.
The program must satisfy the sample input and output.

Solution:

template<class T, class Pred>                         //LINE-1
T find_if(T first, T last, Pred prd) {
    while (first != last && !prd(*first)) ++first;         //LINE-2
        return first;
}
 
void print(std::list<int> li, int lb, int ub){
    boundIt<int> bnd(lb, ub);
    std::list<int>::iterator it = find_if(li.begin(), li.end(), bnd);    //LINE-3
    while(it != li.end()){
        std::cout << *it << " ";
        it = find_if(++it, li.end(), bnd);
    }
}

For answers or latest updates join our telegram channel: Click here to join

These are Nptel Programming in Modern C++ Assignment 9 Answers

More Weeks of Programming in Modern C++: Click here

More Nptel Courses: Click here


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: https://progiez.com/nptel-assignment-answers/


These are Nptel Programming in Modern C++ Assignment 9 Answers
The content uploaded on this website is for reference purposes only. Please do it yourself first.
Home
Account
Cart
Search