Programming in Modern C++ Week 8 Programming Assignments [ Jan-2022 | NPTEL ]

 

Week 8: Programming Assignment 1

Due on 2022-03-24, 23:59 IST
Consider the following program. Fill in the blanks as per the instructions given below:
•at LINE-1 with appropriate template definition,
•at LINE-2 and LINE-3 with appropriate template and function header for add() function,
•at LINE-4 and LINE-5 with appropriate template and function header to overload function call operator,
such that it will satisfy the given test cases.
Your last recorded submission was on 2022-03-24, 19:49 IST
Select the Language for this assignment. 
1
#include<iostream>
using namespace std;

template<class T>    //LINE-1
class ArrayList{
    private:
        T* list;
        int size;
        int idx;
    public:
        ArrayList(int size_){
            size = size_;
            list = new T[size];
            idx = -1;
        }
        void add(T item);
        void operator()();
};
template<class T>//LINE-2
void ArrayList<T>::add( T item ){    //LINE-3
    if(idx < size)
        list[++idx] = item;
}
template<class T>      //LINE-4 
void ArrayList<T>::operator()() {    //LINE-5
    for(int i = size -1; i >= 0; i--){
        cout << list[i] << " ";
    }
}
0
int main(){
1
    int n, m;
2
    char c;
3
    cin >> n;
4
    ArrayList<int> iList(n);
5
    for(int i = 0; i < n; i++){
6
        cin >> m;
7
        iList.add(m);
8
    }
9
    ArrayList<char> cList(n);
10
    for(int i = 0; i < n; i++){
11
        cin >> c;
12
        cList.add(c);
13
    }
14
    iList();
15
    cList();    
16
    return 0;
17
}
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.
   



InputOutput
Test Case 1
4
10 20 30 40
a b c d
40 30 20 10 d c b a 
Test Case 2
5
1 2 3 4 5
e f g h i
5 4 3 2 1 i h g f e 





Week 8: Programming Assignment 2

Due on 2022-03-24, 23:59 IST
Consider the following program. Fill in the blanks as per the instructions given below to
complete the definition of functor Maximum with local state:
•at LINE-1 with appropriate unary_function statement,
•at LINE-2 with constructor header with initialization list,
•at LINE-3 with overloading the function call operator,
such that it will satisfy the given test cases.
Your last recorded submission was on 2022-03-24, 19:51 IST
Select the Language for this assignment. 
1
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;

struct Maximum : public unary_function<int, void>{    //LINE-1
    int count;
    int sum;    
    Maximum(): sum(0), count(0) {}    //LINE-2
  void operator() (int x) { sum+=x;count++; }//LINE-3
};
0
int main(){
1
    vector<int> v;
2
    int n, a;
3
    cin >> n;
4
    for(int i = 0; i < n; i++){
5
        cin >> a;
6
        v.push_back(a);
7
    }
8
    Maximum m = for_each(v.begin(), v.end(), Maximum());
9
    cout << "avg = " << (double)m.sum / m.count;
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.
   



InputOutput
Test Case 1
4 10 20 30 40
avg = 25
Test Case 2
5 3 6 7 10 12
avg = 7.6




Week 8: Programming Assignment 3

Due on 2022-03-24, 23:59 IST
Consider the following program. Fill in the blanks as per the instructions given below:
•at LINE-1 with definition of pointer to function type F_ptr, which can points functions
 message_to_client1() and message_to_client2(),
•at LINE-2 with appropriate header of the function wrapper(),
•at LINE-3 with function call using the pointer to function,
such that it will satisfy the given test cases.
Your last recorded submission was on 2022-03-24, 19:52 IST
Select the Language for this assignment. 
1
#include<iostream>
2
using namespace std;
3
4
class Message{
5
    private:
6
        string greet;
7
    public:
8
        Message(string greet_) : greet(greet_){}
9
        void message_to_client1(string m){
10
            cout << "client-1: " << greet << " from " << m << endl;
11
        }
12
        void message_to_client2(string m){
13
            cout << "client-2: " << greet << " from " << m;
14
        }
15
};
16
void (Message::*F_ptr)(string);    //LINE-1

void wrapper(Message *op,void (Message::*F_ptr)(string), string n){ //LINE-2
    (op->*F_ptr)(n);         //LINE-3
}
0
int main(){
1
    string s, n1, n2;
2
    cin >> s >> n1 >> n2;
3
    Message *op = new Message(s);
4
    wrapper(op, &Message::message_to_client1, n1);
5
    wrapper(op, &Message::message_to_client2, n2);
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.
   



InputOutput
Test Case 1
hello bhaskar nalini
client-1: hello from bhaskar
client-2: hello from nalini
Test Case 2
welcome soumen anand
client-1: welcome from soumen
client-2: welcome from anand


No comments

JavaFX Scene Builder

  This is an article about the JavaFX Scene Builder. You will get a short introduction about the installation and usage of the software. The...