Home

Learn Programming & Prepare for NPTEL Exams... Swayam Solver is your one-stop destination for NPTEL exam preparation.

Programming in Modern C++ | Week 4 : Programming Assignments | Jan-2022 | NPTEL | Swayam

 Programming in Modern C++


If you are using a mobile device then rotate to landscape orientation for better experience.



Week 4 : Programming Assignment 1

Due on 2022-02-24, 23:59 IST
Consider the following program. Fill in the blanks as per the instructions given below:
  • at LINE-1 with appropriate declaration so that global operator function can access private data of class Complex,
  • at LINE-2 with appropriate operator function header,
  • at LINE-3 with appropriate return statement,
such that it will satisfy the given test cases. 

Just scroll your mouse wheel over this code or swipe on this code snippet to scroll down to see the remaining code.

Select the Language for this assignment. 
1
#include<iostream>
2
#include<cstring>
3
using namespace std;
4
 
5
class Complex{
6
    int re, im;
7
public:
8
    Complex(int r, int i) : re(r), im(i){}
9
    void print(){
10
        cout << re << " + j" << im;
11
    }
12
    
13
    friend Complex operator+(const Complex &e, int n);      //LINE-1
14
};
15
 
16
Complex operator+(const Complex &e, int n){            //LINE-2
17
    
18
    return Complex(e.re+n,e.im);               //LINE-3
19
}
0
int main(){
1
    int n;
2
    cin >> n;
3
    Complex c(3, 4);
4
    Complex c1 = c + n;
5
    c1.print();
6
    return 0;
7
}

 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
5
8 + j4
8 + j4
Passed
Test Case 2
10
13 + j4
13 + j4
Passed

Private Test cases used for EvaluationStatus
Test Case 1
Passed





Week 4 : Programming Assignment 2

Due on 2022-02-24, 23:59 IST
Consider the following program. Fill in the blanks as per the instructions given below:
  • at LINE-1 with appropriate keyword to declare the Database pointer variable,
  • at LINE-2 to complete the header for function getInstance(int i),
  • at LINE-3 with appropriate keyword to complete ins variable initialization
such that it will satisfy the given test cases. 

Select the Language for this assignment. 
1
#include<iostream>
2
using namespace std;
3
 
4
class Database{
5
    int data;
6
    static Database *ins;                    //LINE-1
7
    Database(int i) : data(i) {}
8
public:
9
    int get(){ return data; }
10
    static Database* getInstance(int i){        //LINE-2
11
        if(!ins)
12
            ins = new Database(i);
13
        return ins;
14
    }
15
};
16
 
17
Database* Database::ins = 0; // LINE-3
0
int main(){
1
    int n, i;
2
    cin >> n;
3
    int a[n];
4
    for(i=1;i<=n;i++)
5
        cin >> a[i];
6
    for(i=1;i<=n;i++){
7
        Database *ins = Database::getInstance(a[i]);
8
        cout << ins->get() << " ";
9
    }
10
    return 0;
11
}
 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
2
3 4
3 3 
3 3 
Passed
Test Case 2
3
2 4 6
2 2 2 
2 2 2 
Passed

Private Test cases used for EvaluationStatus
Test Case 1
Passed




Week 4 : Programming Assignment 3

Due on 2022-02-24, 23:59 IST

Consider the following program. Fill in the blanks as per the instructions given below:
  • at LINE-1 with appropriate statement so that global operator function can access private members,
  • at LINE-2 with appropriate operator function header,
  • at LINE-3 with appropriate operator function header,
  • at LINE-4 with appropriate operator function header
such that it will satisfy the given test cases.

Select the Language for this assignment. 
1
#include<iostream>
2
using namespace std;
3
 
4
class Point{
5
    int x;
6
    int y;
7
public:
8
    Point(int _x, int _y) : x(_x), y(_y) {}
9
    void print(){
10
        cout << "(" << x << "," << y << ")";
11
    }
12
    friend istream& operator>>(istream& is, Point& p);      //LINE-1
13
    
14
    Point& operator++(){                                //LINE-2
15
        ++x;
16
        return *this;
17
    }
18
    Point operator++(int){                              //LINE-3
19
        Point p(x,y);
20
        ++y;
21
        return p;
22
    }
23
};  // End of class Point
24
 
25
 
26
istream& operator>>(istream& is, Point& p){                //LINE-4
27
    is >> p.x >> p.y;
28
    return is;
29
}
0
int main(){
1
    int i, j;
2
    cin >> i >> j;
3
    Point p(i,j);
4
    ++(++p);
5
    p++;
6
    p.print();
7
    return 0;
8
}

 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
3 4
(5,5)
(5,5)
Passed
Test Case 2
5 2
(7,3)
(7,3)
Passed

Private Test cases used for EvaluationStatus
Test Case 1
Passed



Please subscribe to our YouTube Channel :  Swayam Solver

This will help the creator to go on and on...

Have a great day !

No comments:

Post a Comment

Keep your comments reader friendly. Be civil and respectful. No self-promotion or spam. Stick to the topic. Questions welcome.