Week 3 Lab Program
Week 3 Lab Program
S
RA2311003011087
OODP – WEEK(2) ASSINGNMENT
1 ) Write a menu driven C++ program with following option
a. Accept elements of an array
b. Display elements of an array
c. Sort the array using insertion sort method
d. Sort the array using selection sort method
e. Sort the array using bubble sort method
ANSWER :
#include<iostream>
using namespace std;
swap(arr[min_idx], arr[i]);
}
}
int main() {
int choice, size;
cout << "Enter the size of the array: ";
cin >> size;
int arr[size];
while (true) {
cout << "\nMenu:\n";
cout << "1. Accept elements of the array\n";
cout << "2. Display elements of the array\n";
cout << "3. Sort the array using insertion sort method\n";
cout << "4. Sort the array using selection sort method\n";
cout << "5. Sort the array using bubble sort method\n";
cout << "6. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
acceptArray(arr, size);
break;
case 2:
displayArray(arr, size);
break;
case 3:
insertionSort(arr, size);
cout << "Array sorted using insertion sort.\n";
break;
case 4:
selectionSort(arr, size);
cout << "Array sorted using selection sort.\n";
break;
case 5:
bubbleSort(arr, size);
cout << "Array sorted using bubble sort.\n";
break;
case 6:
cout << "Exiting program.\n";
return 0;
default:
cout << "Invalid choice. Please enter a valid option.\n";
}
}
return 0;
}
2 ) Write a C++ program to add two numbers using the concept of data abstraction
ANSWER :
#include<iostream>
using namespace std;
// Class to demonstrate data abstraction
class Addition {
private:
// Private attributes to hold the values to add
int a, b;
public:
// Constructor to initialize the numbers to add
Addition(int num1, int num2) {
a = num1;
b = num2;
}
int main() {
int num1, num2;
// Using the public method 'add' to perform the addition and display the result
cout << "The sum of " << num1 << " and " << num2 << " is " << myAddition.add() << endl;
return 0;
}
ANSWER :
#include <iostream>
using namespace std;
class BankAccount {
private:
double balance; // Private variable to store the account balance
public:
// Constructor to initialize the account with a balance. Default is 0 if no amount is
specified.
BankAccount(double initialBalance = 0) {
balance = initialBalance;
}
return 0;
}
4 ) Create a class to represent a student in a grading system. Include attributes for the
student's name, ID,
and an array to store grades. Implement methods to calculate the average grade, display the
student's
information, and add a new grade.
ANSWER :
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Student {
private:
string name;
int id;
vector<double> grades; // Using vector to store grades dynamically
public:
// Constructor to initialize the student with a name and ID. Grades are initially empty.
Student(string studentName, int studentId) : name(studentName), id(studentId) {}
int main() {
// Example usage:
Student student1("John Doe", 1234);
student1.addGrade(88.5);
student1.addGrade(92.3);
student1.addGrade(76.0);
student1.addGrade(85.4);
student1.displayInformation();
return 0;
}
5 ) Develop a class to represent an employee in a payroll system. Include attributes for the
employee's name, ID, hourly wage, and hours worked. Implement methods to calculate the
weekly salary and display the employee's information.
ANSWER :
#include <iostream>
#include <iomanip> // For std::setprecision
using namespace std;
class Employee {
private:
string name;
int id;
double hourlyWage;
double hoursWorked;
public:
// Constructor to initialize the employee with their details
Employee(string employeeName, int employeeId, double wage, double hours)
: name(employeeName), id(employeeId), hourlyWage(wage), hoursWorked(hours) {}
int main() {
// Example usage:
Employee employee1("Jane Doe", 101, 25.50, 40); // Employee with $25.50 hourly wage
and 40 hours of work
Employee employee2("John Smith", 102, 30.00, 45); // Includes 5 hours of overtime, if
applicable
employee1.displayInformation();
cout << "\n"; // Just adding a newline for better readability
employee2.displayInformation();
return 0;
}
ANSWER :
#include <iostream>
#include <string>
using namespace std;
class Product {
private:
int id;
string name;
double price;
public:
// Default constructor
Product() : id(0), name(""), price(0.0) {}
int main() {
// Creating an object of Product with details
Product product1(101, "Laptop", 1200.99);
// Displaying product details
cout << "Displaying product details:" << endl;
product1.displayDetails();
return 0;
}
ANSWER :
#include <iostream>
#include <algorithm> // For std::reverse
int i = 0, j = 0, k = 0;
int main() {
// Example arrays
int A[] = {1, 3, 5, 7};
int M = sizeof(A) / sizeof(A[0]);
int B[] = {8, 6, 4, 2};
int N = sizeof(B) / sizeof(B[0]);
int C[M + N]; // Array to hold the result
return 0;
}
8 ) To celebrate the Reunion of 96 Batch of the Famous School the Ram and Jannu the
organizers of the
event decided to liters of Fruit Drinks. However, an unexpected difficulty occurred in the
shop: it
turned out that Fruit Drinks is sold in bottles 0.5, 1 and 2 li volume. At that, there are exactly
0.5
bottles in volume, bone-liter bottles and c of two-liter ones. The organizers have enough
money to buy
any amount of Fruit Drinks. What did cause the heated arguments was how many bottles of
every kind
to buy, as this question is pivotal for the of Fruit Drinks among the Friends. Your task is to
count the
number of all the possible ways to buy exactly n liters of Fruit Drinks and persuade the organ
this
number is too large .All the bottles of Fruit Drinks are considered indistinguishable, i.e. two
variants of
buying are different from each other they differ in the number of bottles of at least one kind.
Constraints:
1≤n≤ 10000
0≤ a, b, c < 5000
Input Format:
The first line contains four integers representing, a, b, c respectively. Output Format:
Print the unique number representing the solution to the problem.
If it is impossible to buy exactly n liters of Fruit Drinks, print 0.
ANSWER :
#include <iostream>
#include <vector>
using namespace std;
int main() {
int a, b, c, n;
cin >> n >> a >> b >> c;
// Initialize 4D array dp
vector<vector<vector<vector<int>>>> dp(n + 1,
vector<vector<vector<int>>>(a + 1,
vector<vector<int>>(b + 1,
vector<int>(c + 1, 0))));
dp[0][0][0][0] = 1;
return 0;
}
9 ) Tamilnadu Educational Minister has ordered the Director of Higher education to make the
Libraries in
Government schools advanced. So they are planning to create a software which keeps track
of the
books availability and respond to students request for books. Can you help the government
to do this?
Functional Description:
Input values need to be passed to the Parameterized constructor and to output need to be
printed by accessing i t.
Constraints:
1< roll ≤100
100 ≤ bcode< 999
Input Format:
First and Second Line of Input has 3 values of type integer, String and Integer separated by a
space representing
Roll Number, Name and Book code respectively. Output Format:
ANSWER :
#include <iostream>
#include <string>
class Student {
private:
int roll_number;
string name;
public:
Student(int roll, string student_name) : roll_number(roll), name(student_name) {}
void printDetails() {
cout << "Roll Number: " << roll_number << endl;
cout << "Name: " << name << endl;
}
};
class Book {
private:
int book_code;
public:
Book(int code) : book_code(code) {}
void printDetails() {
cout << "Book Code: " << book_code << endl;
}
};
int main() {
int roll_number, book_code;
string name;
// Reading input
cin >> roll_number >> name >> book_code;
// Checking constraints
if (!(roll_number > 1 && roll_number <= 100 && book_code >= 100 && book_code < 999))
{
cout << "Invalid input! Please ensure roll number is between 1 and 100, and book code
is between 100 and 998." << endl;
} else {
// Creating objects
Student student(roll_number, name);
Book book(book_code);
// Printing details
student.printDetails();
book.printDetails();
}
return 0;
}
10 ) Tamilnadu land registration authority is panning to keep track of the native addresses
and total area of
the flats people have across the state. Since the total population and area need to be
monitored is huge.
Government is looking for the software which does this task. Can you help them with proper
programming logic for implementing the same?
Constraints:
1≤ hno<500
1< no room< 10
1≤ length < 50
1< breadth < 50
1≤ height < 50
Input Format:
The first line of the input contain a single string denoting the house name.
The second line of the input contain three values of type Integer String and String separated
by a space representing house number, city and state respectively. The third line of the input
has a single Integer representing the number of rooms.
The subsequent lines of input must have length, breadth and height of each room
Output Format:
Print the details of the house in the expected format.
ANSWER :
#include <iostream>
#include <string>
class House {
private:
string house_name;
int house_number;
string city;
string state;
int num_rooms;
double total_area;
public:
House(string name, int number, string c, string s, int rooms) : house_name(name),
house_number(number), city(c), state(s), num_rooms(rooms), total_area(0) {}
int main() {
string house_name, city, state;
int house_number, num_rooms;
double length, breadth, height;
// Reading input
getline(cin, house_name);
cin >> house_number >> city >> state;
cin >> num_rooms;
return 0;
}