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 week 9 to 12 Swayam

NPTEL Programming in Modern C++ Programming Assignment Jan-2024 week 9 to 12 Swayam

   Please scroll down for latest Programs ðŸ‘‡ 


W9_Programming_Qs-1

Due on 2024-03-28, 23:59 IST
Your last recorded submission was on 2024-03-18, 22:22 IST
Select the Language for this assignment. 
1
#include<iostream>
2
#include <algorithm>
3
#include<vector>
4
5
class player{
6
    public:
7
        player(int rank, std::string name) : rank_(rank), name_(name){}
8
        int get_rank(){ return rank_; }
9
        std::string get_name(){ return name_; }
10
    private:
11
        int rank_;
12
        std::string name_;
13
};
14
struct cmpByName{
15
    //code-segment-1
16
  bool operator()( player& e1,  player& e2) {
17
    return e1.get_name().length() < e2.get_name().length();
18
}
19
};
20
21
struct cmpByRank{
22
    //code-segment-2
23
  bool operator()( player& e1,  player& e2) {
24
    return e1.get_rank() < e2.get_rank();
25
}
26
};
0
int main() {
1
    int a, c;
2
    std::string b;
3
    
4
    std::vector<player> pv;
5
    for(int i = 0; i < 4; i++){
6
        std::cin >> a >> b;
7
        player p(a, b);
8
        pv.push_back(p);
9
    }
10
        
11
    std::cin >> c;
12
    if(c == 1)
13
        std::sort(pv.begin(), pv.end(), cmpByRank());
14
    else if(c == 2)
15
        std::sort(pv.begin(), pv.end(), cmpByName());
16
    for(std::vector<player>::iterator it = pv.begin(); it < pv.end(); it++)
17
        std::cout << it->get_rank() << " : " << it->get_name() << std::endl;
18
    return 0;
19
}
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
20 raj
10 dilip
30 subendu
40 nalini
1
10 : dilip\n
20 : raj\n
30 : subendu\n
40 : nalini
10 : dilip\n
20 : raj\n
30 : subendu\n
40 : nalini\n
Passed after ignoring Presentation Error
Test Case 2
20 raj
10 dilip
30 subendu
40 nalini
2
20 : raj\n
10 : dilip\n
40 : nalini\n
30 : subendu
20 : raj\n
10 : dilip\n
40 : nalini\n
30 : subendu\n
Passed after ignoring Presentation Error





W9_Programming_Qs-2

Due on 2024-03-28, 23:59 IST
Your last recorded submission was on 2024-03-18, 22:23 IST
Select the Language for this assignment. 
1
#include <iostream>
2
#include <map>
3
#include <list>
4
5
std::map<int, int> findFrequency(std::list<int> li){
6
    std::map<int, int> fqMap; 
7
    for ( std::list<int>::iterator it = li.begin(); it != li.end(); ++it)   //LINE-1
8
        fqMap[*it]++;
9
    return fqMap;
10
}
11
12
void print(std::map<int, int> fq){
13
    for (std::map<int, int>::iterator it = fq.begin(); it != fq.end(); ++it)    //LINE-2
14
        std::cout << it->first << " => " << it->second << ", ";
15
}
0
int main() { 
1
    std::list<int> li;
2
    int a;
3
    for(int i = 0; i < 10; i++){
4
        std::cin >> a;
5
        li.push_back(a);
6
    }
7
    std::map<int, int> fq = findFrequency(li); 
8
    print(fq);  
9
    return 0;
10
}
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 30 50 20 30 40 50 10 20 10
10 => 3, 20 => 2, 30 => 2, 40 => 1, 50 => 2,
10 => 3, 20 => 2, 30 => 2, 40 => 1, 50 => 2, 
Passed after ignoring Presentation Error
Test Case 2
10 20 10 30 10 40 10 50 20 30
10 => 4, 20 => 2, 30 => 2, 40 => 1, 50 => 1,
10 => 4, 20 => 2, 30 => 2, 40 => 1, 50 => 1, 
Passed after ignoring Presentation Error









W9_Programming_Qs-3

Due on 2024-03-28, 23:59 IST
Your last recorded submission was on 2024-03-18, 22:25 IST
Select the Language for this assignment. 
1
#include<iostream>
2
#include<list>
3
4
template<typename T>
5
struct boundIt{
6
    T ub_, lb_;
7
    boundIt(T lb = 0, T ub = 0) : ub_(ub), lb_(lb) { }
8
9
    bool operator()(T x){ return (x <= ub_ && x >= lb_); }    
10
};
11
template<class T, class Pred>                         //LINE-1
12
T find_if(T first, T last, Pred prd) {
13
    while (first != last && !prd(*first)) ++first;         //LINE-2
14
        return first;
15
}
16
17
void print(std::list<int> li, int lb, int ub){
18
    boundIt<int> bnd(lb, ub);
19
    std::list<int>::iterator it = find_if(li.begin(), li.end(), bnd);    //LINE-3
20
    while(it != li.end()){
21
        std::cout << *it << " ";
22
        it = find_if(++it, li.end(), bnd);
23
    }
24
}
0
int main(){
1
    std::list<int> li {30, 70, 10, 40, 20, 50, 60, 80};
2
    int l, u;
3
    std::cin >> l >> u;
4
    print(li, l, u);
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
30 55
30 40 50
30 40 50 
Passed after ignoring Presentation Error
Test Case 2
55 88
70 60 80
70 60 80 
Passed after ignoring Presentation Error







W10_Programming_Qs-1

Due on 2024-04-04, 23:59 IST
Your last recorded submission was on 2024-03-27, 09:15 IST
Select the Language for this assignment. 
1
#include <iostream>
2
3
int convert(double d){
4
    return int(d);
5
}
6
double convert(int i){
7
    return double(i);
8
}
9
10
template<typename T, typename U>    //LINE-1
11
12
auto divide(T n1, U n2) -> decltype(convert(n1) / convert(n2)) {   //LINE-2
13
    return convert(n1) / convert(n2);
14
}
0
int main(){
1
    int a, b;
2
    double c, d;
3
    std::cin >> a >> b >> c >> d;
4
    std::cout << divide(a, b) << " ";
5
    std::cout << divide(a, c) << " ";
6
    std::cout << divide(c, d);
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
50 40 30 20
1.25 1.66667 1
1.25 1.66667 1
Passed
Test Case 2
10 6 3 2
1.66667 3.33333 1
1.66667 3.33333 1
Passed



Private Test cases used for EvaluationStatus
Test Case 1
Passed



W10_Programming_Qs-2

Due on 2024-04-04, 23:59 IST
Your last recorded submission was on 2024-03-27, 09:16 IST
Select the Language for this assignment. 
1
#include <iostream>
2
3
class ctype { 
4
    public: 
5
        ctype() : cp_(nullptr) {  }
6
        ctype(char c) : cp_(new char(c)) { }
7
        ctype(const ctype& ob) : cp_(new char(*(ob.cp_))) {}  // LINE-1: copy constructor 
8
        ctype& operator=(const ctype& ob) {            // LINE-2: copy assignment 
9
            if (this != &ob) { 
10
                delete cp_;  
11
                cp_ = new char(*(ob.cp_) + 1); 
12
            } 
13
            return *this;
14
        }
15
        ctype(ctype&& ob) noexcept : cp_(ob.cp_) {         // LINE-3: move constructor 
16
            ob.cp_ = nullptr; 
17
        }  
18
        ctype& operator=(ctype&& ob) noexcept {               // LINE-4: move assignment 
19
            if (this != &ob) { 
20
                cp_ = ob.cp_; 
21
                ob.cp_ = nullptr; 
22
            } 
23
            return *this;
24
        }
0
void print(){
1
            if(cp_ == nullptr)
2
                std::cout << "moved, ";
3
            else
4
                std::cout << *cp_ << ", ";
5
        }
6
        ~ctype() { delete cp_; }
7
    private:
8
        char* cp_ = nullptr;
9
};
10
int main(){
11
    char a;
12
    std::cin >> a;
13
    
14
    ctype c1(a);  ctype c2 = c1;
15
    ctype c3 = c1;
16
    
17
    c1.print(); c2.print(); c3.print();
18
    
19
    ctype c4 = std::move(c1);
20
    ctype c5; c5 = std::move(c1);
21
    c1.print(); c4.print(); c5.print();
22
23
    return 0;
24
}
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
A
A, A, A, moved, A, moved,
A, A, A, moved, A, moved, 
Passed after ignoring Presentation Error
Test Case 2
X
X, X, X, moved, X, moved,
X, X, X, moved, X, moved, 
Passed after ignoring Presentation Error


Private Test cases used for EvaluationStatus
Test Case 1
Passed



W10_Programming_Qs-3

Due on 2024-04-04, 23:59 IST
Your last recorded submission was on 2024-03-27, 09:16 IST
Select the Language for this assignment. 
1
#include <iostream>
2
class compex_num{
3
    public:
4
        explicit compex_num(double r = 0, double i = 0) : r_(r), i_(i){}
5
        compex_num operator+(double n){
6
            compex_num res;
7
            res.r_ = this->r_ + n;
8
            res.i_ = this->i_;
9
            return res;
10
        }
11
        compex_num operator+(compex_num c){
12
            compex_num res;
13
            res.r_ = this->r_ + c.r_;
14
            res.i_ = this->i_ + c.i_;
15
            return res;
16
        }
17
        friend std::ostream& operator<<(std::ostream& os, const compex_num& c);
18
    private:
19
        double r_, i_;
20
};
21
std::ostream& operator<<(std::ostream& os, const compex_num& c){
22
    os << c.r_ << " + j" << c.i_;
23
    return os;
24
}
25
template<typename T, typename U>           //LINE-1
26
27
decltype(auto) add(T n1, U n2) {         //LINE-2
28
29
    typedef decltype(n1 + n2) Tmp;    // LINE-3: define new type Tmp
30
    Tmp sum = n1 + n2;
31
    
32
    std::cout << sum << std::endl;
33
}
0
int main(){
1
    int a, b, r1, i1, r2, i2;
2
    std::cin >> a >> b >> r1 >> i1 >> r2 >> i2;
3
    compex_num c1(r1, i1);
4
    compex_num c2(r2, i1);
5
    add(c1, a);
6
    add(c1, c2);
7
    add(a, b);
8
    return 0;
9
}
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 40 50 -60
40 + j40\n
80 + j80\n
30
40 + j40\n
80 + j80\n
30\n
Passed after ignoring Presentation Error
Test Case 2
-1 2 3 4 -2 -4
2 + j4\n
1 + j8\n
1
2 + j4\n
1 + j8\n
1\n
Passed after ignoring Presentation Error



Private Test cases used for EvaluationStatus
Test Case 1
Passed





W11_Programming_Qs-1

Due on 2024-04-11, 23:59 IST
Your last recorded submission was on 2024-04-02, 08:15 IST
Select the Language for this assignment. 
1
#include <iostream>
2
#include <functional>
3
     
4
int main(){
5
    std::function<int(int, int)> recPow;            //LINE-1
6
    recPow =  [&recPow](int n, int m) -> int  {       //LINE-2
7
        return m == 1 ? n : n * recPow(n, m - 1); 
8
    };
9
    auto print =  [&recPow](int n, int m) {          //LINE-3
10
        std::cout << recPow(n, m); 
11
    };
12
    int n, m;
13
    std::cin >> n >> m;
14
    print(n, m);
15
    return 0;
16
}
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 3
64
64
Passed
Test Case 2
2 5
32
32
Passed



W11_Programming_Qs-2

Due on 2024-04-11, 23:59 IST
Your last recorded submission was on 2024-04-02, 08:20 IST
Select the Language for this assignment. 
1
#include <iostream>
2
3
template <typename T>       //LINE-1
4
double getmin(T num){ return num; }    //LINE-2
5
6
template <typename T, typename... Tail>       //LINE-3
7
double getmin(T num, Tail... nums){     //LINE-4  
8
    return num <= getmin(nums...) ? num : getmin(nums...);   //LINE-4
9
}
0
int main(){
1
    int a, b, c;
2
    double d, e, f, g;
3
    std::cin >> a >> b >> c;
4
    std::cin >> d >> e >> f >> g;
5
    std::cout << getmin(a, b, c) << " ";
6
    std::cout << getmin(d, e, f, g) << " ";
7
    std::cout << getmin(a, b, c, d, e, f, g) << " ";
8
    return 0;
9
}
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 5
3.4 5.7 1.2 8.9
1 1.2 1
1 1.2 1 
Passed after ignoring Presentation Error
Test Case 2
4 3 2
9.8 1.2 3.4 .9
2 0.9 0.9
2 0.9 0.9 
Passed after ignoring Presentation Error




W11_Programming_Qs-3

Due on 2024-04-11, 23:59 IST
Your last recorded submission was on 2024-04-02, 09:02 IST
Select the Language for this assignment. 
1
#include <iostream>
2
class payment{
3
    public:
4
        virtual void pay() = 0;
5
};
6
7
class upi_payment : public payment {
8
    public:
9
        upi_payment(const int& upi_num) : upi_num_{upi_num}{ 
10
            std::cout << "lvalue" << " "; 
11
        }
12
        upi_payment(int&& upi_num) : upi_num_{upi_num}{
13
            std::cout << "rvalue" << " "; 
14
        }
15
        void pay(){ std::cout << upi_num_ << " "; }
16
    private:
17
        int upi_num_;
18
};
19
20
class bank_transfer : public payment {
21
    public:
22
        bank_transfer(const int& acc_no) : acc_no_(acc_no){
23
            std::cout << "lvalue" << " "; 
24
        }
25
        bank_transfer(int&& acc_no) : acc_no_(acc_no){ 
26
            std::cout << "rvalue" << " "; 
27
        }
28
        void pay(){ std::cout << acc_no_ << " "; }
29
    private:
30
        int acc_no_;
31
};
32
33
template <typename T1, typename Arg>          //LINE-1
34
35
T1 makePayment(Arg&& a){                     //LINE-2
36
37
    return T1(std::forward<Arg>(a));;       //LINE-3
38
}
0
int main() {
1
    int a, b;
2
    std::cin >> a >> b;
3
    
4
    auto p1 = makePayment<upi_payment>(a);
5
    auto p2 = makePayment<upi_payment>(std::move(a));
6
    auto p3 = makePayment<bank_transfer>(b);
7
    auto p4 = makePayment<bank_transfer>(std::move(b));
8
    std::cout << std::endl;
9
    p1.pay();
10
    p2.pay();
11
    p3.pay();
12
    p4.pay();
13
    return 0;
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
10 20
lvalue rvalue lvalue rvalue \n
10 10 20 20
lvalue rvalue lvalue rvalue \n
10 10 20 20 
Passed after ignoring Presentation Error
Test Case 2
5 3
lvalue rvalue lvalue rvalue \n
5 5 3 3
lvalue rvalue lvalue rvalue \n
5 5 3 3 
Passed after ignoring Presentation Error





W12_Programming_Qs-1

Due on 2024-04-18, 23:59 IST
Your last recorded submission was on 2024-04-08, 17:54 IST
Select the Language for this assignment. 
1
#include <iostream>
2
#include <future>
3
#include <cmath>
4
5
struct prime_numbers{
6
    const long long n_;
7
    prime_numbers(const long long& n) : n_(n) { }
8
    bool is_prime(long long m){
9
        if (m <= 1)
10
            return false;
11
        for (int i = 2; i <= sqrt(m); i++)
12
            if (m % i == 0)
13
                return false;
14
 
15
        return true;
16
    }
17
long long operator()() {     //LINE-1
18
        long long num = 2;
19
        for(int i = 0; i < n_; num++) {
20
            if (is_prime(num)) {
21
                ++i;
22
            }
23
        }
24
        return num - 1;
25
    }
26
};
27
28
long long get_nth_prime(int n){
29
    auto a = std::async(prime_numbers(n)) ;    //LINE-2
30
31
    return  a.get();                  //LINE-3    
32
}
0
int main() {
1
    int n;
2
    std::cin >> n;
3
    std::cout << get_nth_prime(n);
4
    return 0;
5
}
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
150
863
863
Passed
Test Case 2
200
1223
1223
Passed



W12_Programming_Qs-2

Due on 2024-04-18, 23:59 IST
Your last recorded submission was on 2024-04-08, 18:00 IST
Select the Language for this assignment. 
1
#include<iostream>
2
#include<memory>
3
4
class node;
5
class dlist{
6
    public:
7
        dlist() = default;
8
        void insert(const int& item);
9
        void forward_traverse();
10
        void backward_traverse();    
11
    private:
12
        std::shared_ptr<node> first = nullptr;
13
        std::shared_ptr<node> last = nullptr;
14
};
15
class node{
16
    public:
17
        node() = delete;
18
        node(int info) : info_{info}, next{nullptr} {}
19
        friend dlist;;       //LINE-1
20
    private:
21
        int info_;
22
        std::shared_ptr<node> next;
23
        std::weak_ptr<node> prev;
24
};
25
void dlist::forward_traverse(){
26
    for(std::shared_ptr<node> t = first; t != nullptr; t = t->next)   //LINE-2
27
        std::cout << t->info_ << " ";
28
}
29
void dlist::backward_traverse(){
30
    for(std::weak_ptr<node> t = last; auto p = t.lock(); t = p->prev)   //LINE-3
31
        std::cout << p->info_ << " ";
32
}
0
void dlist::insert(const int& item){
1
    std::shared_ptr<node> n = std::make_shared<node>(item);
2
    if(first == nullptr){
3
        first = n;
4
        last = first;
5
    }
6
    else{
7
        n->next = first;
8
        first->prev = n;
9
        first = n;
10
    }
11
}
12
13
int main(){
14
    dlist dl;
15
    int n, m;
16
    std::cin >> n;
17
    for(int i = 0; i < n; i++){
18
        std::cin >> m;
19
        dl.insert(m);
20
    }
21
    dl.forward_traverse();
22
    std::cout << std::endl;
23
    dl.backward_traverse();
24
    return 0;
25
}
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
10 20 30 40
40 30 20 10 \n
10 20 30 40
40 30 20 10 \n
10 20 30 40 
Passed after ignoring Presentation Error
Test Case 2
5
1 2 3 4 5
5 4 3 2 1 \n
1 2 3 4 5
5 4 3 2 1 \n
1 2 3 4 5 
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.