NPTEL Programming In Modern C++ Week9 Assignment Solution 2023

NPTEL-Programming-In-Modern-C-Week9-Assignment-Solution-2023

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++ Week9 Programming Assignment July 2023

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

Course Name : Programming In Modern C++ July 2023

Programming Assignment : Q1

Consider the following program that finds the minimum element in an array. Fill in the blanks
as per the instructions given below:
   • Fill in the blank at LINE-1 with appropriate template declaration.
   • Fill in the blanks at LINE-2 and LINE-3 with appropriate conditional statement.
				
					#include<iostream>

template<typename Itr, typename T>    //LINE-1

void min(Itr first, Itr last, T& mv) {
    mv = *first++;
    while (first != last) {          //LINE-2

        if(*first < mv)            //LINE-3
            mv = *first;
        ++first;
    }
}

int main(){
    int iArr[10];
    int n;
    std::cin >> n;
    for(int i = 0; i < n; i++)
        std::cin >> iArr[i];
        
    double minVal = 0.0;
    min(iArr, iArr + n, minVal);
    std::cout << minVal;
    return 0;
}
				
			

Course Name : Programming In Modern C++ July 2023

Programming Assignment : Q2

Consider the following program that takes inputs a lower limit (l) and an upper limit (u) of a vector. If all the elements of the input vector are within the given lower and upper limits, the program prints “all in [l, u]”. Otherwise, the program prints the first element of the vector which is not within the given limits. Fill in the blanks as per the instructions given below:
  • at LINE-1 with appropriate header to overload function operator,
  • at LINE-2 with appropriate template definition,
  • at LINE-3 with appropriate condition.
				
					#include<iostream>
#include<vector>

struct Bound{
    Bound(int l, int u) : l_(l), u_(u){}

    bool operator()(int n) const {     //LINE-1
        return (n >= l_ && n <= u_);
    }
    int l_, u_;
};

template <typename Itr, typename Pred>      //LINE-2

Itr search(Itr first, Itr last, Pred bd) {
    while (first != last){

        if(!bd(*first))     //LINE-3
            return first;
        ++first;
    }     
    return first;
}

int main(){
    int iArr[] = { 70, 20, 50, 40, 90, 10, 80 }; 
    std::vector<int> iVec(iArr, iArr + sizeof(iArr) / sizeof(*iArr));
    int l, u;
    std::cin >> l >> u;
    Bound bd(l, u);
	
    std::vector<int>::iterator it = search(iVec.begin(), iVec.end(), bd);
	
    if(it == iVec.end())
        std::cout << "all in [" << l << ", " << u << "]";
    else
        std::cout << *it << " is the first element not in [" << l << ", " << u << "]";
    return 0;
}
				
			

Course Name : Programming In Modern C++ July 2023

Programming Assignment : Q3

Consider the following program, which computes the frequency of occurrence (histogram) of
each integer in a given vector. Fill in the blanks as per the instructions given below:
   • at LINE-1 with appropriate statement to iterate over the given vector v,
   • at LINE-2 with appropriate statement to iterate over the given map hi.
				
					#include <iostream>
#include <map>
#include <vector>

std::map<int, int> histo(std::vector<int> v){
    std::map<int, int> hi; 
    for (std::vector<int>::iterator it = v.begin(); it != v.end(); ++it) //LINE-1
        hi[*it]++;
    return hi;
}

void print(std::map<int, int> hi){
    for (std::map<int, int>::iterator it = hi.begin(); it != hi.end(); ++it) //LINE-2
        std::cout << it->first << ": " << it->second << ", ";
}

int main() { 
    std::vector<int> vec;
    for(int i = 0; i < 10; i++){
        int a;
        std::cin >> a;
        vec.push_back(a);
    }
    std::map<int, int> hi = histo(vec); 
    print(hi);	
    return 0;
}