NPTEL Programming In Modern C++ Week 2 Programming Assignment

Programming-In-Modern-C-Week2-Assignment-Solution

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 2 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 which defines a type point and overloads operator + such that the x and y coordinates of a given point can be added with an integer.
Complete the program with the following instructions.
• Fill in the blank at LINE-1 with the appropriate header of the function to overload operator +.
• Fill in the blank at LINE-2 to declare a new point.
• Fill in the blank at LINE-3 to return the new point.

				
					#include <iostream>
using namespace std;

struct point {
    int x, y;
};

point operator + (point pt, int t) {    //LINE-1
    point new_pt;       				//LINE-2
    new_pt.x = pt.x + t;
    new_pt.y = pt.y + t;
    return new_pt;      				//LINE-3
}

int main() {
    int a, b, c;
    cin >> a >> b >> c;
    point p = {a, b};
    int t = c;
    point np = p + t;
    cout << "(" << np.x << ", " << np.y << ")";
    return 0;
}

				
			

Programming Assignment Q2

Consider the following program. Fill in the blanks at LINE-1 and LINE-2 with appropriate headers of overloaded function product. The program must satisfy the sample input and output.

				
					#include <iostream>
using namespace std;

int product(int a=1, int b=1, int c=1) {
    return a * b * c;
}

double product(double a=1, double b=1) {
    return a * b;    
}

int main() {
    int i1, i2, i3;
    double d1, d2;
    cin >> i1 >> i2 >> i3 >> d1 >> d2;
    cout << product(i1) << ", ";
    cout << product(i1, i2) << ", ";
    cout << product(i1, i2, i3) << ", ";
    cout << product(d1, d2);
    return 0;
}
				
			

Programming Assignment Q3

Consider the following program. Fill in the blank at LINE-1 with an appropriate function header for the function update_and_sum( ) such that it satisfies the sample input and output.

				
					#include <iostream>
using namespace std;

int update_and_sum(int a, int b, int &c) {    //LINE-1
    a *= 10;
    b *= 10;
    c = a + b;
    cout << a << ", " << b << ", " << c << endl;
}

int main() {
    int i1, i2, i3 = 0;
    cin >> i1 >> i2;
    update_and_sum(i1, i2, i3);
    cout << i1 << ", " << i2 << ", " << i3 << endl;
    return 0;
}