Programming in Modern C++ Week 10 Solutions

ABOUT THE COURSE :

Programming in Modern C++ Week 6 Solutions

Disclaimer: The answers to these questions are provided for educational and informational purposes only. This website does not claim any surety of 100% correct answers. So, this website urges you to complete your assignment yourself.

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.

Summary
Course Type Elective
Duration 12 weeks
Category Computer Science and Engineering
Start Date 22 Jan 2024
End Date 12 Apr 2024
Exam Registration Ends 16 Feb 2024
Exam Date 28 Apr 2024 IST

Assignment Solutions :

Programming Assignment :

W10_Programming_Qs-1 :

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

#include <iostream>
 
int convert(double d){
    return int(d);
}
double convert(int i){
    return double(i);
}
 
template<typename T, typename U>    //LINE-1
 
auto divide(T n1, U n2) -> decltype(convert(n1) / convert(n2)) {   //LINE-2
    return convert(n1) / convert(n2);
}
W10_Programming_Qs-2 :
#include <iostream>
 
class ctype { 
    public: 
        ctype() : cp_(nullptr) {  }
        ctype(char c) : cp_(new char(c)) { }
        ctype(const ctype& ob) : cp_(new char(*(ob.cp_))) {}  // LINE-1: copy constructor 
        ctype& operator=(const ctype& ob) {            // LINE-2: copy assignment 
            if (this != &ob) { 
                delete cp_;  
                cp_ = new char(*(ob.cp_) + 1); 
            } 
            return *this;
        }
        ctype(ctype&& ob) noexcept : cp_(ob.cp_) {         // LINE-3: move constructor 
            ob.cp_ = nullptr; 
        }  
        ctype& operator=(ctype&& ob) noexcept {               // LINE-4: move assignment 
            if (this != &ob) { 
                cp_ = ob.cp_; 
                ob.cp_ = nullptr; 
            } 
            return *this;
        }
W10_Programming_Qs-3 :

Consider the following program (in C++11). Fill in the blanks as per the instructions given below:
      • Fill in the blanks at LINE-1 with appropriate template definition.
      • Fill in the blanks at LINE-2 to complete the header for function add.
      • Fill in the blanks at LINE-3 to define the new type Tmp.
The program must satisfy the sample input and output.

template<typename T, typename U>           //LINE-1
 
decltype(auto) add(T n1, U n2) {         //LINE-2
 
    typedef decltype(n1 + n2) Tmp;    // LINE-3: define new type Tmp
    Tmp sum = n1 + n2;
    
    std::cout << sum << std::endl;
}

CRITERIA TO GET A CERTIFICATE :
  1. Average assignment score = 25% of average of best 8 assignments out of the total 12 assignments given in the course.
  2. Exam score = 75% of the proctored certification exam score out of 100
  3. Final score = Average assignment score + Exam score
YOU WILL BE ELIGIBLE FOR A CERTIFICATE ONLY IF AVERAGE ASSIGNMENT SCORE >=10/25 AND EXAM SCORE >= 30/75. If one of the 2 criteria is not met,you will not get the certificate even if the Final score >= 40/100.

Join With Us On :

Join Now


Previous Year Questions Now to Boost Your Exam Performance

LOADS OF LOGIC