Nptel Programming in C++ All Answer

                                                                         Week 1


Q1:-

#include<iostream>

#include<algorithm>

#include<cctype>

#include<string.h>

using namespace std;

bool compare(char c1, char c2){

    return (tolower(c1)==tolower(c2))?0:1;        //LINE-1

}

int main(){

    char arr1[20], arr2[20];

    cin >> arr1;

    cin >> arr2;


    cout << lexicographical_compare(arr1, arr1+strlen(arr1), arr2, arr2+strlen(arr2),

                                  compare);

    return 0;

}


Q2:-

#include<iostream>

using namespace std;


void andop(int a, int b){

    cout << (a&b);

}


void orop(int a, int b){

    cout << (a|b);

}


void xorop(int a, int b){

    cout << (a^b);

}

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

}

int main(){

    int ch, a, b;

    cin >> ch >> a >> b;

    caller(ch,a,b);

    return 0;

}


Q3:-

#include<iostream>

#include<string.h>

using namespace std;

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;        //LINE-1

}

int main(){

    char s1[20], s2[20];

    int n,r;

    cin >> s1 >> s2 >> n;

    r = compare(s1,s2,n);

    if(r>0)

        cout << s1;

    else if(r<0)

        cout << s2;

    else

        cout << "equal";

    return 0;

}


                                                                      Week 2


Q1:-

#include<iostream>

using namespace std;


struct Point{

    int x, y;

};

Point operator ~(struct Point &p){               //LINE-1

    struct Point p1 = {0,0};

    p1.x=~p.x+1;                                //LINE-2

    p1.y=~p.y+1;                                //LINE-3

    return p1;

}


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

    struct Point p3 = {0,0};

    p3.x = p1.x + p2.x;

    p3.y = p1.y + p2.y;

    return p3;

}


int main(){

    struct Point p1, p2, p3;

    cin >> p1.x >> p1.y >> p2.x >> p2.y;

    p3 = p2 + ~p1;

    cout << p3.x << " " << p3.y;

    return 0;

}


Q2:-


#include<iostream>

using namespace std;


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

    return (x+n);

}


int main(){

    int x, n, r;

    cin >> x >> n;

    if(n==0)

        r = increment(x);

    else

        r = increment(x,n);

    cout << r;

    return 0;

}


Q3:-

#include<iostream>

using namespace std;


int square(const int &a){      //LINE-1

    return (a*a);              //LINE-2

}

int main(){

    int x, y, r;

    cin >> x >> y;

    r = 2*square(x+y) + 2*(x+y) - 4;

    cout << r;

    return 0;

}


Q4:-


#include<iostream>

using namespace std;


void caller(char *a,int n);       //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=(char*)calloc(sizeof(char),n);              //LINE-3

    caller(a,n);

    free(a);                  //LINE-4

    return 0;

}

void caller(char *a,int n){       //LINE-5

    for(int i=0;i<n;i++)

        cin >> *(a+i);

    reverse(a,n);

    for(int i=0;i<n;i++)

        cout << *(a+i);

}


                                                                         Week 3

Q1:-

#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

    }


    void print(){ cout << "(" << x << "," << y << ")" << endl; }

};


int main(){

    int x1,x2,y1,y2;

    cin >> x1 >> y1 >> x2 >> y2;

    Point p1(x1,y1), p2(x2,y2);

    p1.print(); 

    p2.print();

    printf("Distance: %.2f", p1.distance(p2));

    return 0;

}


Q2:-


#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


    void EnterEle(){

        for(int i=0;i<size;i++)

            cin >> arr[i];

    }

    void FindMax(){

        int max = -1;

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

            if(max < arr[i])

                max = arr[i];

        }

        cout << "Max: " << max;

    }

};


int main(){

    int n;

    cin >> n;

    IntArray a(n);

    a.EnterEle();

    a.FindMax();

    return 0;

}


Q3:- 


#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

    }


    void print(){

        cout << str << endl;

    }

};


int main(){

    string str1,str2;

    cin >> str1 >> str2;

    Myclass m1(&str1[0]);

    Myclass m2 = m1;

    m2.update(&str2[0]);

    m2.print();

    return 0;

}


                                                                            Week 4


Q1:-


#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

};

void print(int x, int y){

    if(x==1)

        cout << Test1(y).x;

    else

        cout << Test2(y).y;

}


int main(){

    int a,b;

    cin >> a >> b;

    print(a,b);

    return 0;

}


Q2:-


#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;

    }


    void print(){ cout << "(" << x << ", " << y << ")"; }

};


int main(){

    int x1,y1,x2,y2;

    cin >> x1 >> y1 >> x2 >> y2;

    positionVector p1(x1,y1), p2(x2,y2), p3(0,0);

    p3 = p1+p2;

    p3.print();

    return 0;

}


Q3:-


#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


    cout << id << " : " << name << " : " << marks; 


} // End of Function print  

 

}; // End of class


int main(){

    string n;

    int i, m, u;

    cin >> i >> n >> m >> u;

    const Student s1(i, n, m);

    s1.updateMarks(u);

    s1.print();

    return 0;

}


Q4:-


#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;

    }


    void show(){

        cout << data;

    }

};


database *database::dbObject = 0;


int main() {

    int x, y;

    database *db1, *db2;

    cin >> x >> y;

    db1 = database::connect(x);

    db2 = database::connect(y);

    db1->show();

    db2->show();

    return 0;

}


                                                                      Week 5


Q1:-


#include <iostream>

using namespace std;


class rectangle{

    protected:

        int width, height;

    public:

        rectangle(int _width, int _height) : width(_width), height(_height){}

        int area(){ return width * height; }

};


class square : public rectangle{

    public:

        square(int _side);

};


class cube : public rectangle{

    public:

        cube(int _side);

        int area(){ return 6 * width * width; }

};


class rectangularPrism : public rectangle{

    int length;

    public:

        rectangularPrism(int _width, int _height, int _length);

        int area(){ return 2 * (width * height + height * length + length * width); }

};

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


int main(){

    int a, b, c, d;

    cin >> a >> b >> c >> d;

    if(a == 1){

        rectangle *r = new rectangle(b, c);

        cout << r->area();

    }

    else if(a == 2){

        square *s = new square(b);

        cout << s->area();

    }

    else if(a == 3){

        cube *cb = new cube(b);

        cout << cb->area();

    }

    else if(a == 4){

        rectangularPrism *rp = new rectangularPrism(b, c, d);

        cout << rp->area();

    }

    return 0;

}


Q2:-


#include <iostream>

using namespace std;


class Person{

    string name;

    public:

        Person(string _name = "Unknown") : name(_name){}

        void show(){ cout << name << ", "; }

};


class Employee : public Person{

    int eid;

    public:

        Employee(){}

        Employee(int _eid, string _name) : Person(_name), eid(_eid){}

        void printEID(){ cout << eid << ", "; }

        void show();

};


class Manager : public Employee{

    string department;

    public:

        Manager(){}

        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(); 

}


int main(){

    int a;

    string b, c;

    cin >> a >> b >> c;

    Manager m(a, b, c);

    m.show();

    return 0;

}


Q3:-


#include <iostream>

using namespace std;


class Volume{

    int edge;

    public:

        Volume(int _edge) : edge(_edge){}

        int getVal(){ return edge * edge * edge; }

};


class Area{

    int edge;

    public:

        Area(int _edge) : edge(_edge){}

        int getVal(){ return edge * edge; }

};

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


    public:

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


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


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

};


int main(){

    int a;

    cin >> a;

    Cube c(a);

    cout << c.getArea() << ", " << c.getVolume();

    return 0;

}


Q4:-


#include <iostream>

using namespace std;


class RAM{

    int size;

    public: 

        RAM(int _size) : size(_size){}

        void getRAMSize(){ cout << size << " GB, "; }

};


class HardDisk{

    int size;

    public: 

        HardDisk(int _size) : size(_size){}

        void getHDSize(){ cout << size << " GB, "; }

};


class Processor{

    double speed;

    public: 

        Processor(double _speed) : speed(_speed){}

        void getSpeed(){ cout << speed << " GHz"; }

};



class Computer : private RAM, private HardDisk, private Processor{

    string brand;

    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


        void getBrand(){ cout << brand << ", "; }


}; // End of class Computer


int main() {

    string s;

    int a, b;

    double c;

    cin >> s >> a >> b >> c;

    Computer com(s, a, b, c);

    com.getBrand();

    com.getRAMSize();

    com.getHDSize();

    com.getSpeed();

    return 0;

}


                                                                        Week 6



Q1:-


#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 "; }

};


int main(){

    int i;

    cin >> i;

    A *pt = new B(i);

    delete pt;

    return 0;

}


Q2:-


#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 "; }

};


int main(){

    int i;

    cin >> i;

    A *pt = new B(i);

    delete pt;

    return 0;

}


Q3:-


#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

};

int square::area(){ return s * s; }


void square::show(){ cout << area() << " "; }

class cube : public square{

    int s;

    public:

        cube(int _s = 0) : s(_s){}


        int area(){ return 6 * s * s; }

};


int main(){

    int i;

    cin >> i;

    square s(i);

    cube c(i);

    square* r[2] = {&s, &c};

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

        r[i]->show();

    }

    return 0;

}


Q4:-


#include <iostream>

using namespace std;


class shape{

    protected:

        int s;

        shape(int _s) : s(_s){}


    public:

        virtual void show() = 0;

};



class circle : public shape{


    public:

        circle(int r) : shape(r){}


        void printArea(){ cout << (3.14 * s * s); }

        void show(){

            cout<<s<<" ";    //LINE-1


            printArea();

        }

};

void shape::show(){     //LINE-2


    cout << s << " "; 

}  

int main(){

    int a;

    cin >> a;

    shape *sp = new circle(a);

    sp->show();

    return 0;

}

👆👆


Comments

Popular Posts