Programming in Modern C++ | Week 11

Session: JULY-DEC 2023

Course Name: Programming in Modern C++

Course Link: Click Here

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


Programming

Question 1
Consider the following program (in C++11).
• Fill in the blanks at LINE-1 and LINE-3 with appropriate template definitions.
• Fill in the blanks at LINE-2 and LINE-4 to complete the return statements for product
functions.
The program must satisfy the sample input and output.

Solution:

#include <iostream>
template <typename T>
double product(T num){ return num; }
template <typename T, typename... Tail>
double product(T num, Tail... nums){
    return num * product(nums...);
}

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


Question 2
Consider the program below (in C++11).
• Fill in the blank at LINE-1 with appropriate template declaration.
• Fill in the blanks at LINE-2 with an appropriate universal reference type parameter for constructor of class derived and an the appropriate call forwarding to the base class constructor.
The program must satisfy the given test cases.

Solution:

class derived : public base {
    public:
        template<typename T>
        derived(T&& n) : base(std::forward<T>(n)) { }
        void show(){ std::cout << n_ << " "; }
};

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


Question 3
Consider the following program that implements a recursive lambda function to find the sum of the digits of an input integer.
• Fill in the blank at LINE-1 to declare the signature of revPrint as std::function.
• Fill the blank at LINE-2 to complete the definition of lambda function revPrint.
The program must satisfy the sample input and output.

Solution:

#include<iostream>
#include<functional>

int main() {
    std::function<int(int)> revPrint;
    revPrint = [&revPrint](int n) -> int {
        if (n == 0)
            return 0;
        return n % 10 + revPrint(n /= 10);
    };

These are Nptel Programming in Modern C++ Assignment 11 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 11 Answers


Quiz

Q1. Consider the program (in C++11) given below.
Choose the appropriate option to fill in the blank at LINE-1 so that the output becomes

base – lvalue : 10
base – rvalue : 20

a) LINE-1: a1_(n1), a2_(n2)
b) LINE-1: a1_(std::forward< T1 >(n1)), a2_(std::forward< U >(n2))
c) LINE-1: a1_(n1), a2 (std::forward< U >(n2))
d) LINE-1: a1_(std::forward< T1 >(n1)), a2_(n2)

Answer: b, c


Q2. Consider the code segment (in C++11) given below.
Identify the line/s where && indicates a universal reference.

a) LINE-1
b) LINE-2
c) LINE-3
d) LINE-4

Answer: b


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


Q3. Consider the following code segment (in C++11).
Identify the statement /s which are true for the given program.

a) It generates compiler error at LINE-1
b) It generates compiler error at LINE-2
c) It generates compiler error at LINE-3
d) It generates the output true

Answer: b, c


Q4. Consider the following code segment.
Identify the appropriate code block to be placed at code-block-1 such that the output is 60
50 40 20 10 .

a) bool compare(int a, int b){ return a > b; }
b) struct compare{
bool operator() (int a, int b){ return a > b; }
}
c) class compare{
bool operator() (int a, int b){ return a > b; }
}
d) bool operator() (int a, int b){ return a > b; }

Answer: b) struct compare{
bool operator() (int a, int b){ return a > b; }
}


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


Q5. Consider the following code segment (in C++11).
Which of the following line/s generate/s compiler error/s?

a) LINE-1
b) LINE-2
c) LINE-3
d) LINE-4

Answer: a, c


Q6. Consider the following code segment (in C++11).
What will be the output?

a) #1
b) #2 #1
c) #4 #2 #1
d) #1 #2 #4

Answer: c) #4 #2 #1


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


Q7. Consider the following program (in C++14).
What will be the output?

a) 254, 254
b) 10000, 10000
c) 10000, 254
d) 254, 10000

Answer: c) 10000, 254


Q8. Consider the following A expression (in C++11).
Identify the most appropriate option that define the equivalent closure object for the above lambda function.

a)
b)
c)
d)

Answer: d)


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


Q9. Consider the following code segment (in C++11).
Which of the following lines will generate compiler error/s?

a) LINE-1
b) LINE-2
c) LINE-3
d) LINE-4

Answer: a


Programming Assignment of Programming in Modern C++

Question 1

Consider the following program (in C++11).
• Fill in the blanks at LINE-1 and LINE-3 with appropriate template definitions.
• Fill in the blanks at LINE-2 and LINE-4 with appropriate parameters for findMax function.
The program must satisfy the sample input and output.

Solution:

#include <iostream>

template<typename T>

double findMax(T num){ return num; }

template<typename T, typename... Args>

double findMax(T num, Args... nums){

    return num >= findMax(nums...) ? num : findMax(nums...);
}

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


Question 2

Consider the following program (in C++11). Fill in the blanks as per the instructions given below:
• Fill in the blank at LINE-1 with an appropriate template declaration for the function wrapper.
• Fill in the blank at LINE-2 with an appropriate header for function wrapper.
• Fill in the blank at LINE-3 with an appropriate return statement for function wrapper.
The program must satisfy the sample input and output.

Solution:

template<typename T, typename U, typename V>
  double wrapper (V&& v, Summation<T, U> s, const std::vector<T>& v1, const std::vector<U>& v2){
  return s(v, v1, v2);
}
template<typename T, typename U, typename V>
  double wrapper (V&& v, Summation<T, U> s, std::vector<T>&& v1, std::vector<U>&& v2){
  return s(v, std::move (v1), std::move (v2));
}

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


Question 3

Consider the following program (in C++11) to find factorials of 3 integers. Fill in the blanks as per the instructions given below:
• Fill in the blank at LINE-1 to complete the λ function for computing factorial.
• Fill in the blank at LINE-2 to complete the λ function for printing the vector v2.
The program must satisfy the sample input and output.

Solution:

for (auto i : v1)
{
  long long factorial = 1;
  for (int j = 1; j <= i; j++)
  {
    factorial *= j;
  }
  v2.push_back (factorial);
}
for (auto i : v2)
{
  std::cout << i << " ";
}
return 0;
}

These are Nptel Programming in Modern C++ Assignment 11 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 11 Answers
The content uploaded on this website is for reference purposes only. Please do it yourself first.
Home
Account
Cart
Search