NPTEL 3 min read Updated Sep 2023

NPTEL Programming In Modern C++ Week 10 Programming Assignment

Faheem Ahmad

1 read

Programming-In-Modern-C-Week10-Programming-Assignment-Solutions

There has been a continual debate on which programming language/s to learn, to use. As the latest TIOBE Programming Community Index for August 2021 indicates – C (13%), Python (12%), C++ (7%), Java (10%), and C#(5%) together control nearly half the programming activities worldwide. Further, C Programming Language Family (C, C++, C#, Objective C etc.) dominate more than 25% of activities. Hence, learning C++ is important as one learns about the entire family, about Object-Oriented Programming and gets a solid foundation to also migrate to Java and Python as needed. C++ is the mother of most general purpose of languages. It is multi-paradigm encompassing procedural, object-oriented, generic, and even functional programming. C++ has primarily been the systems language till C++03 which punches efficiency of the code with the efficacy of OOP. Then, why should I learn it if my primary focus is on applications? This is where the recent updates of C++, namely, C++11 and several later offer excellent depths and flexibility for C++ that no language can match. These extensions attempt to alleviate some of the long-standing shortcomings for C++ including porous resource management, error-prone pointer handling, expression semantics, and better readability. The present course builds up on the knowledge of C programming and basic data structure (array, list, stack, queue etc.) to create a strong familiarity with C++98 / C++03. Besides the constructs, syntax and semantics of C++ (over C), we also focus on various idioms of C++ and attempt to go to depth with every C++ feature justifying and illustrating them with several examples and assignment problems. On the way, we illustrate various OOP concepts. The course also covers important advances in C++ 11 and later released features..

Programming In Modern C++ Week 10 Programming Assignment.

INTENDED AUDIENCE :  Any interested audience
PREREQUISITES :  10th standard/high school
INDUSTRY SUPPORT :  Programming in C++ is so fundamental that all companies dealing with systems as well as application development (including web, IoT, embedded systems) have a need for the same. These include – Microsoft, Samsung, Xerox, Yahoo, Oracle, Google, IBM, TCS, Infosys, Amazon, Flipkart, etc. This course would help industry developers to be up-to-date with the advances in C++ so that they can remain at the state-of-the-art.

Course Layout

Week 1: Programming in C++ is Fun.
Week 2: C++ as Better C.
Week 3: OOP in C++.
Week 4: OOP in C++.
Week 5: Inheritance.
Week 6: Polymorphism.
Week 7: Type Casting.
Week 8: Exceptions and Templates.
Week 9: Streams and STL.
Week 10: Modern C++.
Week 11: Lambda and Concurrency.
Week 12: Move, Rvalue and STL Containers

Programming Assignment Q1

Consider the program below (in C++14).
   • Fill in the blanks at LINE-1 and LINE-2 with appropriate namespace declaration.
   • Fill in the blanks at LINE-3 with an appropriate statement, such that all symbols defined
     in the scope namespace Ver1 becomes available in the main().
The program must satisfy the given test cases.
C++
#include <iostream>
#include <list>
namespace Ver1{
    inline namespace Ver1_1 {    //LINE-1
        int addAll(std::list<int> i_list){
            int sum = 0;
            for(auto i : i_list){
                sum += i;
            }
            return sum;
        }
    }

    namespace Ver1_1 {    //LINE-2

        template<typename T>
        T addAll(std::list<T> t_list){
            T sum = 0;
            for(auto i : t_list){
                sum += i;
            }
            return sum;
        }
    }
}

using namespace Ver1;    //LINE-3
int main(){
    int n;
    std::cin >> n;
    std::list<int> ilist;
    std::list<double> dlist;
    for(int i = 0; i < n; i++){
        int x;
        std::cin >> x;
        ilist.push_back(x);
    }
    for(int i = 0; i < n; i++){
        double x;
        std::cin >> x;
        dlist.push_back(x);
    }
    std::cout << Ver1_1::addAll(ilist) << " ";
    std::cout << addAll(ilist) << " ";
    std::cout << addAll(dlist);
    return 0;
}

Programming Assignment Q2

Consider the following program (in C++14).
   • Fill in the blank at LINE-1 with an appropriate template definition.
   • Fill in the blank at LINE-2 with an appropriate header for function divide.
The program must satisfy the sample input and output.
C++
#include <iostream>
int getNumber(char c){
    return int(c);
}

int getNumber(double d){
    return int(d);
}

double getNumber(int i){
    return double(i);
}
template<class T, class U>    //LINE-1

auto divide(T n1, U n2) -> decltype(getNumber(n1)/getNumber(n2)) {   //LINE-2

    return getNumber(n1) / getNumber(n2);
}
int main(){
    int a;
    double b;
    char c;
    std::cin >> a >> b >> c;
    std::cout << divide(c, a) << " ";
    std::cout << divide(c, b);
    return 0;
}

Programming Assignment Q3

Consider the following program that implements copy constructor, copy assignment, move
constructor and move assignment.
   • Fill the missing code segments at code-segment-1 to implement the move constructor.
   • Fill the missing code segments at code-segment-2 to implement the move assignment.
The program must satisfy the sample input and output.
C++
#include <iostream>
class point { 
    public:
        point(int x = 0, int y = 0) : _px(new int(x)), _py(new int(x)) { } 
        point(const point& p) : _px(new int(*(p._px) * 2)), 
                                   _py(new int(*(p._py) * 2)) { }  
        point& operator=(const point& p) {   
        if (this != &p) { 
            delete _px;
            delete _py;
            _px = new int(*(p._px) * 3);
            _py = new int(*(p._py) * 3);
        } 
        return *this;
    }
    ~point() { delete _px; delete _py; } 
    point(point&& p) noexcept : _px(p._px), _py(p._py) {
        //code-segment-1
      _px = new int(*(p._px) * 4);
      _py = new int(*(p._py) * 4);
      p._px=nullptr;
      p._py=nullptr;
    }   
    point& operator=(point&& p) noexcept{
        //code-segment-2
      delete _px;
      delete _py;
      _px = new int(*(p._px) * 5);
      _py = new int(*(p._py) * 5);
      p._px=nullptr;
      p._py=nullptr;
    }
    
        friend std::ostream& operator<<(std::ostream& os, const point& p) { 
        std::cout << "(" << *(p._px) << ", " << *(p._py) << ")";
        return os;
    }
    friend std::istream& operator>>(std::istream& os, point& p) { 
        std::cin >> *(p._px) >> *(p._py);
        return os;
    }
    private:
        int *_px = nullptr, *_py = nullptr; 
};

int main(){
    point p1;
    std::cin >> p1;
    point p2 = p1;
    point p3;
    p3 = p1;
    std::cout << p1 << ", " << p2 << ", " << p3 << std::endl;
    
    point p4 = std::move(p1);
    std::cout << p4 << ", ";
    point p5;
    p5 = std::move(p4);
    std::cout << p5;
    return 0;
}

// written by

Faheem Ahmad

Writer at Techies Talk

Faheem writes about NPTEL, Coursera, and How To on Techies Talk, turning tricky topics into guides you can follow step by step.

Writes about #NPTEL117 #Coursera13 #How To5

Articles
144
Reads
202
Writing since
2020
All posts by Faheem