Home

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

NPTEL Programming in Modern C++ Programming Assignment Jan-2024 Swayam

NPTEL Programming in Modern C++ Programming Assignment Jan-2024 Swayam

 


NPTEL » Programming in Modern C++


  Please scroll down for latest Programs. 👇 



W6_Programming_Qs-1

Due on 2024-03-07, 23:59 IST
Your last recorded submission was on 2024-02-24, 20:13 IST
Select the Language for this assignment. 
1
#include<iostream>
2
3
using namespace std;
4
5
class Base{
6
protected:
7
    int s;
8
9
    public:
10
        Base(int i=0) : s(i){}
11
12
        virtual ~Base(){ }     //LINE-1
13
14
        virtual int fun() = 0; //LINE-2
15
};
16
17
class Derived : public Base{
18
    int x;
19
    public:
20
        Derived(int i, int j) : Base(i), x(j) {}
21
        ~Derived();
22
        int fun(){
23
            return s*x;
24
        }
25
};
26
class Wrapper{
27
    public:
28
        void fun(int a, int b){
29
            Base *t = new Derived(a,b); //LINE-3
30
            int i = t->fun();
31
            cout << i << " ";
32
            delete t;
33
        }
34
};
0
Derived::~Derived(){ cout << int(s/x) << " "; }
1
int main(){
2
    int i, j;
3
    cin >> i >> j;
4
    Wrapper w;
5
    w.fun(i,j);
6
    return 0;
7
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
8 4
32 2
32 2 
Passed after ignoring Presentation Error
Test Case 2
9 5
45 1
45 1 
Passed after ignoring Presentation Error





Private Test cases used for EvaluationStatus
Test Case 1
Passed




W6_Programming_Qs-2

Due on 2024-03-07, 23:59 IST
Your last recorded submission was on 2024-02-24, 20:23 IST
Select the Language for this assignment. 
1
#include <iostream>
2
using namespace std;
3
const double ir = 7;
4
const double bonus = 4;
5
class FD{
6
    protected:
7
        double principal;
8
    public:
9
        FD(double _p = 0.0) : principal(_p){}
10
        double getInterest(){ return principal * ir / 100; }
11
        double getExtraAmt(){ return principal * bonus / 100 ; }
12
        virtual double getMaturity() = 0;
13
};
14
class Customer : public FD{
15
    public:
16
        Customer(double _amt) : FD(_amt){}
17
        double getMaturity();
18
};
19
class Employee : public FD{
20
    public:
21
        Employee(double _amt) : FD(_amt){}
22
        double getMaturity();
23
};
24
double Customer::getMaturity(){
25
26
    return getInterest() + principal ; //LINE-1
27
}
28
29
30
double Employee::getMaturity(){
31
32
    return principal + getInterest() + getExtraAmt() ; //LINE-2
33
}
0
int main(){
1
    double d;
2
    cin >> d;
3
    FD *ep[2] = {new Customer(d), new Employee(d)};
4
    cout << ep[0]->getMaturity() << " ";
5
    cout << ep[1]->getMaturity();
6
    return 0;
7
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
100 100
107 111
107 111
Passed
Test Case 2
1000 5000
1070 1110
1070 1110
Passed





Private Test cases used for EvaluationStatus
Test Case 1
Passed




W6_Programming_Qs-3

Due on 2024-03-07, 23:59 IST
Your last recorded submission was on 2024-02-24, 20:26 IST
Select the Language for this assignment. 
1
#include<iostream>
2
using namespace std;
3
class A{
4
    public:
5
        A(){ cout << "11 "; }
6
        A(int n){ cout << n + 2 << " "; }
7
        virtual ~A(); //LINE-1
8
};
9
10
class B : public A{
11
    public:
12
        B(int n) : A(n)  //LINE-2
13
        { 
14
            cout << n + 5 << " "; 
15
        } 
16
        B(){ cout << "21 "; }
17
        virtual ~B(){ cout << "22 "; }
18
};
0
A::~A(){ cout << "12 "; }
1
2
int main(){
3
    int i;
4
    cin >> i;
5
    A *pt = new B(i);
6
    delete pt;
7
    return 0;
8
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
4
6 9 22 12
6 9 22 12 
Passed after ignoring Presentation Error
Test Case 2
5
7 10 22 12
7 10 22 12 
Passed after ignoring Presentation Error





Private Test cases used for EvaluationStatus
Test Case 1
Passed







W7_Programming_Qs-1

Due on 2024-03-14, 23:59 IST
Your last recorded submission was on 2024-03-04, 11:45 IST
Select the Language for this assignment. 
1
#include<iostream>
2
using namespace std;
3
class Test1{
4
    int a = 10;
5
    public:
6
        void show(){
7
            cout << a ;
8
        }
9
        void operator=(int x){ //LINE-1
10
            a = a + x;
11
        }
12
};
13
class Test2 : public Test1{
14
    int b = 20;
15
    public:
16
        void show(){
17
            cout << b << " ";
18
        }
19
};
20
void fun(const Test2 &t, int x){
21
    Test2 &u = const_cast<Test2&>(t); //LINE-2
22
    u.show();
23
    Test1 &v = reinterpret_cast<Test1&>(u); //LINE-3
24
    v = x;
25
    v.show();
26
}
0
int main(){
1
    Test2 t1;
2
    int x;
3
    cin >> x;
4
    fun(t1, x);
5
    return 0;
6
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
4
20 14
20 14
Passed
Test Case 2
9
20 19
20 19
Passed



W7_Programming_Qs-2

Due on 2024-03-14, 23:59 IST
Your last recorded submission was on 2024-03-04, 12:01 IST
Select the Language for this assignment. 
1
#include<iostream>
2
using namespace std;
3
class StringClass{
4
    int *arr;
5
    int n;
6
    public:
7
        StringClass(int k) : n(k), arr(new int(n)){} //LINE-1
8
        operator int(){ //LINE-2
9
            return arr[--n] ;
10
        }
11
        StringClass operator=(int k){ //LINE-3
12
            int t;
13
            for(int j = 0; j < k; j++){
14
                cin >> t;
15
                this->arr[j] = t;
16
            }
17
            return *this;
18
        }
19
};
0
int main(){
1
    int k;
2
    cin >> k;
3
    StringClass str(k);
4
    str = k;
5
    for(int i = 0; i < k; i++)
6
        cout << static_cast<int>(str) << " ";
7
    return 0;
8
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
3 
1 3 5
5 3 1
5 3 1 
Passed after ignoring Presentation Error
Test Case 2
4
5 6 4 8
8 4 6 5
8 4 6 5 
Passed after ignoring Presentation Error



W7_Programming_Qs-3

Due on 2024-03-14, 23:59 IST
Your last recorded submission was on 2024-03-04, 12:11 IST
Select the Language for this assignment. 
1
#include<iostream>
2
using namespace std;
3
class B {
4
    public:
5
        B(int i) { cout << 50 * i << " "; }
6
        B() { cout << 1 << " "; }
7
};
8
class D1 : virtual public B { //LINE-1
9
    public:
10
        D1(int i);
11
};
12
class D2 : virtual public B { //LINE-2
13
    public:
14
        D2(int i);
15
};
16
class D3 : virtual public B { //LINE-3
17
    public:
18
        D3(int i);
19
};
20
class DD : public D2, public D1, public D3 { //LINE-4
21
    public:
22
        DD (int i) : D1(i), D2(i), D3(i) {
23
            cout << 2 * i << " ";
24
        }
25
};
0
D1::D1(int i) : B(i) {
1
    cout << 5 * i << " ";
2
}
3
D2::D2(int i) : B(i) {
4
    cout << 4 * i << " ";
5
}
6
D3::D3(int i) : B(i) {
7
    cout << 3 * i << " ";
8
}
9
int main() {
10
    int i = 0;
11
    cin >> i;
12
    DD dd(i);
13
    return i;
14
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
2
1 8 10 6 4
1 8 10 6 4 
Passed after ignoring Presentation Error
Test Case 2
3
1 12 15 9 6
1 12 15 9 6 
Passed after ignoring Presentation Error





W8_Programming_Qs-1

Due on 2024-03-21, 23:59 IST
Your last recorded submission was on 2024-03-10, 22:48 IST
Select the Language for this assignment. 
1
#include <iostream>
2
3
class Computation{
4
    public:
5
        int sum(int a, int b) { return a + b; }
6
        int sub(int a, int b) { return a - b; }
7
};
8
int call(Computation *obj, int x, int y, int(Computation::*fp)(int, int)){    //LINE-1
9
10
    return  (obj->*fp)(x,y);    //LINE-2
11
}
0
int main() {
1
    int a, b;
2
    Computation *c;
3
    std::cin >> a >> b;
4
    std::cout << call(c, a, b, &Computation::sum) << " ";
5
    std::cout << call(c, a, b, &Computation::sub);
6
    return 0;
7
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
10 20
30 -10
30 -10
Passed
Test Case 2
10 10
20 0
20 0
Passed



W8_Programming_Qs-2

Due on 2024-03-21, 23:59 IST
Your last recorded submission was on 2024-03-10, 23:20 IST
Select the Language for this assignment. 
1
#include<iostream>
2
#include<algorithm>
3
#include<vector>
4
5
struct max {    
6
    max(int cnt=0, int ss=0) : cnt_(cnt), ss_(ss)  {}    //LINE-1
7
8
    void operator() (int x ) { ss_ += x*x; ++cnt_; }    //LINE-2
9
10
    int cnt_;    //count of element
11
    int ss_;     //sum of square of emements
12
};
0
int main(){
1
    std::vector<int> v;
2
    int n, a;
3
    std::cin >> n;
4
    for(int i = 0; i < n; i++){
5
        std::cin >> a;
6
        v.push_back(a);
7
    }
8
    max mi = for_each(v.begin(), v.end(), max());
9
    std::cout << "mean = " << (double)mi.ss_/mi.cnt_;
10
    return 0;   
11
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
4 1 2 3 4
mean = 7.5
mean = 7.5
Passed
Test Case 2
5 10 20 30 40 50
mean = 1100
mean = 1100
Passed



W8_Programming_Qs-3

Due on 2024-03-21, 23:59 IST
Your last recorded submission was on 2024-03-10, 23:49 IST
Select the Language for this assignment. 
1
#include<iostream>
2
3
class OutOfArray{
4
    public: 
5
        virtual void what(){ std::cout << "index out of array"; }
6
};
7
8
class InvalidElement{
9
    public: 
10
        virtual void what(){ std::cout << "invalid"; }
11
};
12
template<typename T, int N>    //LINE-1
13
14
class plist{
15
    public:
16
        plist(){
17
            for(int i = 0; i < N; i++)
18
                arr_[i] = -1;
19
        }
20
        //LINE-2: impelement insert() function
21
        void insert(int i, T val)
22
        {
23
          if ( i >= N )
24
            throw OutOfArray();
25
          else 
26
            arr_[i] = val;
27
        }
28
          
29
        //LINE-3: impelement peek() function
30
        T peek(int i )
31
        {
32
          if ( arr_[i] < 0 )
33
            throw InvalidElement();
34
          else 
35
            return arr_[i];
36
        }
37
        
38
    private:
39
        T arr_[N];
40
};
0
int main(){
1
    int n;
2
    char c;
3
    plist<char, 4> li;
4
    try{
5
        for(int i = 0; i < 4; i++){
6
            std::cin >> n;
7
            std::cin >> c;
8
            li.insert(n, c);
9
        }
10
    }catch(OutOfArray& e){
11
        e.what();
12
        std::cout << std::endl;
13
    }
14
    for(int i = 0; i < 4; i++){
15
        try{
16
            std::cout << li.peek(i) << ", ";
17
        }catch(InvalidElement& e){
18
            e.what();
19
            std::cout << ", ";
20
        }
21
    }
22
    return 0;
23
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
1 a 2 b 3 c 0 x
x, a, b, c,
x, a, b, c, 
Passed after ignoring Presentation Error
Test Case 2
2 a 1 x 3 z 2 y
invalid, x, y, z,
invalid, x, y, z, 
Passed after ignoring Presentation Error






































































































































































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.