0% found this document useful (0 votes)
4 views

OODP Week 3 Programs

The document contains multiple C++ programs demonstrating various programming concepts including array manipulation, sorting algorithms, class definitions, and basic input/output operations. Each program is structured with a main function and includes functionalities such as sorting arrays, managing bank accounts, calculating average grades, and handling employee information. The document serves as a practice resource for programming exercises in a structured format.

Uploaded by

poke.cool1000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

OODP Week 3 Programs

The document contains multiple C++ programs demonstrating various programming concepts including array manipulation, sorting algorithms, class definitions, and basic input/output operations. Each program is structured with a main function and includes functionalities such as sorting arrays, managing bank accounts, calculating average grades, and handling employee information. The document serves as a practice resource for programming exercises in a structured format.

Uploaded by

poke.cool1000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

21CSC101T Practice Program

Week – 3
-Pranshu Bharti
RA2311033010104
CINTEL AK – 1
PROGRAM 1:
#include <iostream>

#include <vector>

#include <sstream>

using namespace std;

int main() {

vector<int> arr;

int option, i = 0, n, x;

string buffer;

istringstream ssin;

bool bub_swap;

while (true) {

cout << "(1) Accept elements of an array" << endl;

cout << "(2) Display elements of an array" << endl;

cout << "(3) Sort the array using insertion sort method" << endl;

cout << "(4) Sort the array using selection sort method" << endl;

cout << "(5) Sort the array using bubble sort method\nSelect: ";

cin >> option;

cout << endl;

switch (option) {

case 1:

cout << "Enter elements: ";

while (cin >> x) {


arr.push_back(x);

if (cin.peek() == '\n') break;

cout << "Accepted elements successfully\n\n";

break;

case 2:

cout << "The current array is: ";

for (int j = 0; j < arr.size(); j++) {

cout << arr[j] << " ";

cout << endl << endl;

break;

case 3:

for (int i = 1; i < arr.size(); i++) {

int key = arr[i];

int j = i - 1;

while (key < arr[j] && j >= 0) {

arr[j + 1] = arr[j];

j--;

arr[j + 1] = key;

cout << "Sorted using insertion sort\n\n";

break;

case 4:

for (int i = 0; i < arr.size() - 1; i++) {

int min_idx = i;

for (int j = i + 1; j < arr.size(); j++) {

if (arr[j] < arr[min_idx]) {

min_idx = j;

}
}

int temp = arr[min_idx];

arr[min_idx] = arr[i];

arr[i] = temp;

cout << "Sorted using selection sort\n\n";

break;

case 5:

for (int i = 1; i < arr.size(); i++) {

for (int j = 0; j < arr.size() - i; j++) {

bub_swap = false;

if (arr[j] > arr[j + 1]) {

bub_swap = true;

int temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

cout << "Sorted using bubble sort\n\n";

break;

default: return 0;

return 0;

OUTPUT:
PROGRAM 2:
#include <iostream>
using namespace std;
int add(int a, int b) {
return a + b;
}
int main() {
int a, b;
cin >> a >> b;
cout << add(a,b) << endl;
return 0;
}

OUTPUT:
PROGRAM 3:
#include <iostream>

class BankAccount {

private:

std::string account_number;

double balance;

public:

BankAccount(std::string acc_number, double initial_balance = 0) : account_number(acc_number),


balance(initial_balance) {}

void deposit(double amount) {

if (amount > 0) {

balance += amount;

std::cout << "Deposited " << amount << ". New balance: " << balance << std::endl;

} else {

std::cout << "Invalid deposit amount. Amount must be greater than 0." << std::endl;

void withdraw(double amount) {

if (amount > 0) {

if (balance >= amount) {

balance -= amount;

std::cout << "Withdrew " << amount << ". New balance:" << balance << std::endl;

} else {

std::cout << "Insufficient funds." << std::endl;

} else {

std::cout << "Invalid withdrawal amount. Amount must be greater than 0." << std::endl;
}

void check_balance() {

std::cout << "Account balance: " << balance << std::endl;

};

int main() {

BankAccount account("12345", 1000);

account.deposit(1500);

account.withdraw(300);

account.check_balance();

return 0;

OUTPUT:
PROGRAM 4:
#include <iostream>

#include <string>

#include <vector>

class Student {

private:

std::string name;

std::string id;

std::vector<int> grades;

public:

Student(std::string name, std::string id) : name(name), id(id) {}

void addGrade(int grade) {

grades.push_back(grade);

double calculateAverageGrade() {

if (grades.empty()) return 0.0;

int sum = 0;

for (int grade : grades) {

sum += grade;

return static_cast<double>(sum) / grades.size();

void displayInfo() {

std::cout << "Name: " << name << std::endl;

std::cout << "ID: " << id << std::endl;


std::cout << "Grades: ";

for (int grade : grades) {

std::cout << grade << " ";

std::cout << std::endl;

std::cout << "Average Grade: " << calculateAverageGrade() << std::endl;

};

int main() {

Student student("Pranshu Bharti", "RA2311033010104");

student.addGrade(95);

student.addGrade(92);

student.displayInfo();

return 0;

OUTPUT:
PROGRAM 5:
#include <iostream>

#include <string>

class Employee {

private:

std::string name;

std::string id;

double hourlyWage;

double hoursWorked;

public:

Employee(std::string name, std::string id, double hourlyWage, double hoursWorked)

: name(name), id(id), hourlyWage(hourlyWage), hoursWorked(hoursWorked) {}

double calculateWeeklySalary() {

return hourlyWage * hoursWorked;

void displayInfo() {

std::cout << "Name: " << name << std::endl;

std::cout << "ID: " << id << std::endl;

std::cout << "Hourly Wage: " << hourlyWage << std::endl;

std::cout << "Hours Worked: " << hoursWorked << std::endl;

std::cout << "Weekly Salary: " << calculateWeeklySalary() << std::endl;

};

int main() {

Employee employee("Pranshu Bharti", "EMP104", 35.0, 60.0);

employee.displayInfo();

return 0;

}
OUTPUT:

PROGRAM 6:
#include <iostream>

#include <string>

class Product {

private:

std::string name;

std::string brand;

double price;

int capacity;

public:

Product(std::string name, std::string brand, double price, int capacity)

: name(name), brand(brand), price(price), capacity(capacity) {}

void displayDetails() {

std::cout << "Product Name: " << name << std::endl;

std::cout << "Brand: " << brand << std::endl;

std::cout << "Price: " << price << std::endl;

std::cout << "Capacity: " << capacity << " ml" << std::endl;
}

};

int main() {

Product waterBottle("Sunflower Oil", "Fortune", 49.99, 1000);

waterBottle.displayDetails();

return 0;

OUTPUT:

PROGRAM 7:
#include <iostream>
#include <vector>
using namespace std;
vector<int> merge_arrays(vector<int> a, vector<int> b) {
int m = a.size(), n = b.size(), i = 0, j = 0;
vector<int> c;
bool a_pushed, b_pushed;
for(int k = 0; k < m+n; k++) {
if ((a[i] <= b[n-1-j] && i < m) || j >= n) {
c.push_back(a[i]);
i++;
}
else {
c.push_back(b[n-1-j]);
j++;
}
}
return c;
}
int main() {
vector<int> A = { 3, 5, 8, 18, 33, 56 };
vector<int> B = { 99, 88, 77, 66, 55, 44, 33, 22, 11, 0 };
vector<int> C = merge_arrays(A, B);
for (int i : C) {
cout << i << " ";
}
cout << endl;
return 0;
}

OUTPUT:
PROGRAM 8:
#include <iostream>

using namespace std;

int main() {

int n, a, b, c;

cin >> n >> a >> b >> c;

int ways = 0;

for (int i = 0; i <= a; ++i) {

for (int j = 0; j <= b; ++j) {

int liters = n - (i * 0.5 + j * 1);

if (liters >= 0 && liters % 2 == 0 && liters / 2 <= c) {

ways++;

cout << ways << endl;

return 0;

OUTPUT:
PROGRAM 9:
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
class library {
int roll_no;
string name;
int book_code;
public:
library(int r,string n,int code) {
roll_no = r;
name = n;
book_code = code;
}
void display() {
cout << "Roll No:" << roll_no << endl;
cout << "Name of the Student:" << name << endl;
cout << "Code of Book Accessed:" << book_code << endl;
}
};
int main() {
int t_roll, t_code;
string t_name;
cin >> t_roll >> t_name >> t_code;
library lib1(t_roll, t_name, t_code);
cin >> t_roll >> t_name >> t_code;
library lib2(t_roll, t_name, t_code);
lib1.display();
lib2.display();
return 0;
}

OUTPUT:

PROGRAM 10:
#include <iostream>
#include <vector>
using namespace std;
class room {
int l,b,h;
public:
room() {
l = 0;
b = 0;
h = 0;
}
room(int _l, int _b, int _h) {
l = _l;
b = _b;
h = _h;
}
int area() { return l * b; }
};
class house {
int hno, no_rooms;
vector<room> rooms;
string name, city, state;
public:
house(string _name, string _city, string _state, int _hno, int _n)
{
name = _name;
city = _city;
state = _state;
hno = _hno;
no_rooms = _n;
}
void add_room(int l, int b, int h) {
room temp(l, b, h);
rooms.push_back(temp);
}
int area() {
int a = 0;
for (room r : rooms) {
a += r.area();
}
return a;
}
};
int main() {
string name, city, state;
int hno, n, l, b, h;
cin >> name >> hno >> city >> state >> n;
house A(name, city, state, hno, n);
for (int i = 0; i < n; i++) {
cin >> l >> b >> h;
A.add_room(l, b, h);
}
cout << "House name: " << name << endl;
cout << "House address: " << hno << ", " << city << ", " <<
state << endl;
cout << "House no of rooms: " << n << endl;
cout << "House area: " << A.area() << endl;
return 0;
}
OUTPUT:

You might also like