Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
111 views23 pages

C++ Coding Lab Questions and Solutions

The document contains 10 coding questions related to C++ classes and objects. Each question provides sample code for a class like Calculator, BankAccount, Student, etc. and demonstrates basic class functionality like constructors, member functions, accessing private members etc. Main() is used to create objects and call member functions to output results. The questions cover concepts like arrays, vectors, inheritance and get/set methods.

Uploaded by

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

C++ Coding Lab Questions and Solutions

The document contains 10 coding questions related to C++ classes and objects. Each question provides sample code for a class like Calculator, BankAccount, Student, etc. and demonstrates basic class functionality like constructors, member functions, accessing private members etc. Main() is used to create objects and call member functions to output results. The questions cover concepts like arrays, vectors, inheritance and get/set methods.

Uploaded by

Aghaash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

WEEK 3 – LAB QUESTIONS

1.
Coding:
#include <iostream>
using namespace std;
void acceptElements(int arr[], int size) {
cout << "Enter " << size << " elements:\n";
for (int i = 0; i < size; ++i) {
cin >> arr[i]; }}
void displayElements(int arr[], int size) {
cout << "Elements of the array are:\n";
for (int i = 0; i < size; ++i) {
cout << arr[i] << " ";}
cout << endl;}
void insertionSort(int arr[], int size) {
for (int i = 1; i < size; ++i) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;}
arr[j + 1] = key;
}
cout << "Array sorted using insertion sort.\n";
}
void selectionSort(int arr[], int size) {
for (int i = 0; i < size - 1; ++i) {
int min_index = i;
for (int j = i + 1; j < size; ++j) {
if (arr[j] < arr[min_index]) {
min_index = j;}}
swap(arr[i], arr[min_index]);
}
cout << "Array sorted using selection sort.\n";
}
void bubbleSort(int arr[], int size) {
for (int i = 0; i < size - 1; ++i) {
for (int j = 0; j < size - i - 1; ++j) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
}
}
}
cout << "Array sorted using bubble sort.\n";}
int main() {
const int SIZE = 10;
int arr[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 << "f. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 'a':
acceptElements(arr, SIZE);
break;
case 'b':
displayElements(arr, SIZE);
break;
case 'c':
insertionSort(arr, SIZE);
break;
case 'd':
selectionSort(arr, SIZE);
break;
case 'e':
bubbleSort(arr, SIZE);
break;
case 'f':
cout << "Exiting...\n";
break;
default:
cout << "Invalid choice. Please try again.\n";
}
} while (choice != 'f');
return 0;
}

Output:
2.
Coding:
#include <iostream>
using namespace std;
class Calculator {
private:
int num1;
int num2;
public:
Calculator(int a, int b) {
num1 = a;
num2 = b;
}
int addNumbers() {
return num1 + num2;
}
};
int main() {
Calculator calc(10, 20);
cout << "Sum: " << [Link]() << endl;
return 0;
}
Output:

3.
Coding:

#include <iostream>
using namespace std;
class BankAccount {
private:
string accountNumber;
string accountHolderName;
double balance;
public:
BankAccount(string accNum, string accHolder, double initialBalance)
{
accountNumber = accNum;
accountHolderName = accHolder;
balance = initialBalance;
}
void deposit(double amount) {
balance += amount;
cout << "Deposit of $" << amount << " successful.\n";
}
void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
cout << "Withdrawal of $" << amount << " successful.\n";
} else {
cout << "Insufficient funds. Withdrawal failed.\n";
}
}
double checkBalance() {
return balance;
}
};
int main() {
BankAccount account("123456789", "John Doe", 1000);
[Link](500);
[Link](200);
[Link](1500);
cout << "Current balance: $" << [Link]() << endl;
return 0;
}
Output:

4.
Coding:
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string name;
string ID;
int grades[100];
int numGrades;
public:
Student(string studentName, string studentID) {
name = studentName;
ID = studentID;
numGrades = 0;
}
void addGrade(int grade) {
if (numGrades < 100) {
grades[numGrades++] = grade;
cout << "Grade added successfully.\n";
} else {
cout << "Maximum number of grades reached. Cannot add more
grades.\n";
}
}
double calculateAverageGrade() {
if (numGrades == 0) {
return 0.0;
}
int sum = 0;
for (int i = 0; i < numGrades; ++i) {
sum += grades[i];
}
return static_cast<double>(sum) / numGrades;
}
void displayStudentInfo() {
cout << "Name: " << name << endl;
cout << "ID: " << ID << endl;
cout << "Number of grades: " << numGrades << endl;
cout << "Grades: ";
for (int i = 0; i < numGrades; ++i) {
cout << grades[i] << " ";
}
cout << endl;
}
};
int main() {
Student student("John Doe", "123456");
[Link](85);
[Link](90);
[Link](78);
[Link]();
cout << "Average grade: " << [Link]() <<
endl;
return 0;
}
Output:

5.
Coding:
#include <iostream>
#include <string>
using namespace std;
class Employee {
private:
string name;
string ID;
double hourlyWage;
double hoursWorked;
public:
Employee(string empName, string empID, double wage, double
hours) {
name = empName;
ID = empID;
hourlyWage = wage;
hoursWorked = hours;
}
double calculateWeeklySalary() {
return hourlyWage * hoursWorked;
}
void displayEmployeeInfo() {
cout << "Name: " << name << endl;
cout << "ID: " << ID << endl;
cout << "Hourly wage: $" << hourlyWage << endl;
cout << "Hours worked: " << hoursWorked << endl;
cout << "Weekly salary: $" << calculateWeeklySalary() << endl;
}
};
int main() {
Employee emp("John Doe", "EMP123", 20.0, 40.0);
[Link]();
return 0;
}
Output:

6.
Coding:
#include <iostream>
#include <string>
using namespace std;
class Product {
private:
string name;
string category;
double price;
int quantity;
public:
Product(string n, string cat, double p, int q) {
name = n;
category = cat;
price = p;
quantity = q;
}
void displayProductDetails() {
cout << "Product Name: " << name << endl;
cout << "Category: " << category << endl;
cout << "Price: $" << price << endl;
cout << "Quantity Available: " << quantity << endl;
}
};
int main() {
Product product1("Laptop", "Electronics", 999.99, 10);
[Link]();
return 0;
}

Output:

7.
Coding:
#include <iostream>
#include <vector>
void mergeArrays(std::vector<int>& A, std::vector<int>& B, std::vector<int>&
C) {
int i = 0, j = 0, k = 0;
while (i < [Link]() && j < [Link]()) {
if (A[i] <= B[j]) {
C[k++] = A[i++];
} else {
C[k++] = B[j++];
}
}
// Copy remaining elements of A, if any
while (i < [Link]()) {
C[k++] = A[i++];
}
// Copy remaining elements of B, if any
while (j < [Link]()) {
C[k++] = B[j++];
}
}
int main() {
std::vector<int> A = {1, 3, 5, 7, 9};
std::vector<int> B = {10, 8, 6, 4, 2};
std::vector<int> C([Link]() + [Link]());
mergeArrays(A, B, C);
for (int i = 0; i < [Link](); i++) {
std::cout << C[i] << " ";
}
return 0;
}

Output:

8.
Coding:
#include <iostream>
#include <vector>
int main() {
int n, a, b, c;
std::cin >> n >> a >> b >> c;
std::vector<std::vector<int>> dp(n+1, std::vector<int>(10001, 0));
dp[0][0] = 1;
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= 10000; j++) {
dp[i][j] = dp[i-1][j] + dp[i-2][j-1] + dp[i-3][j-2];
}
}
int ways = 0;
for (int i = a; i <= n; i++) {
for (int j = 0; j <= 10000; j++) {
if (i-a >= 0 && j-b >= 0 && j-b-c >= 0) {
ways += dp[i-a][j-b-c];
}
}
}
cout << ways << endl;
return 0;
}

Coding:
9.

Coding:

#include <iostream>
#include <string>
using namespace std;
class Library {
private:
int rollNumber;
string studentName;
int bookCode;
public:
Library(int roll, string name, int code) {
rollNumber = roll;
studentName = name;
bookCode = code;
}
void printDetails() {
cout << "Student Details:" << endl;
cout << "Roll Number: " << rollNumber << endl;
cout << "Name: " << studentName << endl;
cout << "Book Code: " << bookCode << endl;
}
};
int main() {
int rollNumber, bookCode;
string studentName;
// Input
cin >> rollNumber >> studentName >> bookCode;
// Create Library object
Library student(rollNumber, studentName, bookCode);
// Output
[Link]();
return 0;
}

Output:
10.

Coding:

#include <iostream>
#include <vector>
#include <string>
using namespace std;
class House {
private:
string houseName;
int houseNumber;
string city;
string state;
int numRooms;
vector<int> lengths;
vector<int> breadths;
vector<int> heights;
public:
House(string name, int number, string cityName, string stateName,
int rooms) {
houseName = name;
houseNumber = number;
city = cityName;
state = stateName;
numRooms = rooms;
[Link](numRooms);
[Link](numRooms);
[Link](numRooms);
}
void setRoomDimensions(int roomIndex, int length, int breadth, int
height) {
lengths[roomIndex] = length;
breadths[roomIndex] = breadth;
heights[roomIndex] = height;
}
void printDetails() {
cout << "House Name: " << houseName << endl;
cout << "House Number: " << houseNumber << endl;
cout << "City: " << city << endl;
cout << "State: " << state << endl;
cout << "Number of Rooms: " << numRooms << endl;
for (int i = 0; i < numRooms; ++i) {
cout << "Room " << i+1 << " Dimensions - Length: " << lengths[i] << ",
Breadth: " << breadths[i] << ", Height: " << heights[i] << endl;
}
}
};
int main() {
string houseName, city, state;
int houseNumber, numRooms;
// Input
cout << "Enter house name: ";
getline(cin, houseName);
cout << "Enter house number, city, and state (separated by spaces): ";
cin >> houseNumber >> city >> state;
cout << "Enter number of rooms: ";
cin >> numRooms;
// Create House object
House house(houseName, houseNumber, city, state, numRooms);
// Input room dimensions
for (int i = 0; i < numRooms; ++i) {
int length, breadth, height;
cout << "Enter dimensions for room " << i+1 << " (length breadth
height): ";
cin >> length >> breadth >> height;
[Link](i, length, breadth, height);
}
// Output
cout << endl << "House Details:" << endl;
[Link]();
return 0;
}
Output:

You might also like