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 July-2024 | Swayam

NPTEL Programming in Modern C++ Programming Assignment July-2024 | Swayam

 

NPTEL » Programming in Modern C++


  Please scroll down for latest Programs. ðŸ‘‡ 




W1_Programming-Qs.1

Due on 2024-08-08, 23:59 IST

Consider the program below.
• Fill in the blank at LINE-1 to declare a stack variable st.
• Fill in the blank at LINE-2 to push values into the stack.
• Fill in the blank at LINE-3 with the appropriate statement.
The program must satisfy the given test cases.
Select the Language for this assignment. 
1
#include<iostream>
2
#include<cstring>
3
#include<stack>
4
using namespace std;
5
int main() {
6
    char input[20];
7
    char character;
8
    cin >> input;
9
stack<char> st; //LINE-1
10
11
    for(int i = 0; i < strlen(input); i++)
12
13
         st.push(input[i]); //LINE-2
14
15
    for(int i = 0; i < strlen(input); i++) {
16
17
        character =  st.top();  //LINE-3
0
cout << character;
1
        st.pop();
2
    }
3
    return 0;
4
}
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
coding
gnidoc
gnidoc
Passed
Test Case 2
reverse
esrever
esrever
Passed



W1_Programming-Qs.2

Due on 2024-08-08, 23:59 IST
Consider the following program.
• Fill in the blank at LINE-1 with the appropriate if statement,
• Fill in the blank at LINE-2 and LINE-3 with the appropriate return statements.
The program must satisfy the sample input and output.
Select the Language for this assignment. 
1
#include<iostream>
2
#include<string>
3
using namespace std;
4
bool IsLonger(string str1, string str2){
5
6
    if(str1.length() > str2.length()) //LINE-1
7
8
        return true; //LINE-2
9
10
    else
11
         return false; //LINE-3
12
}
0
int main(){
1
    string str1, str2;
2
3
    cin >> str1 >> str2;
4
5
    cout << str1 << ", " << str2 << " : " << IsLonger(str1, str2);
6
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
apple banana
apple, banana : 0
apple, banana : 0
Passed
Test Case 2
notebook note
notebook, note : 1
notebook, note : 1
Passed



W1_Programming-Qs.3

Due on 2024-08-08, 23:59 IST
Select the Language for this assignment. 
1
#include <iostream>
2
#include <cmath>     //LINE-1 
3
4
using namespace std;
5
struct Point{
6
    int x, y;
7
};
8
9
double calculate_distance(Point pt1, Point pt2){
10
11
    return abs(pt1.x - pt2.x) + abs(pt1.y - pt2.y) ;    //LINE-2
12
}
0
int main() { 
1
    int x1, y1, x2, y2;
2
    cin >> x1 >> y1 >> x2 >> y2;
3
    Point pt1, pt2;
4
    pt1.x = x1;
5
    pt1.y = y1;
6
    pt2.x = x2;
7
    pt2.y = y2;
8
    cout << calculate_distance(pt1, pt2);
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
2 5 3 8
4
4
Passed
Test Case 2
10 20 40 20
30
30
Passed





W2_Programming-Qs.1

Due on 2024-08-08, 23:59 IST
Your last recorded submission was on 2024-08-01, 21:06 IST
Select the Language for this assignment. 
1
#include <iostream>
2
using namespace std;
3
4
float compute(const float &x){ //LINE-1
5
6
    return (x - 1); //LINE-2
7
}
0
int main(){
1
    float y;
2
    cin >> y;
3
    cout << compute(y * 2);
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
3.5
6
6
Passed
Test Case 2
2.0
3
3
Passed




Private Test cases used for EvaluationStatus
Test Case 1
Passed



W2_Programming-Qs.2

Due on 2024-08-08, 23:59 IST
Select the Language for this assignment. 
1
#include <iostream>
2
#include <string>
3
using namespace std;
4
5
string reverseString(const string& str) {
6
    string reversed;// LINE-1
7
    for (int i = str.length() - 1; i >= 0; i--) {
8
        reversed += str[i];
9
    }
10
    return reversed;
11
}
12
13
bool isPalindrome(const string& str) {
14
    string reversedStr = reverseString(str) ;// LINE-2
15
    return str == reversedStr;
16
}
0
int main(){
1
    string input;
2
    cin >> input;
3
4
    string reversedInput = reverseString(input);
5
    cout << "Reversed: " << reversedInput << endl;
6
7
    if (isPalindrome(input)) {
8
        cout << "The string is a palindrome." << endl;
9
10
    } else {
11
        cout << "The string is not a palindrome." << endl;
12
    }
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
level
Reversed: level\n
The string is a palindrome.
Reversed: level\n
The string is a palindrome.\n
Passed after ignoring Presentation Error
Test Case 2
hello
Reversed: olleh\n
The string is not a palindrome.
Reversed: olleh\n
The string is not a palindrome.\n
Passed after ignoring Presentation Error




Private Test cases used for EvaluationStatus
Test Case 1
Passed



W2_Programming-Qs.3

Due on 2024-08-08, 23:59 IST
Select the Language for this assignment. 
1
#include <iostream>
2
using namespace std;
3
4
struct Vector{
5
    int x;
6
    int y;
7
};
8
9
struct Vector operator+(const Vector& v1, const Vector& v2){ //LINE-1
10
    Vector v;
11
    v.x = v1.x + v2.x;
12
    v.y = v1.y + v2.y;
13
    return v;
14
}
15
16
Vector wrapper(const Vector v1, const Vector v2){
17
    Vector v = v1 + v2; //LINE-2
18
    return v;
19
}
0
int main(){
1
    int a, b, c, d;
2
    cin >> a >> b >> c >> d;
3
    Vector v1 = {a, b}, v2 = {c, d};
4
    Vector result = wrapper(v1, v2);
5
    cout << result.x << " " << result.y;
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
1 2 3 4
4 6
4 6
Passed
Test Case 2
5 5 10 10
15 15
15 15
Passed





Private Test cases used for EvaluationStatus
Test Case 1
Passed




W3_Programming-Qs.1

Due on 2024-08-15, 23:59 IST
Your last recorded submission was on 2024-08-08, 15:30 IST
Select the Language for this assignment. 
1
#include<iostream>
2
using namespace std;
3
4
class Pair {
5
    const int *a, *b;
6
    public:
7
        Pair(int val1, int val2) : a(new int(val1)), b(new int(val2)){}    //LINE-1
8
        ~Pair(){  delete a; delete b; }    //LINE-2
9
        int getA(){ return *a; }    //LINE-3
10
        int getB(){ return *b; }    //LINE-4
11
};
0
int main(){
1
    int x, y;
2
    cin >> x >> y;
3
    Pair p(x, y);
4
    cout << "[" << p.getA() << ", " << p.getB() << "]";
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
3 4
[3, 4]
[3, 4]
Passed
Test Case 2
7 8
[7, 8]
[7, 8]
Passed


W3_Programming-Qs.2

Due on 2024-08-15, 23:59 IST
Your last recorded submission was on 2024-08-08, 15:31 IST
Select the Language for this assignment. 
1
#include<iostream>
2
using namespace std;
3
4
class Calculator {
5
    int a, b;
6
    mutable int total;                                      //LINE-1
7
    public:
8
        Calculator(int a_, int b_) : a(a_ * 2), b(b_ * 2){}
9
        void calculateTotal() const { total = a + b; };       //LINE-2
10
        void display() const {                     //LINE-3 
11
            cout << "a = " << a << ", b = " << b << ", total = " << total; 
12
        }
13
};
0
int main(){
1
    int x, y;
2
    cin >> x >> y;
3
    const Calculator calc(x, y);
4
    calc.calculateTotal();
5
    calc.display();
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
4 6
a = 8, b = 12, total = 20
a = 8, b = 12, total = 20
Passed
Test Case 2
7 -3
a = 14, b = -6, total = 8
a = 14, b = -6, total = 8
Passed



W3_Programming-Qs.3

Due on 2024-08-15, 23:59 IST
Your last recorded submission was on 2024-08-08, 15:32 IST
Select the Language for this assignment. 
1
#include<iostream>
2
#include<cstring>
3
#include<cstdlib>
4
using namespace std;
5
6
class Student {
7
    int sid;
8
    char *name;
9
    public:
10
        Student(int sid_, const char *name_) : sid(sid_), name(strdup(name_)) {}
11
        Student(const Student& s) : sid(s.sid), name(strdup(s.name)) { }   //LINE-1
12
        Student& operator=(const Student& s) {    //LINE-2 
13
            if (this != &s) {              //LINE-3 
14
                free(name);
15
                sid = s.sid;
16
                name = strdup(s.name);              
17
            }
18
            return *this;
19
        }
20
        void display(){
21
             cout << sid << " : " << name << endl;
22
        }
23
};
0
int main(){
1
    int a, b;
2
    char n1[80], n2[80];
3
    cin >> a >> n1 >> b >> n2;
4
    Student s1(a, n1);
5
    Student s2 = s1;
6
    Student s3(b, n2);
7
    s1 = s3;
8
    s1.display();
9
    s2.display();
10
    s3.display();
11
    return 0;
12
}
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
5 John
10 Alice
10 : Alice\n
5 : John\n
10 : Alice
10 : Alice\n
5 : John\n
10 : Alice\n
Passed after ignoring Presentation Error
Test Case 2
20 Bob
25 Carol
25 : Carol\n
20 : Bob\n
25 : Carol
25 : Carol\n
20 : Bob\n
25 : Carol\n
Passed after ignoring Presentation Error




W4_Programming-Qs.1

Due on 2024-08-22, 23:59 IST
Your last recorded submission was on 2024-08-15, 16:24 IST
Select the Language for this assignment. 
1
#include<iostream>
2
using namespace std;
3
4
class Vector{
5
    int x, y;
6
7
public:
8
    Vector(int a, int b) : x(a), y(b){}
9
10
    void display(){
11
12
        cout << x << " " << y;
13
    }
14
friend Vector operator+(Vector c, int c2);        //LINE-1
15
};
16
17
Vector operator+(Vector c, int c2){                               //LINE-2
18
    
19
    return Vector( c.x+c2, c.y);                      //LINE-3
20
}
0
int main(){
1
    int n;
2
    cin >> n;
3
    Vector v(2, 3);
4
    Vector v1 = v + n;
5
    v1.display();
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
4
6 3
6 3
Passed
Test Case 2
7
9 3
9 3
Passed



W4_Programming-Qs.2

Due on 2024-08-22, 23:59 IST
Select the Language for this assignment. 
1
#include<iostream>
2
using namespace std;
3
4
class Singleton {
5
    int data;
6
    static Singleton *instance;                         //LINE-1
7
    Singleton(int value) : data(value) {}
8
public:
9
    int getData() { return data; }
10
    static Singleton* getInstance(int value) {        //LINE-2
11
        if(!instance)
12
            instance = new Singleton(value);
13
        return instance;
14
    }
15
};
16
17
Singleton* Singleton::instance = 0;                   //LINE-3
0
int main(){
1
    int n, value;
2
    cin >> n;
3
    int arr[n];
4
    for(int i = 0; i < n; i++)
5
        cin >> arr[i];
6
    for(int i = 0; i < n; i++){
7
        Singleton *instance = Singleton::getInstance(arr[i]);
8
        cout << instance->getData() << " ";
9
    }
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
2
3 4
3 3
3 3 
Passed after ignoring Presentation Error
Test Case 2
3
2 4 6
2 2 2
2 2 2 
Passed after ignoring Presentation Error


W4_Programming-Qs.3

Due on 2024-08-22, 23:59 IST
Select the Language for this assignment. 
1
#include<iostream>
2
using namespace std;
3
4
class Vector {
5
    int x;
6
    int y;
7
8
public:
9
    Vector(int _x, int _y) : x(_x), y(_y) {}
10
11
    void display(){
12
13
        cout << "(" << x << "," << y << ")";
14
    }
15
friend istream& operator>>(istream&, Vector&);        //LINE-1
16
    
17
    Vector& operator++(){                                //LINE-2
18
        ++x;
19
        return *this;
20
    }
21
    Vector operator++(int){                             //LINE-3
22
        Vector temp(x, y);
23
        ++y;
24
        return temp;
25
    }
26
};
27
istream& operator>>(istream& is, Vector& v){               //LINE-4
28
    is >> v.x >> v.y;
29
    return is;
30
}
0
int main(){
1
    int i, j;
2
    cin >> i >> j;
3
    Vector v(i, j);
4
    ++(++v);
5
    v++;
6
    v.display();
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 4
(5,5)
(5,5)
Passed
Test Case 2
5 2
(7,3)
(7,3)
Passed


W5_Programming-Qs.1

Due on 2024-08-29, 23:59 IST
Select the Language for this assignment. 
1
#include<iostream>
2
using namespace std;
3
4
class Contact {
5
    private:
6
        int phone_number;
7
        string email;
8
    public:
9
        Contact(int phone_number_, string email_) : phone_number(phone_number_), 
10
                                                      email(email_){}
11
        void displayContact(){
12
            cout << "Phone: " << phone_number << endl;
13
            cout << "Email: " << email << endl;
14
        }
15
};
16
class Employee : private Contact {
17
    private:
18
        int emp_id;
19
        string emp_name;
20
    public:
21
        Employee(int emp_id_, string emp_name_, int phone_number_, string email_) 
22
          : emp_id(emp_id_), emp_name( emp_name_), Contact(phone_number_ , email_){}    // LINE-1
23
        using Contact::displayContact;   // LINE-2
24
        void display(){
25
            cout << "ID: " << emp_id << endl;
26
            cout << "Name: " << emp_name << endl;
27
        }
28
};
0
int main(){
1
    int id, phone;
2
    string name, email;
3
    cin >> id >> name;
4
    cin >> phone >> email;
5
    Employee e(id, name, phone, email);
6
    e.display();
7
    e.displayContact();
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
102 Bob
1234567890 bob@example.com
ID: 102\n
Name: Bob\n
Phone: 1234567890\n
Email: bob@example.com
ID: 102\n
Name: Bob\n
Phone: 1234567890\n
Email: bob@example.com\n
Passed after ignoring Presentation Error



W5_Programming-Qs.2

Due on 2024-08-29, 23:59 IST
Select the Language for this assignment. 
1
#include <iostream>
2
using namespace std;
3
4
class Appliance {
5
protected:
6
    int power;
7
8
public:
9
    Appliance(int p) : power(p) {}
10
11
    friend ostream& operator<<(ostream& os, const Appliance& a);
12
};
13
14
class WashingMachine : public Appliance{  // LINE-1
15
protected:
16
    int drum_size;
17
18
public:
19
    WashingMachine(int p, int ds) :  drum_size(ds), Appliance(p) {}  // LINE-2
20
21
    friend ostream& operator<<(ostream& os, const WashingMachine& wm);
22
};
23
24
class Refrigerator : public Appliance {  // LINE-3
25
protected:
26
    int capacity;
27
28
public:
29
    Refrigerator(int p, int c) :  Appliance(p), capacity(c){}  // LINE-4
30
31
    friend ostream& operator<<(ostream& os, const Refrigerator& r);
32
};
0
ostream& operator<<(ostream& os, const Appliance& a) {
1
    os << "Power: " << a.power << "W" << endl;
2
    return os;
3
}
4
5
ostream& operator<<(ostream& os, const WashingMachine& wm) {
6
    os << "Power: " << wm.power << "W, Drum size: " << wm.drum_size << "L" << endl;
7
    return os;
8
}
9
10
ostream& operator<<(ostream& os, const Refrigerator& r) {
11
    os << "Power: " << r.power << "W, Capacity: " << r.capacity << "L" << endl;
12
    return os;
13
}
14
15
int main() {
16
    int a, b, c, d, e;
17
    cin >> a >> b >> c >> d >> e;
18
    Appliance appliance(a);
19
    WashingMachine wm(b, c);
20
    Refrigerator fridge(d, e);
21
    cout << appliance << wm << fridge;
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
500 600 50 700 200
Power: 500W\n
Power: 600W, Drum size: 50L\n
Power: 700W, Capacity: 200L
Power: 500W\n
Power: 600W, Drum size: 50L\n
Power: 700W, Capacity: 200L\n
Passed after ignoring Presentation Error
Test Case 2
300 400 35 450 150
Power: 300W\n
Power: 400W, Drum size: 35L\n
Power: 450W, Capacity: 150L
Power: 300W\n
Power: 400W, Drum size: 35L\n
Power: 450W, Capacity: 150L\n
Passed after ignoring Presentation Error


W5_Programming-Qs.3

Due on 2024-08-29, 23:59 IST
Select the Language for this assignment. 
1
#include<iostream>
2
using namespace std;
3
4
class X {
5
    int x;
6
    public:
7
        X(int _x = 0);
8
        int getSum(); 
9
};
10
11
class Y : public X {
12
    int y;
13
    public:
14
        Y(int _x = 0, int _y = 0);
15
        int getSum(); 
16
};
17
18
class Z : public Y {
19
    int z;
20
    public:
21
        Z(int _x = 0, int _y = 0, int _z = 0);
22
        int getSum(); 
23
};
24
X::X(int _x) : x(_x) {}    //LINE-1
25
26
Y::Y(int _x, int _y) :  X(_x), y(_y) {}    //LINE-2
27
28
Z::Z(int _x, int _y, int _z) : Y(_x,_y), z(_z) {}    //LINE-3
29
30
int X::getSum(){  return x; }    //LINE-4 
31
32
int Y::getSum(){ return X::getSum() + y; }    //LINE-5
33
34
int Z::getSum(){ return Y::getSum() + z; }    //LINE-6
0
int main(){
1
    int a, b, c;
2
    cin >> a >> b >> c;
3
    X xObj(a);
4
    Y yObj(a, b);
5
    Z zObj(a, b, c);
6
    cout << xObj.getSum() << ", " << yObj.getSum() << ", " << zObj.getSum();
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
10 20 30
10, 30, 60
10, 30, 60
Passed
Test Case 2
10 -10 10
10, 0, 10
10, 0, 10
Passed





Private Test cases used for EvaluationStatus
Test Case 1
Passed



W6_Programming-Qs.1

Due on 2024-09-05, 23:59 IST
Select the Language for this assignment. 
1
#include <iostream>
2
#define PI 3.14
3
using namespace std;
4
5
class Circle{
6
    protected:
7
        double radius;
8
    public:
9
        Circle(double r) : radius(r) {}
10
11
        virtual double Area();            //LINE-1
12
13
        void Print();            //LINE-2
14
};
0
double Circle::Area() { return PI*radius*radius; }
1
void Circle::Print() { cout << Area() << " "; }
2
3
class Cylinder: public Circle{
4
    double height;
5
    public:
6
        Cylinder(double r, double h) : Circle(r), height(h) {}
7
        double Area() { return 2*PI*radius*radius*height; }
8
};
9
10
int main(){
11
    double r, h;
12
    cin >> r >> h;
13
    Circle c1(r);
14
    Cylinder c2(r,h);
15
    Circle *c[2] = {&c1, &c2};
16
    for(int i=0;i<2;i++)
17
        c[i]->Print();
18
        
19
    return 0;
20
}
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 4
12.56 100.48
12.56 100.48 
Passed after ignoring Presentation Error
Test Case 2
5 1
78.5 157
78.5 157 
Passed after ignoring Presentation Error



W6_Programming-Qs.2

Due on 2024-09-05, 23:59 IST
Select the Language for this assignment. 
1
#include <iostream>
2
using namespace std;
3
4
class myClass1{
5
    int data1;
6
    public:
7
        myClass1(int d) : data1(d) {}
8
        virtual void print();                     //LINE-1
9
        friend void Mul(myClass1&, myClass1&);    //LINE-2
10
};
11
12
class myClass2 : public myClass1{
13
    int data2;
14
    public:
15
        myClass2(int d1, int d2) : myClass1(d1), data2(d2) {}
16
        void print(){
17
            myClass1::print();                    //LINE-3
18
            cout << data2 << " ";
19
        }
20
};
0
void myClass1::print(){ cout << data1 << " "; }
1
2
void Mul(myClass1 &m1, myClass1 &m2){
3
    m1.data1 = m1.data1 * m2.data1;
4
}
5
6
int main(){
7
    int m, n;
8
    cin >> m >> n;
9
    myClass1 *t1 = new myClass2(m,n);
10
    myClass1 *t2 = new myClass1(m);
11
    Mul(*t1, *t2);
12
    t1->print();
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
2 4
4 4
4 4 
Passed after ignoring Presentation Error
Test Case 2
3 5
9 5
9 5 
Passed after ignoring Presentation Error




W6_Programming-Qs.3

Due on 2024-09-05, 23:59 IST
Select the Language for this assignment. 
1
#include <iostream>
2
using namespace std;
3
4
class Base{
5
    int d;
6
    public:
7
        Base(int _d);
8
        virtual ~Base();        //LINE-1
9
        int get() { return d; }
10
};
11
12
class Derived : public Base{
13
    public:
14
        Derived(int _d);
15
        ~Derived();              //LINE-2
16
};
0
Base::Base(int _d) : d(_d) { cout << d << " "; }
1
Base::~Base(){ cout << 2*d << " "; }
2
Derived::Derived(int _d) : Base(_d) { cout << 3*_d << " "; }
3
Derived::~Derived() { cout << Base::get() << " "; }
4
5
int main(){
6
    int m;
7
    cin >> m;
8
    Derived *t = new Derived(m);
9
    Base *t1 = t;
10
    delete t1;
11
    return 0;
12
}
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
5
5 15 5 10
5 15 5 10 
Passed after ignoring Presentation Error
Test Case 2
2
2 6 2 4
2 6 2 4 
Passed after ignoring Presentation Error




W7_Programming-Qs.1

Due on 2024-09-12, 23:59 IST
Select the Language for this assignment. 
1
#include<iostream>
2
using namespace std;
3
class Class1{
4
    int a = 10;
5
    public:
6
        void show(){
7
            cout << a << " ";
8
        }
9
};
10
class Class2{
11
    int b = 20;
12
    public:
13
        void show(){
14
            cout << b;
15
        }
16
        int operator=(int x){ //LINE-1
17
            b = b + x;
18
            return b;
19
        }
20
};
21
void fun(const Class1 &t, int x){
22
    Class1 &u = const_cast<Class1&>(t); //LINE-2
23
    u.show();
24
    Class2 &v = reinterpret_cast<Class2&>(u); //LINE-3
25
    v = x;
26
    v.show();
27
}
0
int main(){
1
    Class1 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
10 14
10 14
Passed
Test Case 2
9
10 19
10 19
Passed




W7_Programming-Qs.2

Due on 2024-09-12, 23:59 IST
Select the Language for this assignment. 
1
#include<iostream>
2
using namespace std;
3
class TestClass{
4
    int *arr;
5
    int n;
6
    public:
7
        TestClass(int k) : n(k), arr(new int(n)){} //LINE-1
8
        operator int(){ //LINE-2
9
            return arr[--n];
10
        }
11
        TestClass 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
    TestClass s(k);
4
    s = k;
5
    for(int i = 0; i < k; i++)
6
        cout << static_cast<int>(s) << " ";
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 2 3
3 2 1
3 2 1 
Passed after ignoring Presentation Error
Test Case 2
4
5 6 4 7
7 4 6 5
7 4 6 5 
Passed after ignoring Presentation Error



W7_Programming-Qs.3

Due on 2024-09-12, 23:59 IST
Select the Language for this assignment. 
1
#include<iostream>
2
#include<cctype>
3
using namespace std;
4
class Character{
5
    char c;
6
    public:
7
        Character(char _c) : c(tolower(_c)){} //LINE-1
8
9
        operator char(){ return c; } //LINE-2
10
11
         operator int(){ return c - 'a' + 1; } //LINE-3
12
};
0
int main(){
1
    char c;
2
    cin >> c;
3
    Character cb = c;
4
    cout << (char)cb << ": position is " << int(cb);
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
A
a: position is 1
a: position is 1
Passed
Test Case 2
c
c: position is 3
c: position is 3
Passed





W8_Programming-Qs.1

Due on 2024-09-19, 23:59 IST
Select the Language for this assignment. 
1
#include <iostream>
2
 template<class T1, class T2, int N> //LINE-1
3
4
void average(T1 arr[], T2& avg) { //LINE-2
5
6
    T1 total = 0 ; 
7
    for(int i = 0; i < N; i++)
8
        total += arr[i];
9
    avg = (double)total / N;
10
}
11
int main(){
12
13
     const int size = 4; //LINE-3
0
int iA[size];
1
    for(int i = 0; i < size; i++)
2
        std::cin >> iA[i];
3
    double avg = 0.0;
4
    average<int, double, size>(iA, avg);
5
    std::cout << avg;
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 40
25
25
Passed
Test Case 2
-2 4 5 -1
1.5
1.5
Passed


W8_Programming-Qs.2

Due on 2024-09-19, 23:59 IST
Select the Language for this assignment. 
1
#include<iostream>
2
#include<algorithm>
3
#include<vector>
4
struct average {
5
    int cnt_; //count of element
6
    int ss_; //sum of square of elements
7
8
    average(int cnt = 0 , int ss = 0) : cnt_(cnt), ss_(ss) {} //LINE-1
9
10
    void operator() (int x) { ss_ += x*x; ++cnt_; } //LINE-2
11
};
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
    average mi = for_each(v.begin(), v.end(), average());
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-09-19, 23:59 IST
Select the Language for this assignment. 
1
#include<iostream>
2
class testArray{
3
public:
4
    virtual void what(){ std::cout << "index out of array"; }
5
};
6
class InvalidElement{
7
public:
8
    virtual void what(){ std::cout << "invalid"; }
9
};
10
template<typename T, int N> //LINE-1
11
class plist{
12
    T arr_[N];
13
public:
14
    plist(){
15
        for(int i = 0; i < N; i++)
16
            arr_[i] = -1;
17
    }
18
    //LINE-2: impelement insert() function
19
  void insert(int i, T val){
20
    if(i < N)
21
    arr_[i] = val;
22
    else
23
    throw testArray();
24
    }
25
    //LINE-3: impelement peek() function
26
  T peek(int i){
27
    if(arr_[i] < 0)
28
    throw InvalidElement();
29
    return arr_[i];
30
    }
31
};
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(testArray& 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
2 a 1 x 3 z 2 y
invalid, x, y, z,
invalid, x, y, z, 
Passed after ignoring Presentation Error
Test Case 2
0 a 2 b 3 x 4 z
index out of array\n
a, invalid, b, x,
index out of array\n
a, invalid, b, x, 
Passed after ignoring Presentation Error



W9_Programming-Qs.2

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





W10_Programming-Qs.1

Due on 2024-10-03, 23:59 IST
Your last recorded submission was on 2024-09-29, 09:54 IST
Select the Language for this assignment. 
1
#include <iostream>
2
3
class KG;
4
class G{
5
    public:
6
        G(double w) : w_(w) { }
7
        KG getValue();     
8
        void print(){ std::cout << w_ << "G "; }
9
    private:
10
        double w_;
11
};
12
13
class KG{
14
    public:
15
        KG(double w) : w_(w) { }
16
        G getValue();         
17
        void print(){ std::cout << w_ << "KG "; }
18
    private:
19
        double w_;
20
};
21
KG G::getValue() { return KG(w_ / 1000); }    //LINE-1
22
23
G KG::getValue() { return G(w_ * 1000); }     //LINE-2
24
25
template <typename T>                              //LINE-3
26
27
decltype(auto) convert_weight(T w) {              //LINE-4
28
29
    return w.getValue();  
30
}
0
int main(){
1
    double  a, b;
2
    std::cin >> a >> b;
3
    G o1(a);
4
    KG o2(b);
5
    convert_weight(o1).print();
6
    convert_weight(o2).print();
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
10 10
0.01KG 10000G
0.01KG 10000G 
Passed after ignoring Presentation Error
Test Case 2
2000 20
2KG 20000G
2KG 20000G 
Passed after ignoring Presentation Error



W10_Programming-Qs.2

Due on 2024-10-03, 23:59 IST
Your last recorded submission was on 2024-09-29, 10:02 IST
Select the Language for this assignment. 
1
#include <iostream>
2
#include <vector>
3
4
class Integer { 
5
    public:
6
        Integer(){}
7
        Integer(int i) : ip_(new int(i)) { } 
8
        Integer(const Integer& n) : ip_(new int(*(n.ip_) * 5)) { }    // LINE-1: copy constructor 
9
        Integer& operator=(const Integer& n) {      // LINE-2: copy assignment 
10
            if (this != &n) { 
11
                delete ip_;  
12
                ip_ = new int(*(n.ip_) * 10); 
13
            } 
14
            return *this;
15
        }
16
        ~Integer() { delete ip_; } 
17
        Integer(Integer&& n) : ip_(n.ip_) { n.ip_ = nullptr; }  // LINE-3: move constructor 
18
        Integer& operator=(Integer&& d) {        // LINE-4: move assignment 
19
            if (this != &d) { 
20
                ip_ = d.ip_; 
21
                d.ip_ = nullptr; 
22
            } 
23
            return *this;
24
        }
0
void show(){
1
            if(ip_ == nullptr)
2
                std::cout << "moved : ";
3
            else
4
                std::cout << *ip_ << " : ";
5
        }
6
        private:
7
            int* ip_ {nullptr};
8
};
9
10
int main(){
11
    int a;
12
    std::cin >> a;
13
    Integer n1(a);
14
    Integer n2 = n1;
15
    Integer n3;
16
    n3 = n1;
17
    n1.show();
18
    n2.show();
19
    n3.show();
20
    
21
    Integer n4 = std::move(n1);
22
    Integer n5;
23
    n5 = std::move(n1);
24
    n1.show();
25
    n4.show();
26
    n5.show();
27
    return 0;
28
}
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
5
5 : 25 : 50 : moved : 5 : moved :
5 : 25 : 50 : moved : 5 : moved : 
Passed after ignoring Presentation Error
Test Case 2
-10
-10 : -50 : -100 : moved : -10 : moved :
-10 : -50 : -100 : moved : -10 : moved : 
Passed after ignoring Presentation Error



W10_Programming-Qs.3

Due on 2024-10-03, 23:59 IST
Your last recorded submission was on 2024-09-29, 09:52 IST
Select the Language for this assignment. 
1
#include <iostream>
2
#include <vector>
3
4
template<typename T, typename U>            //LINE-1
5
6
void inner_product( std::vector<T>& v1, std::vector<U>& v2) {    //LINE-2
7
8
    typedef decltype(v1[0] * v2[0]) Tmp;    // LINE-3: define new type Tmp
9
    /* Don't edit the following part */
10
    Tmp sum = 0;
11
    
12
    for (int i=0; i < v1.size(); ++i) {
13
        sum += v1[i] * v2[i];
14
    }
15
    std::cout << sum << " ";
16
}
0
int main(){
1
    float a;
2
    int b;
3
    double c;
4
    std::vector<float> Vec1;            
5
    std::vector<int> Vec2;              
6
    for(int i = 0; i < 3; i++) {
7
        std::cin >> a;
8
        Vec1.push_back(a);
9
    }
10
    for(int i = 0; i < 3; i++) {
11
        std::cin >> b;
12
        Vec2.push_back(b);
13
    }
14
    
15
    inner_product(Vec1, Vec2);
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.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
1.5 2.5 3.5
1 2 3
17
17 
Passed after ignoring Presentation Error
Test Case 2
5.5 3.0 4.5
11 12 13
155
155 
Passed after ignoring Presentation Error



W11_Programming-Qs.1

Due on 2024-10-10, 23:59 IST
Select the Language for this assignment. 
1
#include <iostream>
2
3
template <typename T>    //LINE-1
4
5
double findMin( T num){ return num; }    //LINE-2
6
7
template <typename T, typename... Tail>    //LINE-3
8
9
double findMin(T num, Tail... nums){     //LINE-4
10
11
    return num <= findMin(nums...) ? num : findMin(nums...);   
12
}
0
int main(){
1
    int a, b, c;
2
    double d, e, f;
3
    std::cin >> a >> b >> c;
4
    std::cin >> d >> e >> f;
5
    std::cout << findMin(a, b, c) << " ";
6
    std::cout << findMin(d, e, f) << " ";
7
    std::cout << findMin(a, b, c, d, e, f);
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
12.5 15.5 20.5
10 12.5 10
10 12.5 10
Passed
Test Case 2
1 5 3
2.3 6.7 2.1
1 2.1 1
1 2.1 1
Passed



W11_Programming-Qs.2

Due on 2024-10-10, 23:59 IST
Select the Language for this assignment. 
1
#include <iostream>
2
#include <vector>
3
4
template<typename T, typename U>
5
struct Sum_op{
6
    double operator()(std::ostream& os, std::vector<T>&& v1, std::vector<U>&& v2){
7
        os << "rvalue version: ";
8
        double sum {0.0};
9
        for(T i : v1) sum += i;
10
        for(U i : v2) sum += i;
11
        return sum;
12
    }
13
    //template<typename T, typename U>
14
    double operator()(std::ostream& os, const std::vector<T>& v1, 
15
                        const std::vector<U>& v2){
16
        os << "lvalue version: ";
17
        double sum {0.0};
18
        for(T i : v1) sum += i;
19
        for(U i : v2) sum += i;
20
        return sum;
21
    }
22
};
23
template<typename F, typename... T>    //LINE-1 
24
25
auto wrapper(std::ostream& os, F&& func, T&&... args) -> decltype(func(os,
26
args...)) {    //LINE-2
27
28
    return func(os, std::forward<T>(args)...);    //LINE-3
29
}
0
int main() {
1
    std::vector<int> iv;
2
    std::vector<double> dv;
3
    for(int i = 0; i < 3; i++){
4
        int a;
5
        std::cin >> a;
6
        iv.push_back(a);
7
    }
8
    for(int i = 0; i < 3; i++){
9
        double b;
10
        std::cin >> b;
11
        dv.push_back(b);
12
    }
13
14
    std::cout << wrapper(std::cout, Sum_op<int, double>(), iv, dv);
15
    std::cout << std::endl;
16
    std::cout << wrapper(std::cout, Sum_op<int, double>(), std::move(iv), 
17
                    std::move(dv));
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
10 20 30
1.5 2.5 2.5
lvalue version: 66.5\n
rvalue version: 66.5
lvalue version: 66.5\n
rvalue version: 66.5
Passed
Test Case 2
1 -3 -4
2.5 4.5 -3
lvalue version: -2\n
rvalue version: -2
lvalue version: -2\n
rvalue version: -2
Passed



W11_Programming-Qs.3

Due on 2024-10-10, 23:59 IST
Your last recorded submission was on 2024-10-10, 12:12 IST
Select the Language for this assignment. 
1
#include <iostream>
2
#include <functional>
3
#include <vector>
4
5
int main() {
6
    std::vector<int> vec1;
7
    std::vector<long long> vec2;
8
    
9
    for(int i = 0; i < 3; i++){
10
        int a;
11
        std::cin >> a;
12
        vec1.push_back(a);
13
    }
14
 const std::function<long long(int)> factorial = [&factorial](int n) {    //LINE-1
15
16
        return n > 1 ? n * factorial(n - 1) : 1;
17
18
    };
19
    
20
    for(auto i : vec1)
21
        vec2.push_back(factorial(i));
22
        
23
    [](std::vector<long long> x) {    //LINE-2 
24
25
        for (auto i : x){ std::cout << i << " "; } }(vec2);
26
     
27
    return 0;
28
}
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 3 4
2 6 24
2 6 24 
Passed after ignoring Presentation Error
Test Case 2
1 3 5
1 6 120
1 6 120 
Passed after ignoring Presentation Error





W12_Programming-Qs.1

Due on 2024-10-17, 23:59 IST
Your last recorded submission was on 2024-10-16, 08:54 IST
Select the Language for this assignment. 
1
#include<iostream>
2
3
class myData{
4
        int i_;
5
    public:
6
        myData() = default;
7
        explicit myData(int i) : i_(i) {}
8
        explicit myData(const myData& d) : i_(d.i_) {}
9
        void update(int i) { i_ = i; };
10
        friend std::ostream& operator<<(std::ostream& os, const myData& d){
11
            os << d.i_ << " ";
12
            return os;
13
        }
14
};
15
template <typename T> 
16
class SmartPtr {
17
    public:
18
        explicit SmartPtr(T* pointee) : pointee_(pointee) { }
19
        SmartPtr(SmartPtr& other) : pointee_(other.pointee_) {    //LINE-1: copy constructor 
20
21
            other.pointee_ = nullptr;
22
        }
23
        ~SmartPtr() { if(pointee_ != nullptr) delete pointee_; }
24
        T& operator*() const { return *pointee_; } // LINE-2: Dereferencing
25
26
        T* operator->() const { return pointee_; }  // LINE-3: Indirection 
27
28
    private:
29
        T* pointee_; 
30
};
0
int main(){
1
    int a, b;
2
    std::cin >> a >> b;
3
    SmartPtr<myData> sp(new myData(a));
4
    std::cout << *sp;
5
    sp->update(b);
6
    std::cout << *sp;
7
    SmartPtr<myData> sp2 = sp;
8
    std::cout << *sp2;
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
5 10
5 10 10
5 10 10 
Passed after ignoring Presentation Error
Test Case 2
10 20
10 20 20
10 20 20 
Passed after ignoring Presentation Error





W12_Programming-Qs.2

Due on 2024-10-17, 23:59 IST
Your last recorded submission was on 2024-10-16, 08:55 IST
Select the Language for this assignment. 
1
#include<iostream>
2
#include<memory>
3
4
template<typename T>
5
class mylist;
6
7
template<typename U>
8
class mynode{
9
    public:
10
        mynode(U _info) : info(_info), next(nullptr) {}
11
        friend mylist<U>;
12
    private:
13
        U info;
14
        std::shared_ptr<mynode<U>> next;
15
        std::weak_ptr<mynode<U>> prev;
16
};
17
18
template<typename T>
19
class mylist{
20
    public:
21
        mylist() = default;
22
        void add(const T& item){
23
            std::shared_ptr<mynode<T>> n = std::make_shared<mynode<T>>(item);
24
            if(first == nullptr){
25
                first = n;
26
                last = first;
27
            }
28
            else{
29
                n->next = first;
30
                first->prev = n;
31
                first = n;
32
            }
33
        }
34
void biTraverse(){
35
            for(std::shared_ptr<mynode<T>> t = first; t != nullptr; t = t->next)   //LINE-1
36
                std::cout << t->info << " ";
37
            std::cout << std::endl;
38
            for(std::weak_ptr<mynode<T>> t = last; auto p = t.lock(); t = p->prev)   //LINE-2
39
                std::cout << p->info << " ";
40
        }
41
    private:
42
        std::shared_ptr<mynode<T>> first = nullptr;
43
        std::shared_ptr<mynode<T>> last = nullptr;
44
};
0
int main(){
1
    mylist<int> il;
2
    int n, a;
3
    std::cin >> n;
4
    for(int i = 0; i < n; i++){
5
        std::cin >> a;
6
        il.add(a);
7
    }
8
    il.biTraverse();
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
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.