Week 3
Week 3
Week 3
arr[j + 1] = key;
}
}
int main() {
const int MAX_SIZE = 100;
int arr[MAX_SIZE];
int size;
char choice;
do {
cout << "\nMenu:\n";
cout << "a. Accept elements of an array\n";
cout << "b. Display elements of an array\n";
cout << "c. Sort the array using insertion sort method\n";
cout << "d. Sort the array using selection sort method\n";
cout << "e. Sort the array using bubble sort method\n";
cout << "x. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 'a':
cout << "Enter the size of the array: ";
cin >> size;
acceptArray(arr, size);
break;
case 'b':
displayArray(arr, size);
break;
case 'c':
insertionSort(arr, size);
cout << "Array sorted using insertion sort." << endl;
break;
case 'd':
selectionSort(arr, size);
cout << "Array sorted using selection sort." << endl;
break;
case 'e':
bubbleSort(arr, size);
cout << "Array sorted using bubble sort." << endl;
break;
case 'x':
cout << "Exiting program. Goodbye!" << endl;
break;
default:
cout << "Invalid choice. Please enter a valid option." << endl;
}
return 0;
}
2. Write a C++ program to add two numbers using the concept of data abstraction
#include<iostream>
// Class declaration with private data members and public member functions
class Adder {
private:
int num1;
int num2;
public:
// Constructor to initialize data members
Adder() : num1(0), num2(0) {}
void setNum2(int n) {
num2 = n;
}
int main() {
// Creating an object of the Adder class
Adder adderObj;
return 0;
}
class BankAccount {
private:
string accountHolderName;
string accountNumber;
double balance;
public:
// Constructor to initialize the account
BankAccount(string holderName, string accNumber, double initialBalance)
: accountHolderName(holderName), accountNumber(accNumber),
balance(initialBalance) {}
// Method to withdraw money from the account with a check for sufficient funds
void withdraw(double amount) {
if (amount > 0) {
if (amount <= balance) {
balance -= amount;
cout << "Withdrawal of $" << amount << " successful. New balance: $" << balance
<< endl;
} else {
cout << "Insufficient funds. Cannot withdraw $" << amount << ". Current balance:
$" << balance << endl;
}
} else {
cout << "Invalid withdrawal amount. Amount must be greater than zero." << endl;
}
}
int main() {
// Creating a BankAccount object
BankAccount myAccount("John Doe", "123456789", 1000.00);
myAccount.deposit(500.0);
myAccount.withdraw(200.0);
myAccount.withdraw(1500.0); // Attempting to withdraw more than the current
balance
cout << "Final Balance: $" << myAccount.checkBalance() << endl;
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.
#include<iostream>
#include<string>
using namespace std;
class Student {
private:
string name;
string studentID;
double grades[100]; // Assuming a maximum of 100 grades
public:
// Constructor to initialize the student's attributes
Student(string studentName, string id) : name(studentName), studentID(id) {
// Initialize grades array to zeros
for (int i = 0; i < 100; ++i) {
grades[i] = 0.0;
}
}
cout << "Cannot add more grades. Maximum limit reached." << endl;
}
};
int main() {
// Creating a Student object
Student student1("John Doe", "123456");
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.
#include <iostream>
#include <string>
class Employee {
private:
std::string name;
int id;
double hourlyWage;
double hoursWorked;
public:
// Constructor to initialize the employee
Employee(std::string empName, int empId, double empHourlyWage, double
empHoursWorked)
: name(empName), id(empId), hourlyWage(empHourlyWage),
hoursWorked(empHoursWorked) {}
int main() {
// Example usage of the Employee class
Employee emp1("", 0, 0.0, 0.0); // Initializing with default values
emp1.setEmployeeInfo(); // Take input from the user
emp1.displayEmployeeInfo(); // Display the employee's information
return 0;
}
6. Write a C++ program to display product detail using classes.
#include<iostream>
#include<string>
using namespace std;
public:
// Constructor to initialize product details
Product(string name = "", int id = 0, double price = 0.0)
: productName(name), productId(id), productPrice(price) {}
int main() {
// Creating a Product object
Product laptop;
return 0;
}
int main() {
const int MAX_SIZE_A = 5;
const int MAX_SIZE_B = 4;
const int MAX_SIZE_C = MAX_SIZE_A + MAX_SIZE_B;
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.
#include<iostream>
#include<vector>
using namespace std;
// Dynamic programming
for (int i = 0; i <= n; ++i) {
for (int x = 0; x <= a; ++x) {
for (int y = 0; y <= b; ++y) {
for (int z = 0; z <= c; ++z) {
if (i + 0.5 * x + y + 2 * z <= n) {
dp[i + static_cast<int>(0.5 * x + y + 2 * z)][x][y] = (dp[i + static_cast<int>(0.5 *
x + y + 2 * z)][x][y] + dp[i][x][y]) % MOD;
}
}
}
}
}
return result;
}
int main() {
int a, b, c, n;
cin >> a >> b >> c >> n;
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:
Print the Details of Student and Book in the expected format.
#include <iostream>
#include <string>
class LibrarySystem {
private:
int rollNumber;
std::string studentName;
int bookCode;
public:
// Parameterized constructor to initialize student details and book code
LibrarySystem(int roll, std::string name, int code)
: rollNumber(roll), studentName(name), bookCode(code) {}
int main() {
int roll, bcode;
std::string name;
// Input values for Roll Number, Name, and Book Code
std::cout << "Enter Roll Number, Name, and Book Code (separated by space): ";
std::cin >> roll >> name >> bcode;
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.
#include <iostream>
#include <string>
using namespace std;
class Room {
private:
int length;
int breadth;
int height;
public:
// Parameterized constructor
Room(int len, int br, int ht)
: length(len), breadth(br), height(ht) {}
class House {
private:
string houseName;
int houseNumber;
string city;
string state;
int numRooms;
int totalArea;
public:
// Parameterized constructor
House(const string& name, int hno, const string& ct, const string& st)
: houseName(name), houseNumber(hno), city(ct), state(st), numRooms(0),
totalArea(0) {}
int main() {
// Input values
string houseName, city, state;
int houseNumber, numRooms;
return 0;
}