NPTEL » Programming in C++ Assignment 2021

Programming In C++ WEEK 1

NPTEL Programming in C++ With the growth of Information and Communication Technology, there is a need to develop large and complex software. To meet this requirement object-oriented paradigm has been developed and based on this paradigm the C++ programming language emerges as the best programming environment.

Programming in C++ is a MOOC based course that is 12 weeks in duration and can fulfill the criteria of 4 credits in a year. You can visit the NPTEL SWAYAM platform and register yourself for the course. This course is brought to you by Prof. Partha Pratim Das received his BTech, MTech, and Ph.D. degrees in 1984, 1985, and 1988 respectively from IIT Kharagpur.

Programming In C++ 2021 Details:-

Contents

  1. Who Can Join: The undergraduate students from the engineering disciplines namely CSE, IT, EE, ECE, etc. might be interested in this course.
  2. Requirements/Prerequisites: 1. Basic Knowledge of Programming & Data Structure 2. C Programming 3. Attending a course on OOP / OOAD with this course will help.
  1. INDUSTRY SUPPORT: Programming in C++ is so fundamental that all companies dealing with systems as well as application development (including web, IoT, embedded systems, machine learning) have a need for the same. These include – Microsoft, Samsung, Xerox, Yahoo, Google, IBM, TCS, Infosys, Amazon, Flipkart, etc.

Programming in C++ Assignment Week 08 Answers:-

Q1. Consider the following program. Fill in the blanks: at LINE-1 with proper declaration of n,at LINE-2 with appropriate template header, and at LINE-3 with proper function-header to overload operator =, such that it satisfies the given test cases.

Code:-

#include<iostream>
using namespace std;

const int n=5;                   //LINE-1

template<class T, int n>           //LINE-2
class Array{
    T _items[n];
    int _i;
    public:
        Array(int size = 0) : _i(0){}

        void operator=(const T& data){    //LINE-3

            _items[_i++] = data;
        }

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

Code:-

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

class IndexOutOfRange : public exception { // LINE-1

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

        return "IndexOutOfRange";         //LINE-2
    }
};

template<class T>
class List{
    int _size;
    T* _items;
    public:
        List(int size) : _size(size), _items((T*)malloc(_size * sizeof(T))){}
        T& operator[](int i){
            if(i < _size){
                return _items[i];
            }

            throw IndexOutOfRange();    //LINE-3
        }
};

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

Code:

#include<iostream>
using namespace std;

template<class T>
class Multiplier{
    T _v1, _v2;
    public:
        Multiplier(T v1 = 0, T v2 = 0) : _v1(v1), _v2(v2){}
        T calculate();
};
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 type ComplexNum so that it satisfies the given test cases.

Code:-

template<>                  //LINE-1

class Maximum<ComplexNum>{           //LINE-2

    ComplexNum _v1, _v2;
    public:
        Maximum(ComplexNum v1, ComplexNum v2) : _v1(v1), _v2(v2){}
        ComplexNum getMax(){
            if(_v1._r > _v2._r)
                return _v1;
            else if(_v1._r < _v2._r)
                return _v2;
            else{
                if(_v1._i > _v2._i)
                    return _v1;
                else
                    return _v2;
            }
        }

Programming in C++ UNPROCTORED EXAM(EVENING SESSION) Answers:-

Q1. Consider the following program. Fill the blank at LINE-1 with appropriate header for pre-increment operator overloading; in the blanks at LINE-2 and LINE-3 to complete thedefinition of pre-increment operator. Fill in the blank at LINE-4 with appropriate header forpost-increment operator overloading; in the blanks at LINE-5 and LINE-6 to complete thedefinition of post-increment operator such that it satisfies the given input / output.

Code:-

#include<iostream>
using namespace std;
class ComplexNum {
    int r, i;
    public:
        ComplexNum(int _r, int _i) : r(_r), i(_i) { }
        ComplexNum& operator++(){		//LINE-1

            ++r;			//LINE-2
	
            return *this;			//LINE-3	
        }
        ComplexNum operator++(int){        //LINE-4

            ComplexNum t(r,i);	 		 //LINE-5

            ++r;		//LINE-6
            return t;
        }

Q2. Consider the following program. Fill in the blanks at LINE-1 with appropriate overloading ofcasting operator and LINE-2 with appropriate parameter to the function print() such thatit satisfies the test cases.

Code:-

operator Character() {  //LINE-1

    return Character((char)(d)); 

    }      

};

void print(Character a){          //LINE-2

    a.show();
}

Q3. Consider the following program. Fill in the blank at LINE-1 to create the array arr. Fill inthe blanks at LINE-2 and LINE-3 with appropriate throw such that it satisfies the test cases.

Code:-

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

class Array{
    int *arr;
    int l;      //length of the array
    public:
        Array(int _l) : l(_l), arr(new int(_l)){    //LINE-1
            for(int i = 0; i < l; i++)
                arr[i] = 0;
        }
        void setEle(int i, int n){    //add value n to index i
            if(i < l)
                arr[i] = n;
            else
                throw i;           //LINE-2
        }
        int getEle(int i){    //return value of given index position
            if(i < l)
                return arr[i];
            else
                throw i;         //LINE-3
        }
};

Q4. Consider the following program. Fill in the blanks at LINE-1 with appropriate template definition.Fill in the blanks at LINE-2 and LINE-3 with appropriate specialization for C-stringsand proper required header of the class such that it satisfies the given test cases.

Code:-

#include <iostream>
#include <cstring>
using namespace std;
template <class T>          //LINE-1

class myType {
    T n1, n2;
    public:
        myType(T _n1, T _n2) : n1(_n1), n2(_n2) {}
        T sum() { return n1 + n2; }
};

template<>   // LINE-2: Create explicit specialization for C-string
class myType<char*>{ // LINE-3: Write the required class header

Q5. Consider the following program. At LINE-1, define area() as pure virtual function and atLINE-2, define volume() as virtual function. At LINE-3, verify if *ep is of type square and atLINE-4, verify if *ep is of type cube so that it satisfies the given test cases.

Code:-

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

class shape {
    public:
        virtual void area() = 0;    //LINE-1

        virtual void volume() { cout << "cannot implement"; }   //LINE-2
};
class square : public shape {
    int s;
    public:
        square(int _s) : s(_s) {}
        void area();
};
class cube : public shape {
    int s;
    public:
        cube(int _s) : s(_s) {}
        void area();
        void volume();
};

void caller(shape *ep) {

    if (typeid(*ep) == typeid(square) )		//LINE-3

        ep->area();

    if (typeid(*ep) == typeid(cube) ){       //LINE-4

        ep->area();
        ep->volume();
    }
}

Q6. Consider the following program. At LINE-1, fill in the blank with appropriate forward declaration. At LINE-2, make appropriate declaration of function operator+() such that it access theprivate member of class g. LINE-3, provide appropriate header for function operator+()such that it satisfies the given test cases.

Code:-

#include <iostream>
using namespace std;

class kg;    //LINE-1
class g {
    int we;
    public:
        g(double _we) : we(_we) {}
        void disp();
        g operator+(kg&);
};
class kg {
    int we;
    public:
        kg(int _we) : we(_we) {}

        friend g g::operator+(kg&);    //LINE-2
};

g g::operator+(kg& n) {       //LINE-3
    g temp(0);
    temp.we = n.we * 1000 + we; 
    return temp; 
}

Q7. Consider the following program. Implement the following functions:operator+(int op)operator–(int op)operator()(int op)such that output satisfies the test cases.

Code:-

void Stack::operator+(int op) {
	//implement push operation
    items[++top]=op;
}
void Stack::operator--() {
	//implement pop operation    
    top--;
}
int Stack::operator()() {
	//implement peek operation    
    return items[top];
}

Programming in C++ UNPROCTORED EXAM Answers:-

Q1. Consider the following program. The functions client1(), client2(), client3() create message depending upon the caller() which return to the main(). Fill in the blanks at LINE-1, LINE-2, LINE-3 as per the instructions given below such that the program satisfies the test cases.LINE-1: Create a new pointer to function type named fun_ptrLINE-2: Allocate memory for the concatenated messageLINE-3: Initialize the function pointer fp with the functions.

Code:-

typedef char * (*fun_ptr) ( char *);     // LINE-1

char * AllocateMemory(char *str, char *msg){
    
    char* s = new char [strlen(str) + strlen(msg) + 1]; // LINE-2
    return s;
}


int main(){
    int i;
    char msg[80];
    
  fun_ptr fp[3] = {client1 , client2 , client3}; //LINE-3

Q2. Consider the following code segment. For class point overload operator + at LINE-1such that both variables x and y of class point will be raised by a value of z by theoverloaded function. Overload operator ++ at LINE-2 such that it will increment theupdated value (after adding z to both x and y) of x by 1. Check the test cases for clarity.

Code:-

point point::operator+(int z)
{
  point temp;
  temp.x=this->x+z+1;
  temp.y=this->y+z;
  return temp;
}


point point::operator++(int z)
{
  point temp;
  temp.x=this->x+z;
  temp.y=this->y+z;
  return temp;
  
}
ANSWER

Q3. Consider the following program. In class complex_num, fill in the blanks as follows:at LINE-1 to complete the definition of default constructor so that both variables areinitialized to 0 by default,at LINE-2 to complete the definition of constructor,it will assign the r with the double value and i with 0.at LINE-3 to complete the definition of parameterized constructor so that both variablescan be initialized with the values passed as parameters of constructor,at LINE-4 to complete the header to overload of operator+,at LINE-5 to complete the header to overload of casting operator to double

Code:-

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

class complex_num{
    double r, i;
    public:
        complex_num():r(0),i(0){}                //LINE-1

        complex_num(double r_):r(r_),i(0){}                //LINE-2

        complex_num(double r_,double i_):r(r_),i(i_){}                //LINE-3

        complex_num operator+(const complex_num& c){                    //LINE-4
            complex_num res;
            res.r = r + c.r;
            res.i = i + c.i;
            return res;
        }

        operator double(){ return sqrt(r*r + i*i); }      //LINE-5
};

Q4. Consider the following program. Fill in the blank at LINE-1 with appropriate initializationlist in the constructor definition. Fill in the blanks at LINE-2 and LINE-3 to call eat() functionof class WaterHabitat and LandHabitat respectively such that it satisfies the test cases.

Code:-

class Amphibians : public WaterHabitat, public LandHabitat {
    public:
        Amphibians(string name, int _amt1, int _amt2) : WaterHabitat(name,_amt1),LandHabitat(name, _amt2){}    //LINE-1

        void eat(){

        WaterHabitat::eat();                       //LINE-2

           LandHabitat::eat();                      //LINE-3
        }
};

Q5. Consider the following program. A class Account is declared to store the account number, name and salary of the employee. We have the functionality to change the salary through a member function of the class. At LINE-1 and LINE-2 fill in the blanks to declare variables sl and balance (salary) respectively; at LINE-3 define the header for change() function which can change the balance; LINE-4 define the header for print() function which can print the account information appropriately such that it satisfy the given test cases.

Code:-

Q6. Consider the following program where class NegativeValException is defined for an exception. The class NegativeValException has two data members: _what and _what_str which are initialized by the given constructor. Fill in the blanks at LINE-1 and LINE-2 with proper initialization of data members _what and _what_str. At LINE-3, fill in blank with proper argument-list such that overloading of operator<<() can access the private members of class NegativeValException. At LINE-4, fill in the blank with proper argument list to overload operator<<() which prints the private data members of class class NegativeValException so that it satisfies the test cases.

Code:-

Q7. Consider the following program. Do the required forward declaration at LINE-1. Use the
appropriate keywords to declare operator+ at LINE-2 and LINE-3. At LINE-4 declare the
function operator() of class foot so that object of class inches can use it and fill the function header
for operator+ at LINE-5 such that it satisfies the given test cases.

Code:-

Programming in C++ Assignment Week 07 Answers:-

Q1. Consider the following program. Fill in the blanks at LINE-1 and LINE-2 with appropriate function headers such that it would satisfy the given test cases.

Code:-

        StringType(char* str): _str(str){}    //LINE-1
        operator char*()  {                //LINE-2

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

Code:-

        Char(char _ch) : ch(toupper(_ch)){}    //LINE-1
        operator char(){ return ch; }        //LINE-2
        operator int(){ return ch - 'A'; }  //LINE-3
quizxp telegram

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

Code:-

class derived1 : virtual public base {    //LINE-1
    public:
        derived1(int i);
};
class derived2 : virtual public base {     //LINE-2
    public:
        derived2(int i);
};
class derived3 : virtual public base {    //LINE-3
    public:
        derived3(int i);
};

class derived_derived      
    : public derived2, public derived1, public derived3 {    //LINE-4

Q4. Consider the following program. Fill in the blank at LINE-1 with declaration of pure virtual function calculate(). Fill in the blanks at LINE-2, LINE-3 and LINE-4 with appropriate inheritance statement such that it satisfies the given test cases.

Code:-

virtual int calculate(){}    //LINE-1
};

class sum : virtual public math{    //LINE-2
    public:
        sum(int v1, int v2) : math(v1, v2){}
        int calculate() { return (_v1 + _v2); }
};

class subtraction : virtual public math{     //LINE-3
    public:
        subtraction(int v1, int v2) : math(v1, v2) {}
        int calculate() { return (_v1 - _v2); }
};

class multiplication : virtual public math{     //LINE-4

C++ Assignment Week 06 Answers:-

Q1. Consider the program below. Fill in the blank at LINE-1 with the appropriate declaration of destructor. Fill in the blank at LINE-2 with the appropriate initialization-list such that it satisfies the given test cases. Do not change any other part of the code.

Code:-

#include<iostream>
using namespace std;

class A{
    public:
        A(){ cout << "1 "; }
        A(int n){ cout << n * 2 << " "; }

        virtual ~A();   //LINE-1
};
A::~A(){ cout << "2 "; }
class B : public A{
    public:
        B(){ cout << "3 "; }

        B(int n) : A(n){ cout << n * 5 << " "; }    //LINE-2
        virtual ~B(){ cout << "4 "; }
};

Q2. Consider the program below. Fill in the blanks at LINE-1 and LINE-2 with appropriate statements such that that it satisfies the given test cases.

Code:-

double manager::getSalary(){

    return salary+getHRA();        //LINE-1 
}

double salesperson::getSalary(){

    return salary+getHRA()+getTA();        //LINE-2    
}

Q3. Consider the program below. Fill in the blanks at Line-1 and Line-2 with appropriate declarations such that it matches given test cases. Do not change any other part of the code.

Code:-

#include <iostream>
using namespace std;

class square{
    int s;
    public:
        square(int _s = 0) : s(_s){}

        virtual int area();         //LINE-1

        void show();         //LINE-2
};

Q4. Fill in the blank at Line-1 with appropriate function call to the show() function from class shape and also fill up LINE-2 with appropriate header of show() such that it matches the given test cases. Do not change any other part of the code.

Code:-

        void show(){
            cout<<s<<" ";    //LINE-1

            printArea();
        }
};
void shape::show(){     //LINE-2

    cout << s << " "; 
}  

C++ Assignment Week 05 Answers:-

Q1. Consider the following program. Fill in the blanks at LINE-1, LINE-2 and LINE-3 with appropriate initialization-list such that it can satisfy the given test cases.

Code:-

square::square(int _side):rectangle(_side,_side){}   //Line-1

cube::cube(int _side):rectangle(_side,_side){}   //Line-2


rectangularPrism::rectangularPrism(int _width, int _height, int _length)
 
    
 :rectangle(_width,_height),length(_length){}    //Line-3

Q2. Consider the following program. Fill in the blanks in LINE-1 with appropriate initialization list. Also fill in the blanks in LINE-2 and LINE-3 with appropriate statements such that it satisfies the test cases.

Code:-

        Manager(int _eid, string _name, string _department) 
             :Employee(_eid,_name),department(_department){}   //Line-1
        
        void printDepartment(){ cout << department; }        
        void show();
};

void Employee::show(){ 
    printEID();

   Person::show();     //Line-2 
}

void Manager::show(){ 

    Employee::show();   //Line-3
    printDepartment(); 
}
quizxp telegram

Q3. Consider the following program. Fill in the blank at LINE-1 with a proper inheritance statement.Fill in the function bodies at LINE-2 and LINE-3 with appropriate statements such that the program satisfies the given test cases.

Code:-

class Cube :public Volume,Area{   //Line-1

    public:
        Cube(int _edge) : Volume(_edge), Area(_edge){}

                   int getVolume(){Volume::getVal();}   //Line-2

    int getArea(){Area::getVal();}       //Line-3
};

Q4. Consider the following program. Fill in the blank at LINE-1 with the appropriate initialization list. Fill in the body of the functions at LINE-2, LINE-3, and LINE-4 with appropriate statements such that the program must satisfy the given test cases.

Code:-

    public:
        Computer(string _brand, int _ram_size, int _hd_size, double _speed) 
            :brand(_brand),RAM(_ram_size),HardDisk(_hd_size),Processor(_speed){}        //LINE-1
                                   
        void getRAMSize(){RAM::getRAMSize();}                         //LINE-2

        void getHDSize(){HardDisk::getHDSize();}                         //LINE-3

        void getSpeed(){Processor::getSpeed();}                         //LINE-4

C++ Assignment Week 04 Answers:-

Q1. Consider the following program. Fill in the blanks at LINE-1 and LINE-2, such that it will satisfy the given test cases.

Code:-

#include<iostream>
using namespace std;

class Test1{
    int x;
public:
    Test1(int _x) : x(_x) { cout << "Class 1: "; }

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

};

class Test2{
    int y;
public:
    Test2(int _y) : y(_y) { cout << "Class 2: "; }

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

Q2. Consider the following program. Fill in the blanks: at LINE-1 to overload addition operator for the class position vector, at LINE-2 and LINE-3 to calculate addition of two-position vector, such that it will satisfy

Code:-

#include<iostream>
using namespace std;

class positionVector{
    int x, y;
public:
    positionVector(int a, int b) : x(a), y(b) {}
    positionVector operator+(positionVector pv){    //LINE-1

        positionVector p(0,0);

        p.x = this->x+pv.x;                                     //LINE-2

        p.y = this->y+pv.y;                                     //LINE-3

        return p;
    }
quizxp telegram

Q3. Consider the following program and fill in the banks at LINE-1, LINE-2 and LINE-3 withappropriate keywords, such that it matches the given test cases.

Code:-

#include<iostream>
using namespace std;

class Student{
    const int id;
    string name;

    mutable int marks;                                //LINE-1

public:
    Student(int a, string b, int c) : id(a), name(b), marks(c) {}

    void updateMarks(int x) const { marks += x; }     //LINE-2

    void print() const {                            //LINE-3

Q4. Consider the below program. Please fill in the blanks at Line-1, Line-2, and LINE-3 by the instructions given in the comments, such that it satisfies the given test cases.

Code:-

#include<iostream>
using namespace std;

class database {
    int data;

    static database *dbObject;            //LINE-1 Complete the declaration
    database(int x) : data(x) {}
public:
    static database* connect(int x){      //LINE-2 Mention return type of the function
        if(!dbObject)

         dbObject=new database(x);    //LINE-3 Allocate memory to pointer dbObject
        return dbObject;
    }

C++ Assignment Week 03 Answers:-

C++ Week 3: Q1. Consider the following program. Fill in the blanks at LINE-1 and LINE-2 to complete parameterized and copy constructor and at LINE-3 to complete the return statement which calculates the Euclidean distance between two points (Use math library functions when needed). Please check the sample input and output.

Answer/Code:-

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

class Point{
    const int x,y;
public:
    Point(int _x=0, int _y=0) : x(_x),y(_y){}               //LINE-1
    Point(const Point& p) : x(p.x),y(p.y){}                 //LINE-2
    double distance(Point p){
        return sqrt(pow(p.x - x, 2) +
                    pow(p.y - y, 2)* 1.0);     //LINE-3
    }

C++ Week 3: Q2.Consider the following program. Fill in the blanks at LINE-1 to complete the parameterizedconstructor and at LINE-2 to complete the destructor which deletes the dynamically allocatedmemory to data member arr. Consider the following test cases.

Answer/Code:-

#include<iostream>
using namespace std;

class IntArray{
    int *arr;
    int size;
public:
    IntArray(int n) :arr(new int[n]),size(n){}    //LINE-1

    ~IntArray(){delete[] arr; }                    //LINE-2

C++ Week 3: Q3. Consider the following program. Fill in the blanks at LINE-1 and LINE-2 to complete the constructor and destructor and at LINE-3 to complete the copy assignment operator header and at LINE-4 to concatenate the function input with the class data member such that it satisfies the given test cases.

Answer/Code:-

#include<iostream>
#include<malloc.h>
#include<string.h>
using namespace std;

class Myclass{
    char *str;
public:
    Myclass(char *s) : str(s) {}     //LINE-1

    ~Myclass(){ delete str; }                 //LINE-2

    Myclass& operator=(Myclass& m){    //LINE-3

        free(str);
        str = strdup(m.str);
        return *this;
    }
    void update(char* x){
        strcat(str," ");
      strcat(str,x);//LINE-4
    }

C++ Assignment Week 02 Answers:-

C++ Week 2: Q1.Consider the following program. Fill in the blanks at LINE-1 to overload unary operator ∼, at LINE-2 and LINE-3 to complete ∼ operator overloading and at LINE-4 to overload addition operator for structure Point so that it will satisfy sample test cases.

Answer/Code:-

Point operator~(struct Point &p){               //LINE-1
    struct Point p1 = {0,0};
    p1.x=-p.x;                                //LINE-2
    p1.y=-p.y;                                //LINE-3
    return p1;
}

Point operator+(const Point &p1, const Point &p2){   //LINE-4

C++ Week 2: Q2.Consider the following program and fill in the blanks at LINE-1 to complete the increment function header such that it matches the given test cases.

Answer/code:-

#include<iostream>
using namespace std;

int increment(int x, int n=1){      //LINE-1

Also check:-

C++ Week 2: Q3.Consider the following program and fill in the banks at LINE-1 with appropriate function header for square( ) so that it will take one argument as call by reference and at LINE-2 for the return statement. Consider the given test cases.

Answer/code:-

#include<iostream>
using namespace std;

int square(int a){      //LINE-1
    return (a*a);              //LINE-2
}

C++ Week 2: Q4. Consider the following program. Fill in the blank at LINE-1 to declare caller( ) function prototype, at LINE-2 to complete reverse( ) function header, at LINE-3 to dynamically allocate memory of size n using new operator, at LINE-4 to destroy the allocated memory in variable a and at LINE-5 to complete the caller( ) function header. Consider the sample test cases.

Answer/code:-

#include<iostream>
using namespace std;

void caller(char*,int);       //LINE-1
void reverse(char * p,int n){      //LINE-2
    char c;
    for(int i=0;i<n/2;i++){
        c = *(p+i);
        *(p+i) = *(p+n-i-1);
        *(p+n-i-1) = c;
    }
}

int main(){
    char *a;
    int n = 5;
    a=new char[n];        //LINE-3
    caller(a,n);
    delete(a);                 //LINE-4
    return 0;
}
void caller(char* a,int n){       //LINE-5

C++ Assignment Week 01 Answers:-

C++ Week 1: Q1.Consider the following code snippet. Fill in the blank at LINE-1 to complete the return statement of the function compare. Please check the sample input and output. (lexicographical_compare function returns 1 if the first string is lexicographically less than the second string)

Answer/Code:-

bool compare(char c1, char c2)
{
  return (tolower(c1)==tolower(c2))?0:1;
}

ALSO CHECK:-

C++ Week 1: Q2 – Consider the following program and fill the blank at LINE-1 to complete function pointer declaration and at LINE-2 to call the function pointer such that it satisfies the sample input and output.

Answer/Code:-

void (*fun_ptr[])(int, int) = {andop, orop, xorop};    //LINE-1

void caller(int ch, int a, int b){
    if(ch<=2)
        fun_ptr[ch](a,b);    //LINE-2
}

C++ Week 1: Q3 – Consider the following program and fill in the blanks at LINE-1 to return an integer after comparing both strings (s1 and s2) upto n characters such that it satisfies the test cases.

Answer/code:-

int compare(char *s1, char *s2, int n){
    int x=0;
  for(int i=0;i<n;i++)
    if(s1[i]==s2[i])
    x=0;
  else if(s1[i]>s2[i]) 
    return 1;
  else 
    return -1;
  return x;        
}

NPTEL » Programming In Java Assignment week 10 Sep-2020

NPTEL » Programming In Java Assignment week 6 Sep-2020

Find Other Quiz Here:

Amazon Fashion Quiz Answers & Win ₹1000

Amazon Great Indian Festival Quiz Answers: Win Rs. 50,000

NPTEL » Programming In Java Assignment week 6 Sep-2020

NPTEL » Programming In Java Assignment week 7 Sep-2020

NOTE: These codes are based on our knowledge. Answers might be incorrect, we suggest you to not the copy-paste answers blindly.

[foobar id=”1315″]