NPTEL PROGRAMMING IN C++ ASSIGNMENT 2021

NPTEL PROGRAMMING IN C++ ASSIGNMENT

NPTEL Programming in C++ course builds up on the knowledge of C programming and basic data structure (array, list, stack, queue, binary tree etc.) to create a strong familiarity with C++98 and C++03. Besides the constructs, syntax and semantics of C++ (over C), we also focus on various idioms of C++.

NPTEL PROGRAMMING IN C++ is MOOC course offered by IIT Kharagpur on the NPTEL platform. This course help students to achieve in developing understanding in OOP. Hence this course is advised in conjunction with OOAD. The course is developed by Prof. Partha Pratim Das received his BTech, MTech and PhD degrees in 1984, 1985 and 1988 respectively from IIT Kharagpur. He served as a faculty in Department of Computer Science and Engineering, IIT Kharagpur from 1988 to 1998.

  1. Who Can Join: This is an elective course. Intended for senior UG/PG students. BE/ME/MS/PhD
  2. Requirements/Prerequisites: Basic Knowledge of Programming & Data Structure, C Programming, Attending a course on OOP / OOAD with this course will help
  3. INDUSTRY SUPPORT: All IT Industries

CRITERIA TO GET A CERTIFICATE

Average assignment score = 25% of the average of the best 8 assignments out of the total 12 assignments given in the course.
Exam score = 75% of the proctored certification exam score out of 100

Final score = Average assignment score + Exam score

Students will be eligible for CERTIFICATE ONLY IF AVERAGE ASSIGNMENT SCORE >=10/25 AND EXAM SCORE >= 30/75. If any of the 2 criteria are not met, the student will not get the certificate even if the Final score >= 40/100.

NPTEL PROGRAMMING IN C++ ONLINE PROGRAMMING TEST EVENING 2021:-

Contents

Q1. Consider the following program. ConfMgr is a singleton class. Fill in the blanks with the given instruction below:

Code:-

#include <iostream>
using namespace std;
class ConfMgr {
    string host; 

    mutable int port;    //LINE-1
    ConfMgr(){}
    ConfMgr(string _host, int _port) : host(_host), port(_port) { }
    public:
        ~ConfMgr() { }
        static const ConfMgr& createInstance(string _host = "localhost", 
            int _port = 8080) {

            ConfMgr * myPrinter_ = new ConfMgr(_host,_port); ;   //LINE-2

            return *myPrinter_;   //LINE-3
        }

        void update(int _port)const{    //LINE-4
            port = _port;
        }

        void GetConf()const {    //LINE-5
            cout << host << ":" << port << endl;
        }
};

Q2. Consider the following program. Fill the blanks with the given instruction below:

Code:-

#include <iostream>
using namespace std;

class NaturalNumber{
    int n;
    public:
        NaturalNumber(int _n = 0) {
            if(_n < 0)
                throw -10;    //LINE-1
            n = _n;
        }
        int operator++(int n){    //LINE-2
            NaturalNumber temp = *this;
            ++this->n;
            return temp;
        } 
        operator int(){ return n; }    //LINE-3
};

Q3. Consider the following program. Fill the blanks with the given instruction below:

Code:-

#include<iostream>
using namespace std;

class point {
    private:
        int x, y;
    public:
        point() : x(0), y(0) { }
        point& operator()(int,int);    //LINE-1
        int operator[](char c){    //LINE-2
            if(c == 'X' || c == 'x')
                return x;
            else if(c == 'Y' || c == 'y')
                return y;
            else
                return 0;
        } 
        void show();
};

point& point::operator()(int _x,int _y) {         //LINE-3
    x = _x; 
    y = _y; 
    return *this; 
}

Q4. ˆ Fill in the blank at LINE-1 with appropriate template header for class FiveElements.

Code:-

#include<iostream>
using namespace std;

template<class T,int N>    //LINE-1
class FiveElements{
    T *base;
    public:
        FiveElements(T a[]) : base(a){    //LINE-2
            for(int i = 0; i < N; i++)
                base[i] = a[i];
        }
        void print();
};

template<class T,int N>    //LINE-3

void FiveElements<T,N>::print(){    //LINE-4
     for(int i = 0; i < N; i++)
         cout << base[i] * 10 << "-";
}

Q5. Consider the following program. Fill the blanks with the given instruction below:
ˆ Fill in the blanks at LINE-1 and LINE-2 with appropriate template specialization for complex class.
ˆ At LINE-3, fill in blank with appropriate return statement.
ˆ Fill in the blanks at LINE-4 and LINE-5 with appropriate template header for print() of class Number such that it satisfies the given test cases. 

Code:-

Q6. ˆ Fill in the blanks at LINE-1 and LINE-3 with appropriate function declarations for + operator overloading function.

Code:-

#include<iostream>
#define c 1.852    //conversion factor from nautical_mile to kilometer
using namespace std;

class nautical_mile;
class kilometer{
    double val;
    public:
        kilometer(double _val = 0) : val(_val){}
        friend kilometer operator+(kilometer n,nautical_mile m);    //LINE-1
        operator double(){                       //LINE-2
            return val;
        }
};

class nautical_mile{
    double val;
    public:
        nautical_mile(double _val = 0) : val(_val){}
        friend kilometer operator+(kilometer n,nautical_mile m);    //LINE-3
};

Q7. Consider the following program. Fill the blanks at LINE-1, LINE-2 and LINE-3 with appropriate inheritance statements such that it satisfies the given test cases.

Code:-

class faculty : public person{    //LINE-1
    public:
        faculty(int n) ;
};

class student : virtual  public person{    //LINE-2
    public:
        student(int n) ;
};

class ta :  public faculty, virtual public student{    //LINE-3
    public:
        ta(int n) ;
};

NPTEL PROGRAMMING IN C++ ONLINE PROGRAMMING TEST 2021:-

Q1. Consider the following program. Fill in the banks as per the instructions given below. 

code:-

#include <iostream>
using namespace std;

class feet;
class inch{
    public:
    double val;
        inch(double _val) : val(_val){}

              //LINE-1
};

class cm{
    public:
    double val;
        cm(double _val) : val(_val){}
        void print();

  double operator=(double d){val=d;};       //LINE-2
};

class feet{
    public:
    double val;
        feet(double _val) : val(_val){}
        double operator+(inch );
};


double feet::operator+(inch in){               //LINE-3

    return this->val*30.48 + in.val*2.54;    //LINE-4
}

Q2. Consider the following program. It considers that each book object is associated with exactly one author, and a bookList object consists of a number of book objects. Fill in the banks as per the instructions given below. 

code:-

 class book : private author{
    string name;
    public:
        book() : name("unknown"), author("unknown"){}    //LINE-1: default constructor

        book(string _bname, string _aname) : name(_bname), author(_aname){} 
                               //LINE-2: parameterized constructor
        string getName(){ return name; }

  string getAname(){return author::getAname();}//LINE-3
};

class bookList{
    int no_books;
    book* list;
    public:
        bookList(int _no_books) : no_books(_no_books), list(new book[_no_books]){}
        void addBook(int i, string bname, string aname){

            book b(bname, aname);    //LINE-4
            list[i] = b;
        }

Q3. Consider the following program. The class ValueSorter sorts an array using ValueSorter::sort() function. The order of sorting depends on a Boolean type flag. 

code:-

#include <iostream>
#include <cstring>
using namespace std;

template <class T,int N,bool flag>   //LINE-1
class ValueSorter{
    T *base;
    bool compare(T x, T y){
        if(flag)
            return x < y;
        else
            return !(x < y);
  }
  public:
        ValueSorter(T *_base) : base(_base){}
        void sort(){
            for(int step = 0; step < N - 1; ++step){
                for(int i = 0; i < N-step-1; ++i) {
                    if(compare(base[i], base[i + 1])) {
                        T temp = base[i];
                        base[i] = base[i+1];
                        base[i+1] = temp;
                    }
                }
            }
        }
        void print();  
};

template <class T,int N,bool flag>   //LINE-2

void ValueSorter<T,N,flag>::print(){    //LINE-3
    for(int i = 0; i < N; i++)
        cout << base[i] << " ";    
}

int main(){
    double b;
  const int n = 5;   //LINE-4

Q4. Consider the following program. In the program, class Person is inherited by class Faculty and class Student. class TA inherits both the class Faculty and class Student. Fill the blanks using the following instructions.

code:-

class book : private author{
    string name;
    public:
        book() : name("unknown"), author("unknown"){}    //LINE-1: default constructor

        book(string _bname, string _aname) : name(_bname), author(_aname){} 
                               //LINE-2: parameterized constructor
        string getName(){ return name; }

  string getAname(){return author::getAname();}//LINE-3
};

class bookList{
    int no_books;
    book* list;
    public:
        bookList(int _no_books) : no_books(_no_books), list(new book[_no_books]){}
        void addBook(int i, string bname, string aname){

            book b(bname, aname);    //LINE-4
            list[i] = b;
        }

Q5. Consider the following program. Fill the blanks using the following instructions.

code:-

#include <iostream>
using namespace std;

class week{
    int n;
    public:
        week(int _n = 0) : n(_n){}

        operator int(){              //LINE-1 
            return n * 7;
        }
};

class month{
    int n;
    public:
        month(int _n = 0) : n(_n){}

        operator int(){               //LINE-2
            return n * 30;
        }
};

class day{
    int n;
    public:
        day(int _n = 0) : n(_n){}

        int getDays(week w=0, month m=0){          //LINE-3
            return n + static_cast<int>(w) + static_cast<int>(m); 
        }
};

Q6. Consider the following program to calculate simple interest and compound interest based on the given information. Fill the blanks using the following instructions.

code:-

virtual int getInterest()=0;                     //LINE-1
};

class SimpleInterest : public Interest{         //LINE-2
    public:
        SimpleInterest(int d = 0, double i = 0.0,   double p = 0.0) : 
                               Interest(d, i, p) {}
        int getInterest(){
            if(duration < 0)
                throw InvalidDuration();       //LINE-3
            return (int)(principal_amount * interest_rate * duration);  
  }
};

class CompoundInterest : public Interest{           //LINE-4
    int compounding_frequency;
    public:
        CompoundInterest(int d = 0, double i = 0.0, double p = 0.0, int f = 0) : 
        Interest(d, i, p), compounding_frequency(f) {}
        int getInterest(){
            if(duration < 0)
                throw InvalidDuration();   //LINE-5
            return (int)((principal_amount * 
                        pow((1 + interest_rate / compounding_frequency),
                    duration * compounding_frequency)) - principal_amount);  
  } 
};

Q7. Consider the following program to calculate simple interest and compound interest based on the given information. Fill the blanks using the following instructions.

code:-

template <class T>          //LINE-1
class Comparator{
    T a, b;
    public:
        Comparator(T _a, T _b) : a(_a), b(_b){}
      string caller1(){ return "ns::(" + ::maximum<T>(a, b) + ")"; }      //LINE-2
      string caller2(){ return ::maximum<T>(10, 20); }      //LINE-3
};

NPTEL PROGRAMMING IN C++ ASSIGNMENT WEEK 8 ANSWERS:-

Q1. Consider the following program. Fill in the blanks at LINE-1 and LINE-2 with the properheader of addVal() function.Fill in the blanks at LINE-3 and LINE-4 with appropriate header for getVals() function sothat it satisfies the given test cases.

Code:-

IMPORTANT:- 20 SEPTEMBER ONLINE PROGRAMMING/ UNPROCTORED TEST ANSWERS WILL ALSO BE PROVIDED SO JOIN US ON TELEGRAM

 template<class T, int n> // LINE-1

void List<T,n>::addVal(const T& val){ // LINE-2

    if(i < n - 1)
        items[++i] = val;
    else
        throw (i + 1);
}

template <class T, int n>// LINE-3

T List<T,n>::getVals(){ // LINE-4
    for(int j = 0; j <= i; j++)
        cout << items[j] << " ";
    cout << endl;
}
quizxp telegram

Q2. Consider the following program. Fill in the blank at LINE-1 with appropriate class name todefine a custom exception type.Fill in the blank at LINE-2 with appropriate return statement.Fill in the blank at LINE-3 with appropriate statement such that it satisfies the given test cases.

Code:-

#include<iostream>
#include<exception>
using namespace std;

class AgeException : public exception{    //LINE-1 

    public:
     virtual const char* what() const throw() {

      return "invalid age";    //LINE-2

     }
};

class recruitment{
    string _name;
    int _age;
    public:
        recruitment(string name, int age) : _name(name){

            age < 18 ? throw AgeException(): _age=(age) ;     //LINE-3

        }

IMPORTANT:- 20 SEPTEMBER ONLINE PROGRAMMING/ UNPROCTORED TEST ANSWERS WILL ALSO BE PROVIDED SO JOIN US ON TELEGRAM

Q3. Consider the following program. Fill in the blank at LINE-1 with appropriate template function header such that it satisfies the given test cases.

Code:-

template <class T> T Multiplier<T>::calculate(){    //LINE-1

    return _v1 * _v2;

}

Q4. Consider the following program.Fill in the blanks at LINE-1 and LINE-2 with appropriate class-template specialization for typeAdder.Fill in the blank at LINE-3 with appropriate header for add() function so that it satisfies thegiven test cases.

Code:-

template<>   //LINE-1  
  
class Adder<Point>{    //LINE-2

 Point n;
    public:
     Adder(Point _n) : n(_n){}
        Point add(const int& t);
        friend ostream& operator<<(ostream& os, const Point& p);
};

Point Adder<Point>::add(const int &t){    //LINE-3

    Point res;
    res.x = n.x + t;
    res.y = n.y + t;
    return res;
}

NPTEL PROGRAMMING IN C++ ASSIGNMENT WEEK 7 ANSWERS:-

Q1. Consider the following program. Fill in the blanks at LINE-1 following appropriate instruction (given in comment) and LINE-2, LINE-3, and LINE-4 with appropriate keywords such that it would satisfy the given test cases.

Code:-

        // create abstract function fun()
        virtual void fun() = 0;              //LINE-1
};

class B: public A {                          //LINE-2
    public:
        B(int a = 0): A(a) {}
        void fun(){ display(i+1); }
};

class C: public A {                         //LINE-3
    public:
        C(int a = 0): A(a) {}
        void fun(){ display(i+2); }
};

class D: public A {                      //LINE-4
quizxp telegram

IMPORTANT:- 20 SEPTEMBER ONLINE PROGRAMMING/ UNPROCTORED TEST ANSWERS WILL ALSO BE PROVIDED SO JOIN US ON TELEGRAM

Q2. Consider the following code snippet. Fill in the blank at LINE-1 with appropriate initialization list. Also fill in the blanks at LINE-2 and LINE-3 with appropriate header such that it matches the given test cases.

Code:-

String(int k) : n(k),arr(new char[k]){}     //LINE-1
        operator int(){                             //LINE-2
            return arr[--n];
        }
String& operator=(int k){                //LINE-3

Q3. Consider the following program. Fill in the blank at LINE-1 with appropriate header. Fill in the blanks at LINE-2 and LINE-3 with appropriate type-casting statements such that it satisfies the given test cases.

Code:-

   int operator=(int x){                  //LINE-1
            b = b + x;
        }
};

void fun(const A &t){
    int x;
    cin >> x;
    A &u = const_cast<A&>(t);         //LINE-2
    u.display();
    B &v = reinterpret_cast<B&>(u);   //LINE-3


Q4. Consider the following program. Fill in the blanks at LINE-1, LINE-2, LINE-3 following appropriate inheritance statements such that it satisfies the given test cases.

Code:-

class C2 : virtual public C1 { // LINE-1 : Inherit from class C1
    public:
        C2(int x = 0) : C1(++x) { fun(x); }
};
class C3 : virtual public C1 { // LINE-2 : Inherit from class C1
    public:
        C3(int x = 0) : C1(++x) { fun(x); }
};
class CC : public C3, public C2 { // LINE-3 : Inherit from class C2 and C3

NPTEL PROGRAMMING IN C++ ASSIGNMENT WEEK 6 ANSWERS:-

Q1. Consider the program below. Fill in the blank at LINE-1 and LINE-2 such that it satisfies the

Code:-

#include<iostream>

using namespace std;

class Shape{

protected:                     //LINE-1: Fill up with proper access specifier
    
    double ar;
public:
    virtual void area() = 0 ;   //LINE-2: Declare area as pure virtual function

Q2. Consider the program below. Fill in the blanks at LINE-1 with appropriate keyword, at LINE-2with declaration of fun() as pure virtual and at LINE-3 with appropriate object instantiationsuch that it satisfies the given test cases.

Code:-

#include<iostream>
using namespace std;

class Base{
protected:
    int s;

    public:

        Base(int i=0) : s(i){} 

        virtual ~Base(){ }               //LINE-1

        virtual int fun(int a) = 0;      //LINE-2
};


class Derived : public Base{
    public:
        Derived(int i) : Base(i) {}
        ~Derived();
        int fun(int x){
            return s*x;
        }
};

class Wrapper{

    public:

        void fun(int a, int b){

            Base *t = new Derived(a);     //LINE-3

Q3. Consider the program below. Fill in the blanks at Line-1 with appropriate keyword andLine-2 with appropriate statement so that global function fun can call private function of theclass. Consider the given test cases.

Code:-

#include<iostream>

using namespace std;

class A{
    int i;
    virtual void f();        //LINE-1

    public:
        A(int x) : i(x) {}
	
        friend void fun(A &);     //LINE-2
};

Q4. Fill in the blanks at LINE-1, LINE-2, and LINE-3 following the instructions given such that itmatches the given test cases.

Code:-

#include <iostream>
using namespace std;

class Rectangle{
    protected:
        int w;
        Rectangle(int _w) : w(_w){}
    public:
        virtual void print() = 0;    //LINE-1: Declare print as pure virtual function
};


class Square : public Rectangle{
    int h;
    public:
        Square(int _h) : Rectangle(_h), h(_h) {}

        void printArea(){ cout << (h*w); }

        void print(){

            Rectangle::print();   //LINE-2: call appropriate function

            printArea();
    }
};

void Rectangle::print(){   //LINE-3: Complete the header of the print function 
    cout << w << " "; 
}  

NPTEL PROGRAMMING IN C++ ASSIGNMENT WEEK 5 ANSWERS:-

Q1. Consider the following program. Fill in the blank at LINE-1 with appropriate inheritance statement. Fill in the blanks at LINE-2 and LINE-3 to complete the function definitions such that the program can satisfy the given test cases.

Code:-

class Circle : public Area{    //LINE-1
    int r;
    public:
        Circle(int _r) : r(_r){	}
        double getArea(){ return PI*r*r; }    //LINE-2
        
        double getPerimeter(){ return 2*PI*r; }    //LINE-3
};
quizxp telegram

Q2. Consider the following program. Fill in the blanks in LINE-1, LINE-2 and LINE-3 to completeconstructor definitions. Consider the given test cases.

Code:-


Derived1::Derived1(int _x) : Base(_x, _x){}    //LINE-1

Derived2::Derived2(int _x) : Base(_x, _x){}    //LINE-2

Derived3::Derived3(int _x, int _y, int _z) : Base(_x, _y), c(_z){}    //LINE-3

Q3. Consider the following program. Fill in the blank at LINE-1 such that global function studDetails()can access private member of class Student. Fill in the blanks at LINE-2 and LINE-3 to callconstructor of Student class. After all fill ups, the program must satisfy the given test cases.

Code:-

public:
        friend void studDetails(const Student&);    //LINE-1
};

class Bengali : public Student{
    public:
        Bengali(string n, int m) : Student(n, "Bengali", m){}    //LINE-2
};

class English : public Student{
    public:
        English(string n, int m) : Student(n, "English", m){}    //LINE-3
};

Q4. Consider the following program. Fill in the blank at LINE-1 and LINE-2 such that the programmust satisfy the given test cases.

Code:-

class Derived : public Base1, public Base2{    //LINE-1

    int d;

    public:

        Derived(int x) : d(x), Base1(x+1), Base2(x+2){}    //LINE-2

NPTEL PROGRAMMING IN C++ ASSIGNMENT WEEK 4 ANSWERS:-

Q1. Consider the following program. Fill in the blanks at LINE-1 to complete forward declaration and at LINE-2 for accessing data member of the class from another class function. Consider the given sample input and output.

Code:-

 #include<iostream>
using namespace std;

class Interest;    //LINE-1
class Maturity{
    double prin;
    double amt = 0;
    public: 
        Maturity(double p) : prin(p){}
        double calculate(Interest&);
};

class Interest{
    double in;
    public:
        Interest(double i) : in(i){	}
        friend class Maturity;    //LINE-2
};

Q2. Consider the following program. Fill in the blanks at LINE-1, LINE-2 and LINE-3 with appropriate keywords. Declare function at LINE-4 so that this global function can access private members of the class. Refer to the sample test cases for the inputs and their expected outputs.

Code:-

     mutable double basic;    //LINE-1
    mutable double salary;   //LINE-2
public:
    Employee(int i, double b, double s=0) : id(i), basic(b), salary(s){	}
    void setBasic(double b) const{    //LINE-3
        basic = b;
    }
    friend double calculate(const Employee & e);    //LINE-4
};

Q3. Consider the following program and fill in the banks at LINE-1 and LINE-2 such that it satisfies the given test cases.

Code:-

    friend void print(int, int);                  //LINE-1
};

class B{
    int y;
public:
    B(int _y) : y(_y){ cout << "Class B: ";	}
    friend void print(int, int);                  //LINE-2
quizxp telegram

Q4. Consider the program given below. Please fill in the blanks at LINE-1 with appropriate keyword, at LINE-2 with appropriate return type and at LINE-3 with appropriate initialization such that it satisfies the given test cases.

Code:-

    static singleton *object;            //LINE-1
    singleton(char x) : c(x) {	}
public:
    static singleton *create(char x){    //LINE-2
        if(!object)
            object = new singleton(x);
        return object;
    }
    void show();
};

singleton *singleton::object=0;       //LINE-3

NPTEL PROGRAMMING IN C++ ASSIGNMENT WEEK 3 ANSWERS:-

Q1. Consider the following program. Fill in the blank at LINE-1 and LINE-2 to complete theinitialization lists for the given constructors. Fill in the blank at LINE-3 to complete thereturn statement.

Code:-


#include <iostream>
using namespace std;

class rectangle{
    int *w, *h;
    public:
        rectangle(int _w, int _h) : w(&_w), h(&_h){}    //LINE-1
        
        rectangle(rectangle &r) : w(this->w), h(this->h){}    //LINE-2

        ~rectangle(){ delete w; delete h; }

        int area(){ return (*w) * (*h); }    //LINE-3
};
quizxp telegram

Q2. Consider the following program. Fill in the blank at LINE-1 to declare data-members and atLINE-2 to free the data-members so that the following test cases are satisfied.

Code:-

class Outer{

    Inner *y, *x ;    //LINE-1: declare data-members

public:
    Outer(int a, int b) : x(new Inner(a)), y(new Inner(b)){}

    ~Outer() { delete x; delete y; }    //LINE-2: free the data-members

};

Q3. Consider the following program.Fill in the blank at LINE-1 for appropriate declaration of data member department.At LINE-2, fill in the blank to complete the argument-list.Fill in the blanks at LINE-3 and LINE-4 to complete the header for functions changeDepartment()and display(), such that it satisfies the given test cases.

Code:-

#include <iostream>
#include <string>
using namespace std;
class Employee {
    int id;
    string name;

    mutable string department;    //LINE-1

    public:
        Employee(const int& _id, const string& _name, const string& _department="Unknown")
            : id(_id), name(_name), department(_department) {}    //LINE-2

        void changeDepartment(const string& s) const {    //LINE-3
            department = s;
        }
        void display() const {     //LINE-4

            cout << "[" << id << "] " << name << " : " << department << endl;
        }
};

Q4. Consider the following program.Fill in the blanks as per the given instructions:at LINE-1 complete the initialization-list for default constructor,at LINE-2 complete the initialization-list for parametrized constructor.at LINE-3 complete the initialization-list for copy constructor,such that it satisfies the given test cases.

Code:-

public:
        FloatNumber() : sign('+'), exponent(0), mantissa(0) {}    //LINE-1

        FloatNumber(char _sign, int _exponent, int _mantissa) : 
        				sign(_sign), exponent(_exponent), mantissa(_mantissa) {}    //LINE-2

        FloatNumber(FloatNumber& f) : sign(f.sign), exponent(f.exponent), mantissa(f.mantissa) {}    //LINE-3

        void display(){ cout << sign << exponent << "." << mantissa << endl; }
};

NPTEL PROGRAMMING IN C++ ASSIGNMENT WEEK 2 ANSWERS:-

Q1. Consider the following program. Fill in the blanks at LINE-1 to complete the macro definition so that it will satisfy the given test cases

CODE:-

#include <iostream>

using namespace std;

#define SQR(x)(x*(x)) //LINE-1: complete the macro definition
quizxp telegram

Q2. Consider the following program and fill in the blank at LINE-1 to complete the prototype of the multi() such that it matches the given test cases.

CODE:-

#include <iostream>

using namespace std;

int multi(int = 1,int = 1,int = 1);    //LINE-1: Complete function prototype

Q3. Consider the following program and fill in the banks at LINE-1 with appropriate allocation and initialization for pointer p such that it satisfies the given test cases.

CODE:

const int *p = &a; // LINE-1: allocate and initialize

Q4. Consider the following program. Fill in the blank at LINE-1 to complete the header of the func() such that it satisfies the test cases.

Code:-

#include <iostream>

using namespace std;

int& func(int& x) {    //LINE-1: complete the header of func()

quizxp telegram

NPTEL PROGRAMMING IN C++ ASSIGNMENT WEEK 1 ANSWERS:-

Q1. Consider the program below which compares two strings character wise and decides if one string is greater/lesser/equal than/with the another. Refer the input and output for better understanding. Fill in the blank at LINE-1 and LINE-2 with appropriate conditions such that it satisfies input and output of the given test cases.

CODE:-

#include <iostream>
#include <string>
using namespace std;

int scmp(string str1, string str2){

    if (str1>str2)    //LINE-1

        return 1;
	
    else if (str1==str2)     //LINE-2

        return 0;

    else
        return -1;
}

Q2. Consider the following program. Fill in the blank at LINE-1 to define a function pointer type Fun_Ptr such that it can be used to create a pointer to point sum() and subtract() functions. Fill in the blank at LINE-2 to define the third formal parameter of the function. Fill in blank at LINE-3 with appropriate function parameter such that it satisfies the sample input and output.

CODE:-

typedef int Fun_ptr(int,int);    //LINE-1

int caller(int a, int b, Fun_ptr fp) {     //LINE-2

    return (*fp)(a,b);      //LINE-3
}
quizxp telegram

Q3. Consider the following program and fill in the blanks at LINE-1 to define a stack. Fill in the blank at LINE-2 to pop one element from the stack. Fill in the blank at LINE-3 to push one element at a time such that it satisfied with test cases.

CODE:-

#include<iostream>
#include<string>
#include<stack>
using namespace std;

stack <string> ss;	//LINE-1
void remove(){
    ss.pop();      //LINE-2
}

int main() {
    string str;
    int n;
    cin >> n;
	
    for(int i = 0; i < n; i++){
        cin >> str;
        ss.push(str);   //LINE-3
    }

ALSO CHECK:- INTERNSHIP OPPORTUNITIES