Programming in Modern C++ | Week 12

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 12 Answers


Quiz

Q1. Which of the following smart pointers has/have shared ownership policy?
a) std::auto_ptr (C++03)
b) std:: weak_ptr (C++11)
c) std::unique_ptr (C++11)
d) std::shared_ptr (C++11)

Answer: b), d)


Q2. Consider the program (in C++11) given below.
What will be the output?

a) 1
b) -4
c) -5
d) -13

Answer: a) 1


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

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


Q3. Consider the program (in C++11) given below that squares each element of vector vec and sums all the squared elements.
Fill in the blank at LINE-1 with appropriate option(s) such that the output becomes 149 16 30.

a) summation (vec, res)
b) summation (vec, &res)
c) summation(std::ref (vec), &res)
d) std::bind(summation, std::ref (vec), &res)

Answer: b), c)


Q4. Consider the program (in C++11) given below.
What will be the output?

a) SmartPtr::ctor(), SmartPtr::operator(), SmartPtr::operator(),
b) SmartPtr:: ctor(), SmartPtr::operator(), SmartPtr::operator->()
c) SmartPtr::ctor(), SmartPtr::operator(), SmartPtr::operator* SmartPtr::operator->(),
d) SmartPtr:: ctor(), SmartPtr::operator->(), SmartPtr::operator->(),

Answer: b) SmartPtr:: ctor(), SmartPtr::operator(), SmartPtr::operator->()


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

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


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

a) rc 2 rc 3 rc 4 rc 4 rc 3
b) rc 2 rc 3 rc 4 rc = 3 rc = 2
c) rc 2 rc=2rc4 rc = 2 rc = 1
d) rc 2 rc=2 rc 3 rc = 2 rc = 1

Answer: c) rc 2 rc=2rc4 rc = 2 rc = 1


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

a) A B
b) wp1 is expired
wp2 is expired
c) A
wp2 is expired
d) a
wp2 is expired

Answer: d) a
wp2 is expired


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

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


Q7. Consider the code segment (C++11) given below.
What is/are the possible output?

a) 3 0 1
b) 0 1
c) 0 3
d) 0 3 1

Answer: a) 3 0 1


Q8. Consider the following code segment (in C++11).
Which of the following cannot be TRUE about the output of the above program?

a) It generates output as:
5, 5
13, 13
b) It generates output as:
8, 8
13, 13
c) It generates output as:
8, 8
5, 5
d) It results in deadlock

Answer: c), d)


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

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


Q9. Consider the code segment (C++11) given below.
What is/are the output/s that is/are not possible? (think logically)

a) consumtion by X : instances available = 7
consumtion by Z : instances available = 2
consumtion by Y : failed
b) consumtion by Z : failed
consumtion by X : instances available = 2
consumtion by Y : failed
c) consumtion by Z : instances available = 5
consumtion by X : instances available = 2
consumtion by Y : failed
d) consumtion by Z : instances available = 5
consumtion by X : instances available = 2
consumtion by Y : instances available = 1

See also  All About NPTEL

Answer: b), d)


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

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


Programming

Question 1

Consider the following program that implements a recursive lambda function to find out the n, where n, m 2 1.
Fill in the blank at LINE-1 to declare the signature of recPow as std::function.
Fill the blank at LINE-2 to complete the definition of lambda function recPow.
Fill the blank at LINE-3 to complete the definition of lambda expression print, which calls recPow.
The program must satisfy the sample input and output.

Solution:

long long operator()(){
        long long num = 2;
        for(int i = 0; i < n_; num++) {
            if (is_prime(num)) {
                ++i;
            }
        }
        return num - 1;
    }
};
long long get_nth_prime(int n){
    auto a = std::async(prime_numbers(n));
    return  a.get();
}

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

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


Question 2

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 getmin function.
The program must satisfy the sample input and output.

Solution:

class node{
    public:
        node() = delete;
        node(int info) : info_{info}, next{nullptr} {}
        friend dlist;
    private:
        int info_;
        std::shared_ptr<node> next;
        std::weak_ptr<node> prev;
};
void dlist::forward_traverse(){
    for(std::shared_ptr<node> t = first; t != nullptr; t = t->next)
        std::cout << t->info_ << " ";
}
void dlist::backward_traverse(){
    for(std::weak_ptr<node> t = last; auto p = t.lock(); t = p->prev)
        std::cout << p->info_ << " ";
}

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

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


Programming

Question 1
Consider the following program (in C++11).
   • Fill in the blank at LINE-1 with appropriate header to overload function operator.
   • Fill the blank at LINE-2 to invoke the functor Factorial() asynchronously.
   • Fill the blank at LINE-3 to receive the output from functor Factorial().
The program must satisfy the sample input and output.

Solution:

#include <iostream>
#include <future>

struct Factorial{
    Factorial(const long long& n) : n_( n) { }
    long long operator()() {     //LINE-1
        long long f = 1;
        if(n_ == 0 || n_ == 1)
            return 1;
        for(int i = 1; i <= n_; i++)
            f *= i;
        return f;
    }
    const long long n_;
};
long long callFacto(int n){
    auto a = std::async(Factorial(n));
    return a.get();
}

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


Question 2
Consider the following program in C++14 to represent a generic deque (double-ended-queue), which allows adding items at the front of the queue and at the end of the queue. Complete the program as per the instructions given below.
   • Fill in the blank at LINE-1 with appropriate statements so that after execution of the for loop, t refers to the last element of the deque.
   • Fill in the blank at LINE-2 with appropriate statements to traverse the list in forward direction.
   • fill in the blank at LINE-3 with appropriate statements to traverse the list in backward direction,
The program must satisfy the sample input and output. 

See also  Programming in Modern C++ Week 2 Assignment Answers

Solution:

void addEnd(const T1& item)
{
  std::shared_ptr<node<T1>> n = std::make_shared<node<T1>>(item);
  if(first == nullptr)
  {
    first = n;
    last = first;
  }
  else
  {
    std::shared_ptr<node<T1>> t = first;
    for(; t->next != nullptr; t = t->next);
    t->next = n;
    n->prev = t;
    last = n;
  }
}
void traverse()
{
  for(std::shared_ptr<node<T1>> t = first; t != nullptr; t = t->next)
    std::cout << t->info << " ";
}
void rev_traverse()
{
  for(std::shared_ptr<node<T1>> p = last; p != nullptr; p = p->prev.lock())
    std::cout << p->info << " ";
}
private:
std::shared_ptr<node<T1>> first { nullptr };
std::shared_ptr<node<T1>> last  { nullptr };
};
}

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


Quiz

Q1. Which of the following types of smart pointers follow /s shared ownership policy?
a) std: :auto_ptr
b) std: :unique_ptr
c) std: :std: :shared ptr
d) std: :weak ptr

Answer: c, d


Q2. Consider the code segment (in C++11) given below.
What will be the output?

a) SmartPtr::ctor(), MyClass::ctor(), MyClass::fun(), SmartPtr::operator->(),
b) MyClass::ctor(), SmartPtr::ctor(), SmartPtr::operator->(), MyClass::fun(),
c) SmartPtr::ctor(), MyClass::ctor(), SmartPtr::operator(), MyClass::fun(),
d) MyClass: :ctor(), SmartPtr::ctor(), SmartPtr::operator(), MyClass::fun(),

Answer: d) MyClass: :ctor(), SmartPtr::ctor(), SmartPtr::operator(), MyClass::fun(),


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


Q3. What will be the output?
a) -7 6
b) -8 6
c) -7 7
d) -8 16

Answer: a) -7 6


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

a) 22322
b) 22420
c) 23430
d) 23422

Answer: b) 22420


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


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

a) 1 1 expired expired
b) 1 0 1 0
c) 1 1 expired 1
d) 1 1 1 expired

Answer: a) 1 1 expired expired


Q6. Consider the following code segment (in C++11).
Choose the appropriate option to fill in the blank at LINE-1 such that output becomes 24.5.

a) std: :thread (Summation (dv))
b) std: :async(Summation(dv))
c) std: :atomic(Summation(dv))
d) std: :thread{bind(Summation(dv))}

Answer: b) std: :async(Summation(dv))


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


Q7. Consider the following program (in C++14).
Identify the statement/s that is/are not true about the program.

a) It generates output as :
0 1 2 3 5 6 7 8
b) It generates output as :
5 6 7 8 0 1 2 3
c) It generates output as :
5 0 6 1 7 2 8 3
d) It generates output as :
0 1 2 3

Answer: d


Q8. Consider the following program (in C++11)
Identify the statement/s that is/are not true about the program.

a) It generates output as :
R1 : 10 10
R2 : 30 30
b) It generates output as :
R1 : 10 10
R2 : 20 20
c) It generates output as :
R2 : 20 20
R1 : 30 30
d) It results in deadlock

Answer: b


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


Q9. Consider the following code segment (in C++11).
Choose the appropriate option to fill in the blanks at LINE-1 and LINE-2 so that the output becomes 100 400 900 1600.

a) LINE-1 : p1.set_value (v)
LINE-2 : auto rv = f1.get()
b) LINE-1 : p1.set_value (v)
LINE-2 : auto rv = f2.get()
c) LINE-1 : p2.set_value (v)
LINE-2 : auto rv = f1.get()
d) LINE-1 : p2.set_value (v)
LINE-2 : auto rv = f2.get()

See also  Programming in Modern C++ | Week 11

Answer: b) LINE-1 : p1.set_value (v)
LINE-2 : auto rv = f2.get()


Programming Assignment of Programming in Modern C++

Question 1

Consider the program below (in C++11), which implements a smart pointer.
    • Fill in the blank at LINE-1 with appropriate header and initializer list for the copy  constrcutor.
    • Fill in the blank at LINE-2 with appropriate header to overload dereferenceing operator.
    • Fill in the blank at LINE-3 with appropriate header to overload indirection operator.
      The program must satisfy the given test cases.

Solution:

template <typename T>
class SmartPtr {
public:
explicit SmartPtr(T* pointee) : pointee_(pointee) { }
SmartPtr(const SmartPtr& other) : pointee_(other.pointee_) { }
    ~SmartPtr() { if(pointee_ != nullptr) delete pointee_; }

    T& operator*() { return *pointee_; }

    T* operator->() { return pointee_; }

private:
    T* pointee_;

};

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


Question 2

Consider the following program in C++14 to represent a doubly linked list (of generic type), which allows adding items at the front of the list. Complete the program as per the instructions given below.
    • Fill in the blank at LINE-1 with appropriate statements to traverse the list in forward direction.
    • fill in the blank at LINE-2 with appropriate statements to traverse the list in backward direction,
The program must satisfy the sample input and output.

Solution:

void biTraverse()
{
  std::shared_ptr<node<T>> t = first;
  std::shared_ptr<node<T>> p = last;

  while (t != nullptr)
  {
    std::cout << t->info << " ";
    t = t->next;
  }

  std::cout << std::endl;

  while (p != nullptr)
  {
    std::cout << p->info << " ";
    p = p->prev.lock();
  }
}
    private:
        std::shared_ptr<node<T>> first = nullptr;
        std::shared_ptr<node<T>> last = nullptr;
};

*Sorry can’t able to pass private cases please try to do it yourself.


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


Question 3

Consider the following program (in C++11).
   • Fill in the blank at LINE-1 by defining a mutex object.
   • Fill the blanks at LINE-2 and LINE-4 by locking the mutex object.
   • Fill the blanks at LINE-3 and LINE-5 by unlocking the mutex object.
The program must satisfy the sample input and output. 

Solution:

#include <iostream>
#include <thread>
#include <functional>
#include <chrono>
#include <mutex>
std::mutex mtx; //LINE-1

class account {
public:
account() = default;
void deposit(double amount){
mtx.lock(); //LINE-2
update_amount = amount;
int delay = (int)((double)std::rand() / (double)(RAND_MAX)* 10);
std::this_thread::sleep_for(std::chrono::milliseconds(delay));
balance_ += update_amount;
mtx.unlock(); //LINE-3
}
void withdrawal(double amount){
mtx.lock(); //LINE-4
update_amount = amount;
int delay = (int)((double)std::rand() / (double)(RAND_MAX)* 10);
std::this_thread::sleep_for(std::chrono::milliseconds(delay));
balance_ -= update_amount;
mtx.unlock(); //LINE-5
}

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