Programming in Modern C++ | Week 12

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.

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()

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
The content uploaded on this website is for reference purposes only. Please do it yourself first.
Home
Account
Cart
Search