NPTEL Programming In Modern C++ Week 8 Programming Assignment

Programming-In-Modern-C-Week8-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 8 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.
 • Fill in the blank at LINE-1 with template declaration.
 • Fill in the blank at LINE-2 to define the header of function extract( ).
				
					#include<iostream>
#include<vector>
using namespace std;

template<class T>    //LINE-1
class Filter{
    T ub, lb;
    public:
        Filter(T _lb = 0, T _ub = 0) : ub(_ub), lb(_lb) { }
        vector<T> extract(vector<T> tVec);
};
template<class T> vector<T> Filter<T> :: extract(vector<T> tVec){    //LINE-2
    vector<T> rVec;
    for(int i = 0; i < tVec.size(); i++){
        if(tVec[i] <= ub && tVec[i] >= lb)
            rVec.push_back(tVec[i]);
    }
    return rVec;
}
int main(){
    int i1, i2;
    char d1, d2;
    cin >> i1 >> i2 >> d1 >> d2;
    Filter<int> flt1(i1, i2);
    Filter<char> flt2(d1, d2);
    int arr1[] = {30, 10, 50, 60, 80, 20, 40, 70, 90};
    char arr2[] = {'a', 'n', 'i', 'm', 'l', 's'};
    vector<int> iVec(arr1, arr1 + 9);
    vector<char> cVec(arr2, arr2 + 6);
    vector<int> rVec1 = flt1.extract(iVec);
    vector<char> rVec2 = flt2.extract(cVec);
    for(int i = 0; i < rVec1.size(); i++)
        cout << rVec1[i] << ", ";
    cout << endl;
    for(int i = 0; i < rVec2.size(); i++)
        cout << rVec2[i] << ", ";
    return 0;
}
				
			

Programming Assignment Q2

Consider the following program.
  • Fill in the blank at LINE-1 with the appropriate inheritance statement.
  • Fill in the blank at LINE-2 with the appropriate return statement.
  • Fill in the blank at LINE-3 with the appropriate declaration of generic array items.
  • Fill in the blank at LINE-4 with the appropriate function header to overload operator[].
  • Fill in the blank at LINE-5 to throw exception InvalidAccess.
				
					#include<iostream>
#include<exception>
#include<vector>
#include<cstdlib>
using namespace std;
class InvalidAccess : public exception { // LINE-1
    public:
        virtual const char* what() const throw() {
            const char* str = "invalid access";    //LINE-2
        }
};
template<class T>
class ItemList{
    int n;
    T* items;    //LINE-3
    public:
        ItemList(vector<T> _items) : n(_items.size()), 
                       items((T*)malloc(n * sizeof(int))){
            for(int i = 0; i < n; i++){
                items[i] = _items[i];
            }
        }
        
        T operator[](int i){     //LINE-4
            if(i >= 0 && i < n){
                return items[i];
            }
            throw InvalidAccess();      //LINE-5
        }
};

int main(){
    int idx[3];
    for(int i = 0; i < 3; i++){
        cin >> idx[i];
    }
    
    vector<int> iVec;
    vector<char> cVec;
    for(int i = 0; i < 5; i++){
        iVec.push_back((i + 1) * 10);
        cVec.push_back(65 + i);
    }
    
    ItemList<int> iList(iVec);
    ItemList<char> cList(cVec);
    
    for(int i = 0; i < 3; i++){
        try{
            cout << iList[idx[i]] << ", ";
            cout << cList[idx[i]] << endl;
        }catch(InvalidAccess e){
            cout << e.what() << endl;
        }
    }
    return 0;
}
				
			

Programming Assignment Q3

Consider the following program that consider a list of strings as input and prints them in
sorted order of their length.
  • Fill in the blanks at LINE-1 with the function header to overload the function operator.
  • Fill in the blanks at LINE-2 with an appropriate return statement such that it sorts them
  according to the length of the strings (in case, if the two strings have the same length
  then it maintains the actual order).
				
					#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
struct less_mag {
    bool operator()(string x, string y){    //LINE-1

        return x.length() < y.length();    //LINE-2
    }
};
int main(){
    vector<string> iVec;
    int n;
    string na;
    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> na;
        iVec.push_back(na);
    }
    sort(iVec.begin(), iVec.end(), less_mag());
    for(int i = 0; i < iVec.size(); i++)
        cout << iVec[i] << " ";
    return 0;
}