NPTEL Programming In Modern C++ Week 1 Programming Assignment 2023

NPTEL Programming In Modern C++ Week1 Programming Assignment

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 1 Programming Assignment 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

Programming In Modern C++ 2023

Programming Assignment Q1 :

Consider the following program. Fill in the blanks as per the instructions given below:
    • at LINE-1 with stack declaration,
    • at LINE-2 to push values into stack,
    • at LINE-3 with appropriate statement
such that it will satisfy the given test cases.
				
					#include<iostream>
#include<cstring>
#include<stack>
using namespace std;


int main() {
    char str[20];
    char ch;
    cin >> str;
    stack<char> s; //LINE-1

    for(int i = 0; i < strlen(str); i+=2)
        s.push(str[i]); //LINE-2

    int len = s.size();

    for(int i = 0; i < len; i++) {

    	ch = s.top();  //LINE-3
    	cout << ch;

        s.pop();
    }
    return 0;
}
				
			

Programming In Modern C++ 2023

Programming Assignment Q2 :

Consider the following program. Fill in the blanks as per the instructions given below.
    • at LINE-1 with appropriate header statement,
    • at LINE-2 with appropriate statement to calculate Euclidean distance
such that it will satisfy the given test cases.
				
					#include <iostream>
#include <cmath>     //LINE-1 

using namespace std;
struct point{
    int x, y;
};

double get_len(point p1, point p2){
    return sqrt((p2.x - p1.x)*(p2.x - p1.x)+(p2.y - p1.y)*(p2.y - p1.y));    //LINE-2
}

int main() { 
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    point p1, p2;
    p1.x = x1;
    p1.y = y1;
    p2.x = x2;
    p2.y = y2;
    cout << round(get_len(p1, p2));
    return 0;
}
				
			

Programming In Modern C++ 2023

Programming Assignment Q3 :

Consider the following program. Fill in the blanks as per the instructions given below:
   • at LINE-1 with appropriate function header,
   • at LINE-2 with appropriate return statement
such that it will satisfy the given test cases.
				
					#include <iostream>
#include <algorithm>
using namespace std;

bool max_str(string a, string b) {    //LINE-1

    return a > b;            //LINE-2
}

int main() {
    std::string words[3], word;
    for(int i = 0; i < 3; i++){
        cin >> word;
        words[i] = word;
    }
    sort(words, words + 3, max_str);
    for (int i = 0; i < 3; i++)
        cout << words[i] << " ";
    return 0;
}