├── day19q1.txt ├── .vscode └── settings.json ├── day13.cpp ├── day19q2.txt ├── day24.cpp ├── MIPday03.cpp ├── day16.cpp ├── day12.cpp ├── day04.cpp ├── day10.cpp ├── day15.cpp ├── MIPday04.cpp ├── day19.cpp ├── day33.cpp ├── MIPday01.cpp ├── day27.cpp ├── day32.cpp ├── day07.cpp ├── day18.cpp ├── day09.cpp ├── day14.cpp ├── day17.cpp ├── day26.cpp ├── day03.cpp ├── day23.cpp ├── day01.cpp ├── MIPday02.cpp ├── day08.cpp ├── day31.cpp ├── day06.cpp ├── day25.cpp ├── day05.cpp ├── day34.cpp ├── day35.cpp ├── day22.cpp ├── day36.cpp ├── day20.cpp ├── day11.cpp ├── day21.cpp ├── day29.cpp ├── day02.cpp ├── day30.cpp └── day28.cpp /day19q1.txt: -------------------------------------------------------------------------------- 1 | Anurag Kumar 2 | School of Computer Science 3 | Bachelor of Technology -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "iostream": "cpp" 4 | } 5 | } -------------------------------------------------------------------------------- /day13.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // Question 1 - Write C++ program to demonstrate how to create dynamic variable using new keyword. 6 | 7 | void q1() { 8 | int* dynamicInt = new int(); 9 | *dynamicInt = 42; 10 | cout << "Value of dynamicInt: " << *dynamicInt << endl; 11 | delete dynamicInt; 12 | } 13 | 14 | int main() { 15 | q1(); 16 | } -------------------------------------------------------------------------------- /day19q2.txt: -------------------------------------------------------------------------------- 1 | C++ is the most used and most popular programming language developed by Bjarne Stroustrup. 2 | C++ is a high-level and object-oriented programming language. 3 | This language allows developers to write clean and efficient code for large applications and software development, game development, and operating system programming. 4 | It is an expansion of the C programming language to include Object Oriented Programming(OOPs) and is used to develop programs for computers. 5 | This C++ Tutorial will cover all the basic to advanced topics of C++ like C++ basics, C++ functions, C++ classes, OOPs and STL concepts. -------------------------------------------------------------------------------- /day24.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | // Ques 1 . You are given a binary array. Your task is to find the maximum number of consecutive ones in array. 7 | // Input :- arr :[1,1,0,1,1,1] 8 | // Output: 3. 9 | int maxOnes(vector array) { 10 | 11 | int maxLength = 0; 12 | int curLength = 0; 13 | 14 | for(int i = 0; i < array.size(); ++i) { 15 | if(array[i] == 1) { 16 | curLength++; 17 | } 18 | else { 19 | curLength = 0; 20 | } 21 | if(curLength > maxLength) { 22 | maxLength = curLength; 23 | } 24 | } 25 | return maxLength; 26 | } 27 | 28 | int main() 29 | { 30 | vector binaryArray = {1,1,0,1,1,1}; 31 | cout << maxOnes(binaryArray); 32 | return 0; 33 | } -------------------------------------------------------------------------------- /MIPday03.cpp: -------------------------------------------------------------------------------- 1 | // Ques 3. Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct. 2 | 3 | // Example 1: 4 | // Input: nums = [1,2,3,1] 5 | // Output: true 6 | 7 | // Explanation: 8 | // The element 1 occurs at the indices 0 and 3. 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | 15 | using namespace std; 16 | 17 | bool containsDuplicates(vector array) { 18 | unordered_set s; 19 | for(int i : array) { 20 | s.insert(i); 21 | } 22 | return s.size() != array.size(); 23 | } 24 | 25 | int main() { 26 | vector nums = {1, 2, 3, 4}; 27 | if (containsDuplicates(nums)) { 28 | cout << "true" << endl; 29 | } else { 30 | cout << "false" << endl; 31 | } 32 | return 0; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /day16.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node { 5 | public: 6 | int val; 7 | Node* next; 8 | Node(int data) { 9 | val = data; 10 | next = nullptr; 11 | } 12 | }; 13 | 14 | // Ques 1: Write a C++ code that take linked list from user input that is in sorted order and a value integer. Now insert the value inside linkedlist such that new likedlist is also in sorted order. 15 | void question1(Node* &head, int data) { 16 | 17 | Node* newNode = new Node(data); 18 | 19 | if (head->val >= data) { 20 | newNode->next = head; 21 | head = newNode; 22 | return; 23 | } 24 | Node* current = head; 25 | while (current->next != nullptr && current->next->val < data) { 26 | current = current->next; 27 | } 28 | newNode->next = current->next; 29 | current->next = newNode; 30 | } 31 | 32 | int main() 33 | { 34 | 35 | return 0; 36 | } -------------------------------------------------------------------------------- /day12.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Node { 6 | public: 7 | int val; 8 | Node* next; 9 | Node(int data) { 10 | val = data; 11 | next = nullptr; 12 | } 13 | }; 14 | 15 | // Ques 1 : Write a function void printLinkedList(Node* head) that traverses a singly linked list and prints each element's value. 16 | void printLinkedList(Node* head) { 17 | Node* currentNode = head; 18 | while (currentNode != nullptr) 19 | { 20 | cout << currentNode->val << " "; 21 | currentNode = currentNode->next; 22 | } 23 | cout << "\n"; 24 | } 25 | 26 | // Ques 2: Write a function void insertAtEnd(Node*& head, int data) that inserts a new node with the given data at the end of a singly linked list. 27 | void insertAtEnd(Node*& head, int data) { 28 | Node* newNode = new Node(data); 29 | if(head == nullptr) { 30 | head = newNode; 31 | } 32 | else { 33 | Node* current = head; 34 | while(current->next != nullptr) { 35 | current = current->next; 36 | } 37 | current->next = newNode; 38 | } 39 | } 40 | 41 | int main() { 42 | 43 | } -------------------------------------------------------------------------------- /day04.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | // Ques:01: Write a C++ program that takes two integers as input and performs the following arithmetic operations: 7 | // Addition 8 | // Subtraction 9 | // Multiplication 10 | // Division (ensure that you handle division by zero) 11 | // Modulus 12 | void performArithmetic(int num1, int num2) { 13 | int addition = num1 + num2; 14 | int subtraction = num1 - num2; 15 | int multiplication = num1 * num2; 16 | cout << "Addition: " << addition << endl; 17 | cout << "Subtraction: " << subtraction << endl; 18 | cout << "Multiplication: " << multiplication << endl; 19 | if(num2==0) { 20 | cout << "Division by zero is not possible" << endl; 21 | return; 22 | } 23 | float division = (float)num1/(float)num2; 24 | int modulus = num1%num2; 25 | 26 | cout << "Division: " << division << endl; 27 | cout << "Modulus: " << modulus << endl; 28 | } 29 | 30 | int main() { 31 | int num1, num2; 32 | 33 | cout << "Enter first integer: "; 34 | cin >> num1; 35 | 36 | cout << "Enter second integer: "; 37 | cin >> num2; 38 | 39 | performArithmetic(num1, num2); 40 | return 0; 41 | } -------------------------------------------------------------------------------- /day10.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // Ques 1: Write a function int findMax(const int arr[], int size) that finds and returns the maximum value in an integer array. Test your function with various arrays in the main() function. 6 | int findMax(const int arr[], int size) { 7 | int max = INT32_MIN; 8 | for(int i = 0; i < size; ++i) { 9 | if(arr[i] > max) { 10 | max = arr[i]; 11 | } 12 | } 13 | return max; 14 | } 15 | 16 | // Ques 2: Write a function bool isPalindrome(const std::string &str) that checks whether a given string is a palindrome (a word, phrase, or sequence that reads the same backward as forward). The function should ignore spaces, punctuation, and case sensitivity. Test your function with various strings in the main() function. 17 | bool isPalindrome(const string &str) { 18 | int left = 0; 19 | int right = str.length() - 1; 20 | while(left < right) { 21 | if(str[left] != str[right]) return false; 22 | left++; 23 | right--; 24 | } 25 | return true; 26 | } 27 | 28 | int main() { 29 | int array[] = {1, 5, 3, 9, 2}; 30 | cout << findMax(array, 5) << '\n'; 31 | string str = "redivider"; 32 | cout << isPalindrome(str); 33 | } -------------------------------------------------------------------------------- /day15.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | // Ques 1: Write a program that defines a Student class and uses the this pointer in a member function to display the student's details. 6 | class Student { 7 | public: 8 | string name; 9 | int roll; 10 | int age; 11 | 12 | void displayDetails() { 13 | cout << "Name: " << this->name << endl; 14 | cout << "Roll: " << this->roll << endl; 15 | cout << "Age: " << this->age << endl; 16 | } 17 | }; 18 | 19 | // Ques 2: Write a C++ code that take linked list from user input and a value integer . Delete the value from the linkedlist. 20 | class Node { 21 | public: 22 | int val; 23 | Node* next; 24 | }; 25 | void deleteNode(Node* &head, int value) { 26 | if (head == nullptr) return; 27 | 28 | if (head->val == value) { 29 | Node* del = head; 30 | head = head->next; 31 | delete del; 32 | return; 33 | } 34 | 35 | Node* current = head; 36 | while (current->next != nullptr && current->next->val != value) { 37 | current = current->next; 38 | } 39 | 40 | if (current->next == nullptr) return; 41 | 42 | Node* del = current->next; 43 | current->next = current->next->next; 44 | delete del; 45 | } 46 | 47 | int main() 48 | { 49 | return 0; 50 | } -------------------------------------------------------------------------------- /MIPday04.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // Ques 4. You are given an array prices where prices[i] is the price of a given stock on the ith day. 5 | 6 | // You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. 7 | 8 | // Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0. 9 | 10 | // Example 1: 11 | // Input: prices = [7,1,5,3,6,4] 12 | // Output: 5 13 | // Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. 14 | // Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell. 15 | 16 | using namespace std; 17 | 18 | int maxProfit(vector& prices) { 19 | int n = prices.size(); 20 | vector maxInFuture(n); 21 | 22 | int maxVal = INT_MIN; 23 | 24 | for(int i = n-1; i >= 0; --i) { 25 | maxVal = max(prices[i], maxVal); 26 | maxInFuture[i] = maxVal; 27 | } 28 | 29 | int profit = 0; 30 | 31 | for(int i = 0; i < n; ++i) { 32 | profit = max(profit, maxInFuture[i] - prices[i]); 33 | } 34 | 35 | return profit; 36 | } 37 | 38 | int main() 39 | { 40 | vector prices = {7, 1, 5, 3, 6, 4}; 41 | cout << "Maximum profit: " << maxProfit(prices) << endl; 42 | return 0; 43 | } -------------------------------------------------------------------------------- /day19.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | // Ques 1: Write a C++ program that take txt file input and write your details inside that file through c++ and then print this details. 7 | void q1() { 8 | ofstream file("day19q1.txt", ios::app); 9 | if(file.fail()) { 10 | cout << "Error opening file"; 11 | return; 12 | } 13 | file << "Anurag Kumar\n"; 14 | file << "School of Computer Science\n"; 15 | file << "Bachelor of Technology"; 16 | file.close(); 17 | 18 | ifstream readfile("day19q1.txt"); 19 | if(readfile.fail()) { 20 | cout << "error opening file"; 21 | return; 22 | } 23 | string line; 24 | while(getline(readfile, line)) { 25 | cout << line << '\n'; 26 | } 27 | readfile.close(); 28 | } 29 | 30 | // Ques 2: Write C++ program that take input from the txt file and then print output inside terminal. Take any txt file by yourself. 31 | void q2() { 32 | ifstream file("day19q2.txt"); 33 | if(file.fail()) { 34 | cout << "error opening file"; 35 | return; 36 | } 37 | string line; 38 | while(!file.eof()) { 39 | getline(file, line); 40 | cout << line << '\n'; 41 | } 42 | file.close(); 43 | } 44 | 45 | int main() 46 | { 47 | q1(); 48 | q2(); 49 | return 0; 50 | } -------------------------------------------------------------------------------- /day33.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | // Ques 1. Given a string s, sort it in decreasing order based on the frequency of the characters. The frequency of a character is the number of times it appears in the string. 8 | 9 | // Return the sorted string. If there are multiple answers, return any of them. 10 | 11 | // Input: s = "tree" 12 | // Output: "eert" 13 | // Explanation: 'e' appears twice while 'r' and 't' both appear once. 14 | // So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer. Note :- Take all the required input from user. 15 | 16 | 17 | 18 | string q1(string s) { 19 | unordered_map freq; 20 | for (char c : s) { 21 | freq[c]++; 22 | } 23 | 24 | vector> freqVec; 25 | for (auto& it : freq) { 26 | freqVec.push_back({it.second, it.first}); 27 | } 28 | 29 | sort(freqVec.rbegin(), freqVec.rend()); 30 | 31 | string result; 32 | for (auto& it : freqVec) { 33 | result.append(it.first, it.second); 34 | } 35 | 36 | return result; 37 | } 38 | 39 | int main() { 40 | string s; 41 | cout << "Enter the string: "; 42 | cin >> s; 43 | 44 | string sortedString = q1(s); 45 | cout << sortedString << endl; 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /MIPday01.cpp: -------------------------------------------------------------------------------- 1 | // Ques 1 : Given an array of size N. The task is to find the maximum and the minimum element of the array using the minimum number of comparisons. 2 | 3 | // Sample:- 4 | // Input: arr[] = {3, 5, 4, 1, 9} 5 | // Output: Minimum element is: 1 6 | // Maximum element is: 9 7 | 8 | // Input: arr[] = {22, 14, 8, 17, 35, 3} 9 | // Output: Minimum element is: 3 10 | // Maximum element is: 35. 11 | // Take all the input from user. 12 | // Write all the posible solutions. 13 | 14 | #include 15 | #include 16 | #include 17 | 18 | using namespace std; 19 | 20 | void findMinMax(const vector& arr, int& min, int& max) { 21 | min = INT_MAX; 22 | max = INT_MIN; 23 | for (int num : arr) { 24 | if (num < min) { 25 | min = num; 26 | } 27 | if (num > max) { 28 | max = num; 29 | } 30 | } 31 | } 32 | 33 | int main() { 34 | int n; 35 | cout << "Enter the number of elements in the array: "; 36 | cin >> n; 37 | 38 | vector arr(n); 39 | cout << "Enter the elements of the array: "; 40 | for (int i = 0; i < n; ++i) { 41 | cin >> arr[i]; 42 | } 43 | 44 | int min, max; 45 | findMinMax(arr, min, max); 46 | 47 | cout << "Minimum element is: " << min << endl; 48 | cout << "Maximum element is: " << max << endl; 49 | 50 | return 0; 51 | } -------------------------------------------------------------------------------- /day27.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | // Ques 1. Given an infix expression in the form of string str. Convert this infix expression to postfix expression. 8 | 9 | int precedence(char op) { 10 | if(op == '+'|| op == '-') return 1; 11 | if(op == '*'|| op == '/') return 2; 12 | if(op == '^') return 3; 13 | return 0; 14 | } 15 | 16 | string infixToPostfix(string s) { 17 | stack st; 18 | string result; 19 | 20 | for(char& c : s) { 21 | if(isalnum(c)) { 22 | result += c; 23 | } else if(c == '(') { 24 | st.push(c); 25 | } else if(c == ')') { 26 | while(!st.empty() && st.top() != '(') { 27 | result += st.top(); 28 | st.pop(); 29 | } 30 | st.pop(); 31 | } else { 32 | while(!st.empty() && precedence(st.top()) >= precedence(c)) { 33 | result += st.top(); 34 | st.pop(); 35 | } 36 | st.push(c); 37 | } 38 | } 39 | 40 | while(!st.empty()) { 41 | result += st.top(); 42 | st.pop(); 43 | } 44 | 45 | return result; 46 | } 47 | 48 | int main() 49 | { 50 | string infix = "a+b*(c^d-e)^(f+g*h)-i"; 51 | string postfix = infixToPostfix(infix); 52 | cout << postfix; 53 | return 0; 54 | } -------------------------------------------------------------------------------- /day32.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Ques 1. Implement Queues using linkedlist. 5 | 6 | class Node { 7 | public: 8 | int data; 9 | Node* next; 10 | Node(int val) : data(val), next(nullptr) {} 11 | }; 12 | 13 | class Queue { 14 | private: 15 | Node* front; 16 | Node* rear; 17 | public: 18 | Queue() : front(nullptr), rear(nullptr) {} 19 | 20 | void enqueue(int val) { 21 | Node* newNode = new Node(val); 22 | if (rear == nullptr) { 23 | front = rear = newNode; 24 | return; 25 | } 26 | rear->next = newNode; 27 | rear = newNode; 28 | } 29 | 30 | void dequeue() { 31 | if (front == nullptr) { 32 | cout << "Queue is empty" << endl; 33 | return; 34 | } 35 | Node* temp = front; 36 | front = front->next; 37 | if (front == nullptr) { 38 | rear = nullptr; 39 | } 40 | delete temp; 41 | } 42 | 43 | int peek() { 44 | if (front == nullptr) { 45 | cout << "Queue is empty" << endl; 46 | return -1; 47 | } 48 | return front->data; 49 | } 50 | 51 | bool isEmpty() { 52 | return front == nullptr; 53 | } 54 | 55 | ~Queue() { 56 | while (!isEmpty()) { 57 | dequeue(); 58 | } 59 | } 60 | }; 61 | 62 | int main() 63 | { 64 | 65 | return 0; 66 | } -------------------------------------------------------------------------------- /day07.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // Ques-01 Write C++ program that take array from the user and sort the array using bubble sort algorithm. 6 | void bubbleSort(int arr[], int size) { 7 | for (int i = 0; i < size - 1; i++) { 8 | for (int j = 0; j < size - i - 1; j++) { 9 | if (arr[j] > arr[j + 1]) { 10 | int temp = arr[j]; 11 | arr[j] = arr[j + 1]; 12 | arr[j + 1] = temp; 13 | } 14 | } 15 | } 16 | } 17 | 18 | 19 | // Ques-02 Write C++ program that take array from the user and sort the array using insertion sort algorithm. 20 | void insertionSort(int arr[], int size) { 21 | for (int i = 1; i < size; i++) { 22 | int e = arr[i]; 23 | int j = i - 1; 24 | 25 | while (j >= 0 && arr[j] > e) { 26 | arr[j + 1] = arr[j]; 27 | j--; 28 | } 29 | 30 | arr[j + 1] = e; 31 | } 32 | } 33 | 34 | void displayArray(int array[], int size) { 35 | for(int i = 0; i < size; ++i) { 36 | cout << array[i] << " "; 37 | } 38 | cout << "\n"; 39 | } 40 | 41 | 42 | int main() { 43 | int size; 44 | int array[50]; 45 | cin >> size; 46 | for(int i = 0; i> array[i]; 48 | } 49 | bubbleSort(array, size); 50 | displayArray(array, size); 51 | insertionSort(array, size); 52 | displayArray(array, size); 53 | } -------------------------------------------------------------------------------- /day18.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | // Ques 1: You are tasked with creating a Person class in C++ that stores details of a person, such as firstName, lastName, and age. The class should: 7 | // Have a constructor that takes firstName, lastName, and age as arguments and initializes the member variables. 8 | // Include a method getFullName() that returns the full name of the person (a concatenation of firstName and lastName). 9 | // Add a method getDetails() that returns the firstName, lastName, and age in a tuple. 10 | // Demonstrate the usage of structured bindings to unpack the tuple returned by getDetails(). 11 | 12 | class Person { 13 | string firstName; 14 | string lastName; 15 | int age; 16 | 17 | public: 18 | Person(string fname, string lname, int a) : firstName(fname), lastName(lname), age(a) {}; 19 | 20 | string getFullName() { 21 | return firstName+" "+lastName; 22 | } 23 | 24 | tuple getDetails() { 25 | tuple result = make_tuple(this->firstName, this->lastName, this->age); 26 | return result; 27 | } 28 | }; 29 | 30 | 31 | int main() 32 | { 33 | Person p1("Anurag", "Kumar", 20); 34 | cout << p1.getFullName() << '\n'; 35 | auto [firstName, lastName, age] = p1.getDetails(); 36 | cout << firstName << " " << lastName << '\n'; 37 | cout << age; 38 | return 0; 39 | } -------------------------------------------------------------------------------- /day09.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // Ques 1: Write a C++ program that declares an integer variable, initializes it, and then declares a pointer that stores the address of this variable. Print the value of the variable using both the variable itself and the pointer. 6 | void question1() { 7 | int x; 8 | x = 14; 9 | int* ptrx = &x; 10 | cout << x << "\n"; 11 | cout << *ptrx << "\n"; 12 | } 13 | 14 | 15 | // Ques 2: Write a program that declares an array of integers and a pointer that points to the first element of the array. Use pointer arithmetic to print all the elements of the array. 16 | void question2() { 17 | int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}; 18 | int n = 14; 19 | int *p = array; 20 | for(int i = 0; i < n; ++i) { 21 | cout << *(p+i) << " "; 22 | } 23 | cout << "\n"; 24 | } 25 | 26 | // Create a program that demonstrates the use of a pointer to a pointer. Declare an integer variable, a pointer to the integer, and a pointer to that pointer. Use all three to print the value of the integer. 27 | void question3() { 28 | int x = 14; 29 | int* px = &x; 30 | int** ppx = &px; 31 | 32 | cout << "Direct Variable: " << x << "\n"; 33 | cout << "Through Pointer Variable: " << *px << "\n"; 34 | cout << "Through Pointer to Pointer Variable: " << **ppx << "\n"; 35 | } 36 | 37 | int main() { 38 | question1(); 39 | question2(); 40 | question3(); 41 | } -------------------------------------------------------------------------------- /day14.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Ques 1: Define a car class with properties brand name , model, engine, number of seats and method displayInfo that display the all information of class. 5 | class Car { 6 | public: 7 | string name; 8 | string model; 9 | string engine; 10 | int noOfSeats; 11 | 12 | void displayInfo() { 13 | cout << "Brand Name: " << name << endl; 14 | cout << "Model: " << model << endl; 15 | cout << "Engine: " << engine << endl; 16 | cout << "Number of Seats: " << noOfSeats << endl; 17 | } 18 | }; 19 | 20 | // Ques 2: Write a program that defines a Rectangle class with a copy constructor 21 | class Rectangle { 22 | public: 23 | int length; 24 | int breadth; 25 | 26 | Rectangle(int l=0, int b=0) : length(l), breadth(b) {}; 27 | 28 | Rectangle(Rectangle& obj) { 29 | this->length = obj.length; 30 | this->breadth = obj.breadth; 31 | } 32 | }; 33 | 34 | 35 | 36 | 37 | int main() 38 | { 39 | // Ques 1 40 | Car car1; 41 | car1.name = "Tata"; 42 | car1.model = "Nexon"; 43 | car1.engine = "V6"; 44 | car1.noOfSeats = 5; 45 | car1.displayInfo(); 46 | 47 | // Ques 2 48 | Rectangle rect1; 49 | rect1.length = 10; 50 | rect1.breadth = 5; 51 | 52 | Rectangle rect2 = rect1; 53 | cout << "Rectangle 2 Length: " << rect2.length << endl; 54 | cout << "Rectangle 2 Breadth: " << rect2.breadth << endl; 55 | 56 | return 0; 57 | } -------------------------------------------------------------------------------- /day17.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Ques 1: Write a C++ code that take 2D array and key from user. Now you have to check that key exist inside the array or not if it exits print YES otherwise NO. 5 | void findKey(int array[][10], int r, int c, int key) { 6 | for(int i = 0; i < r; ++i) { 7 | for(int j = 0; j < c; ++j) { 8 | if(array[i][j] == key) { 9 | cout << "YES" << '\n'; 10 | return; 11 | } 12 | } 13 | } 14 | cout << "NO" << '\n'; 15 | } 16 | 17 | // Ques 2: Write a C++ code that take 2D array from user input. Now you have to find sum of all values of the array. 18 | int sum(int array[][10], int r, int c) { 19 | int sum = 0; 20 | for(int i = 0; i < r; ++i) { 21 | for(int j = 0; j < c; ++j) { 22 | sum+=array[i][j]; 23 | } 24 | } 25 | return sum; 26 | } 27 | 28 | int main() 29 | { 30 | int r, c, key; 31 | cout << "Enter number of rows and columns: "; 32 | cin >> r >> c; 33 | 34 | int array[10][10]; 35 | cout << "Enter the elements of the array:\n"; 36 | for(int i = 0; i < r; ++i) { 37 | for(int j = 0; j < c; ++j) { 38 | cin >> array[i][j]; 39 | } 40 | } 41 | 42 | cout << "Enter the key to find: "; 43 | cin >> key; 44 | 45 | findKey(array, r, c, key); 46 | 47 | int totalSum = sum(array, r, c); 48 | cout << "Sum of all elements: " << totalSum << '\n'; 49 | 50 | return 0; 51 | } -------------------------------------------------------------------------------- /day26.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Ques 1. Implement the priority queue using array. 5 | 6 | class PriorityQueue { 7 | private: 8 | 9 | int elements[100]; 10 | int priorities[100]; 11 | int size; 12 | 13 | public: 14 | PriorityQueue() : size(0) {} 15 | 16 | void push(int element, int priority) { 17 | if (size >= 100) { 18 | cout << "Queue overflow" << endl; 19 | return; 20 | } 21 | int i; 22 | for (i = size - 1; i >= 0 && priorities[i] > priority; i--) { 23 | elements[i + 1] = elements[i]; 24 | priorities[i + 1] = priorities[i]; 25 | } 26 | elements[i + 1] = element; 27 | priorities[i + 1] = priority; 28 | size++; 29 | } 30 | 31 | void pop() { 32 | if (size > 0) { 33 | size--; 34 | } else { 35 | cout << "Queue is empty" << endl; 36 | } 37 | } 38 | 39 | int top() { 40 | if (size > 0) { 41 | return elements[size - 1]; 42 | } else { 43 | cout << "Queue is empty" << endl; 44 | return -1; 45 | } 46 | } 47 | 48 | bool empty() { 49 | return size == 0; 50 | } 51 | }; 52 | 53 | int main() { 54 | PriorityQueue q1; 55 | q1.push(4, 6); 56 | q1.push(10, 2); 57 | q1.push(1, 3); 58 | q1.push(3, 5); 59 | q1.push(8, 1); 60 | q1.push(2, 9); 61 | cout << q1.top() << "\n"; 62 | q1.pop(); 63 | cout << q1.top(); 64 | return 0; 65 | } -------------------------------------------------------------------------------- /day03.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Ques - 01: Write a C++ program that takes an integer N as input from the user and calculates the sum of the first N natural numbers using a for loop. 5 | int calculateSum(int N) { 6 | int sum = 0; 7 | for (int i = 1; i <= N; i++) { 8 | sum += i; 9 | } 10 | return sum; 11 | } 12 | 13 | // Ques-02: Write a C++ program that takes an integer N as input from the user and calculates the factorial of N using a loop. 14 | int calculateFactorial(int N) { 15 | int factorial = 1; 16 | for (int i = 1; i <= N; i++) { 17 | factorial *= i; 18 | } 19 | return factorial; 20 | } 21 | 22 | // Ques - 03: Write a C++ program that takes an integer N as input from the user and prints the multiplication table of N up to 10 using a loop. 23 | 24 | void printTable(int N) { 25 | cout << "Multiplication table of " << N << ":\n"; 26 | for (int i = 1; i <= 10; i++) { 27 | cout << N << " x " << i << " = " << N * i << "\n"; 28 | } 29 | } 30 | 31 | int main() { 32 | int N; 33 | 34 | cout << "Enter a number: "; 35 | cin >> N; 36 | int sum = calculateSum(N); 37 | cout << "The sum of the first " << N << " natural numbers is: " << sum << "\n"; 38 | 39 | cout << "Enter a number: "; 40 | cin >> N; 41 | int factorial = calculateFactorial(N); 42 | cout << "The factorial of " << N << " is: " << factorial << "\n"; 43 | 44 | cout << "Enter a number: "; 45 | cin >> N; 46 | printTable(N); 47 | 48 | return 0; 49 | } -------------------------------------------------------------------------------- /day23.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | // Ques 1: You are given an array of integer and target integer now you have to tell it posible to select two elements from an array such that their sum is equal to target. If it is possible then print YES otherwise print NO. Sample testcase arr :[2,7,11,15], target = 9 output : YES . Write code that work in O(n^2) complexity and O(n*logn) complexity. Take all the required input from user. 8 | 9 | // O(n^2) complexity solution 10 | bool q1_N2(const vector& arr, int target) { 11 | int n = arr.size(); 12 | for (int i = 0; i < n; ++i) { 13 | for (int j = i + 1; j < n; ++j) { 14 | if (arr[i] + arr[j] == target) { 15 | return true; 16 | } 17 | } 18 | } 19 | return false; 20 | } 21 | 22 | // O(n*logn) complexity solution 23 | bool q1_nlogn(vector& arr, int target) { 24 | sort(arr.begin(), arr.end()); 25 | int left = 0, right = arr.size() - 1; 26 | while (left < right) { 27 | int sum = arr[left] + arr[right]; 28 | if (sum == target) { 29 | return true; 30 | } else if (sum < target) { 31 | ++left; 32 | } else { 33 | --right; 34 | } 35 | } 36 | return false; 37 | } 38 | 39 | int main() { 40 | int n, target; 41 | 42 | cin >> n; 43 | vector arr(n); 44 | 45 | for (int i = 0; i < n; ++i) { 46 | cin >> arr[i]; 47 | } 48 | 49 | cin >> target; 50 | 51 | cout << q1_N2(arr, target) << "\n"; 52 | cout << q1_nlogn(arr, target); 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /day01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | // Ques - 01 - Write a C++ program that takes two strings as input from the user and concatenates them. Then, output the concatenated string. 7 | 8 | void concatenateString(string s1, string s2) { 9 | string result = s1 + s2; 10 | cout << "Concatenated string: " << result << endl; 11 | } 12 | 13 | // Ques-02 - Write a C++ program that takes five grades (integers) as input from the user and calculates the average grade. Output the average grade to the user. 14 | double findAverageGrade(int g1, int g2, int g3, int g4, int g5) { 15 | return (g1+g2+g3+g4+g5)/5.0; 16 | } 17 | 18 | // Ques-03 - Write a C++ program that takes an integer as input from the user and outputs its square. 19 | unsigned int findSquare(int n) { 20 | return n*n; 21 | } 22 | 23 | int main() { 24 | string s1, s2; 25 | cout << "Enter the first string: "; 26 | getline(cin, s1); 27 | cout << "Enter the second string: "; 28 | getline(cin, s2); 29 | concatenateString(s1, s2); 30 | 31 | int g1, g2, g3, g4, g5; 32 | 33 | cout << "Enter Grade 1: "; 34 | cin >> g1; 35 | cout << "Enter Grade 2: "; 36 | cin >> g2; 37 | cout << "Enter Grade 3: "; 38 | cin >> g3; 39 | cout << "Enter Grade 4: "; 40 | cin >> g4; 41 | cout << "Enter Grade 5: "; 42 | cin >> g5; 43 | double average = findAverageGrade(g1, g2, g3, g4, g5); 44 | cout << "The average grade is: " << fixed << setprecision(4) << average << endl; 45 | 46 | int n; 47 | cout << "Enter a number: "; 48 | cin >> n; 49 | unsigned int square = findSquare(n); 50 | cout << "The square of " << n << " is " << square << endl; 51 | 52 | } -------------------------------------------------------------------------------- /MIPday02.cpp: -------------------------------------------------------------------------------- 1 | // Ques 2 . Given an array arr[] of n integers where arr[i] represents the number of chocolates in ith packet. Each packet can have a variable number of chocolates. There are m students, the task is to distribute chocolate packets such that: 2 | 3 | // Each student gets exactly one packet. 4 | // The difference between the maximum and minimum number of chocolates in the packets given to the students is minimized. 5 | 6 | // Sample:- 7 | // Input: arr[] = {7, 3, 2, 4, 9, 12, 56}, m = 3 8 | // Output: 2 9 | // Explanation: If we distribute chocolate packets {3, 2, 4}, we will get the minimum difference, that is 2. 10 | 11 | // Input: arr[] = {7, 3, 2, 4, 9, 12, 56}, m = 5 12 | // Output: 7 13 | // Explanation: If we distribute chocolate packets {3, 2, 4, 9, 7}, we will get the minimum difference, that is 9 – 2 = 7. 14 | 15 | 16 | #include 17 | using namespace std; 18 | 19 | 20 | int findMinDiff(int n, int m, vector chocolates) { 21 | 22 | sort(chocolates.begin(), chocolates.end()); 23 | 24 | int i = 0; 25 | int j = i+m-1; 26 | 27 | int diff = chocolates[j] - chocolates[i]; 28 | 29 | while(j < n) { 30 | diff = min(diff, chocolates[j] - chocolates[i]); 31 | ++i; 32 | ++j; 33 | } 34 | return diff; 35 | 36 | } 37 | 38 | int main() { 39 | vector chocolates = {7, 3, 2, 4, 9, 12, 56}; 40 | int m = 3; 41 | int n = chocolates.size(); 42 | 43 | int result = findMinDiff(n, m, chocolates); 44 | cout << "Minimum difference is " << result << endl; 45 | 46 | chocolates = {7, 3, 2, 4, 9, 12, 56}; 47 | m = 5; 48 | result = findMinDiff(n, m, chocolates); 49 | cout << "Minimum difference is " << result << endl; 50 | 51 | return 0; 52 | } -------------------------------------------------------------------------------- /day08.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | // Ques 1: Write a function int stringLength(const std::string &str) that returns the length of a given string. Test your function with various strings in the main() function. 4 | int stringLength(const string &str) { 5 | int length = 0; 6 | for(char c : str) { 7 | ++length; 8 | } 9 | return length; 10 | } 11 | 12 | // Ques 2: Implement a function int countCharacter(const std::string &str, char ch) that counts the number of occurrences of a specific character in a given string. Test the function with different strings and characters. 13 | int countCharacter(const string &str, char ch) { 14 | int count = 0; 15 | for(char c : str) { 16 | if(c==ch) { 17 | ++count; 18 | } 19 | } 20 | return count; 21 | } 22 | 23 | // Ques 3: Write a function std::string concatenateStrings(const std::string &str1, const std::string &str2) that concatenates two strings and returns the result. Test the function with various input strings. 24 | string concatenateStrings(const string &str1, const string &str2) { 25 | string output = str1+str2; 26 | return output; 27 | } 28 | 29 | 30 | 31 | 32 | int main() { 33 | string str1 = "Hello, World!"; 34 | string str2 = "This is a string"; 35 | string str3 = "Visual Studio Code"; 36 | 37 | cout << stringLength(str1) << endl; 38 | cout << stringLength(str2) << endl; 39 | cout << stringLength(str3) << endl; 40 | 41 | cout << countCharacter(str1, 'l') << endl; 42 | cout << countCharacter(str2, 'i') << endl; 43 | cout << countCharacter(str3, 'o') << endl; 44 | 45 | cout << concatenateStrings(str1, str2) << endl; 46 | cout << concatenateStrings(str2, str3) << endl; 47 | cout << concatenateStrings(str1, str3) << endl; 48 | } -------------------------------------------------------------------------------- /day31.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | // Ques 1. Given an array, find the nearest smaller element G[i] for every element A[i] in the array such that the element has an index smaller than i. 7 | 8 | // More formally, 9 | 10 | // G[i] for an element A[i] = an element A[j] such that 11 | // j is maximum possible AND 12 | // j < i AND 13 | // A[j] < A[i] 14 | // Elements for which no smaller element exist, consider next smaller element as -1. Note :- Take all the required input from user. Sample :- 15 | // Input 1: 16 | // A = [4, 5, 2, 10, 8] 17 | // Output 1: 18 | // G = [-1, 4, -1, 2, 2] 19 | // Explaination 1: 20 | // index 1: No element less than 4 in left of 4, G[1] = -1 21 | // index 2: A[1] is only element less than A[2], G[2] = A[1] 22 | // index 3: No element less than 2 in left of 2, G[3] = -1 23 | // index 4: A[3] is nearest element which is less than A[4], G[4] = A[3] 24 | // index 4: A[3] is nearest element which is less than A[5], G[5] = A[3] 25 | 26 | vector q1(vector nums) { 27 | int n = nums.size(); 28 | vector result(n); 29 | vector lowestToLeft(n); 30 | 31 | int min = INT_MAX; 32 | for(int i = 0; i < n; ++i) { 33 | if(nums[i] < min) { 34 | min = nums[i]; 35 | } 36 | lowestToLeft[i] = min; 37 | }; 38 | 39 | for(int i = 0; i < n; ++i) { 40 | if(lowestToLeft[i] < nums[i]) { 41 | result[i] = lowestToLeft[i]; 42 | } 43 | else { 44 | result[i] = -1; 45 | } 46 | } 47 | return result; 48 | 49 | } 50 | int main() 51 | { 52 | vector nums = {4, 5, 2, 10, 8}; 53 | vector result = q1(nums); 54 | for(int i : result) { 55 | cout << i << " "; 56 | } 57 | return 0; 58 | } -------------------------------------------------------------------------------- /day06.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define SIZE 50 3 | 4 | using namespace std; 5 | 6 | // Ques-01 Write a C++ program that takes an array as input and prints the sum of the array. 7 | void sumOfArray(int array[], int size) { 8 | int sum = 0; 9 | for(int i = 0; i < size; ++i) { 10 | sum += array[i]; 11 | } 12 | cout << "Sum of the array: " << sum << endl; 13 | } 14 | 15 | // Ques-02 Write a C++ program that takes an array as input and prints the sum of odd numbers and the sum of even numbers separately. 16 | void sumOfOddandEven(int array[], int size) { 17 | int oddSum = 0; 18 | int evenSum = 0; 19 | for(int i = 0; i < size; ++i) { 20 | if(array[i]%2==0) { 21 | evenSum += array[i]; 22 | } 23 | else { 24 | oddSum += array[i]; 25 | } 26 | } 27 | cout << "Sum of odd numbers: " << oddSum << endl; 28 | cout << "Sum of even numbers: " << evenSum << endl; 29 | } 30 | 31 | // Ques-03 Write a C++ program that takes an array as input and prints the number of odd numbers and the number of even numbers in the array. 32 | void countOddandEven(int array[], int size) { 33 | int oddCount = 0; 34 | int evenCount = 0; 35 | for(int i = 0; i < size; ++i) { 36 | if(array[i]%2==0) { 37 | ++evenCount; 38 | } 39 | else { 40 | ++oddCount; 41 | } 42 | } 43 | cout << "Number of odd numbers: " << oddCount << endl; 44 | cout << "NUmber of even numbers: " << evenCount << endl; 45 | } 46 | 47 | int main() { 48 | int array[SIZE]; 49 | int n; 50 | cout << "Enter the size of the array: "; 51 | cin >> n; 52 | cout << "Enter the array: "; 53 | for(int i = 0; i < n; ++i) { 54 | cin >> array[i]; 55 | } 56 | sumOfArray(array, n); 57 | sumOfOddandEven(array, n); 58 | countOddandEven(array, n); 59 | } -------------------------------------------------------------------------------- /day25.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | // Ques 1. Given a non-empty array of integers nums, every element appears twice except for one. Find that single one. 6 | int findSingle(vector &array) { 7 | int xorsum = 0; 8 | for(int i = 0; i < array.size(); ++i) { 9 | xorsum ^= array.at(i); 10 | } 11 | if(xorsum != 0) { 12 | return xorsum; 13 | } 14 | else { 15 | cout << "All elements appear twice"; 16 | return -1; 17 | } 18 | } 19 | 20 | // Ques 2. Implement the stack using linkedlist. 21 | class Node { 22 | public: 23 | int data; 24 | Node* next; 25 | Node(int val) : data(val), next(nullptr) {} 26 | }; 27 | 28 | class Stack { 29 | private: 30 | Node* top; 31 | public: 32 | Stack() : top(nullptr) {} 33 | 34 | void push(int val) { 35 | Node* newNode = new Node(val); 36 | newNode->next = top; 37 | top = newNode; 38 | } 39 | void pop() { 40 | if (top == nullptr) { 41 | cout << "Stack Underflow" << endl; 42 | return; 43 | } 44 | Node* temp = top; 45 | top = top->next; 46 | delete temp; 47 | } 48 | int peek() { 49 | if (top == nullptr) { 50 | cout << "Stack is empty" << endl; 51 | return -1; 52 | } 53 | return top->data; 54 | } 55 | bool isEmpty() { 56 | return top == nullptr; 57 | } 58 | 59 | ~Stack() { 60 | while (top != nullptr) { 61 | pop(); 62 | } 63 | } 64 | }; 65 | 66 | int main() 67 | { 68 | vector array = {3,2,4,2,1,1,3}; 69 | cout << findSingle(array) << "\n"; 70 | Stack myStack; 71 | cout << myStack.isEmpty() << "\n"; 72 | myStack.push(14); 73 | myStack.push(12); 74 | myStack.push(10); 75 | cout << myStack.peek() << "\n"; 76 | myStack.pop(); 77 | cout << myStack.peek() << "\n"; 78 | } -------------------------------------------------------------------------------- /day05.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | // Ques-01 Write a C++ function that checks whether an integer is odd or even. If the integer is odd, print "Odd"; otherwise, print "Even." 7 | void isEvenOrOdd(int n) { 8 | if(n%2==0) { 9 | cout << "Even"; 10 | } 11 | else { 12 | cout << "Odd"; 13 | } 14 | cout << endl; 15 | } 16 | 17 | // Ques-02 Write a C++ function that takes a string as input and reverses it. 18 | 19 | void swap(char *c1, char *c2) { 20 | char temp = *c1; 21 | *c1 = *c2; 22 | *c2 = temp; 23 | } 24 | void reverseString(string s) { 25 | int p1 = 0; 26 | int p2 = s.length() - 1; 27 | 28 | while(p1 < p2) { 29 | swap(&s[p1], &s[p2]); 30 | ++p1; 31 | --p2; 32 | } 33 | cout << "Reversed string: " << s << endl; 34 | } 35 | 36 | // Ques-03 Write a C++ function that takes a string as a parameter and checks how many vowels and consonants are in that string. 37 | // Assuming that string contains only lowercase. 38 | 39 | void countVowelsAndConsonants(string s) { 40 | int vowelCount = 0; 41 | int consonantCount = 0; 42 | for(char c : s) { 43 | switch (c) { 44 | case 'a': 45 | case 'e': 46 | case 'i': 47 | case 'o': 48 | case 'u': 49 | ++vowelCount; 50 | break; 51 | default: 52 | ++consonantCount; 53 | break; 54 | } 55 | } 56 | cout << "Number of vowels: " << vowelCount << endl; 57 | cout << "Number of consonants: " << consonantCount << endl; 58 | 59 | } 60 | 61 | int main() { 62 | int n; 63 | cout << "Enter a number: "; 64 | cin >> n; 65 | isEvenOrOdd(n); 66 | string s; 67 | cout << "Enter a string: "; 68 | cin >> s; 69 | reverseString(s); 70 | cout << "Enter a string: "; 71 | cin >> s; 72 | countVowelsAndConsonants(s); 73 | } 74 | 75 | -------------------------------------------------------------------------------- /day34.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Ques 1. Given the head of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return the reordered list. 5 | 6 | // The first node is considered odd, and the second node is even, and so on. 7 | 8 | // Note that the relative order inside both the even and odd groups should remain as it was in the input. 9 | 10 | // You must solve the problem in O(1) extra space complexity and O(n) time complexity. 11 | 12 | // Input: head = [1,2,3,4,5] 13 | // Output: [1,3,5,2,4]. Note :- Take all the required input from user. 14 | 15 | struct ListNode { 16 | int val; 17 | ListNode* next; 18 | ListNode(int x) : val(x), next(NULL) {} 19 | }; 20 | 21 | ListNode* oddEvenList(ListNode* head) { 22 | if (!head) return head; 23 | 24 | ListNode* odd = head; 25 | ListNode* even = head->next; 26 | ListNode* evenHead = even; 27 | 28 | while (even && even->next) { 29 | odd->next = even->next; 30 | odd = odd->next; 31 | even->next = odd->next; 32 | even = even->next; 33 | } 34 | 35 | odd->next = evenHead; 36 | return head; 37 | } 38 | 39 | void printList(ListNode* head) { 40 | while (head) { 41 | cout << head->val << " "; 42 | head = head->next; 43 | } 44 | cout << endl; 45 | } 46 | 47 | ListNode* createList() { 48 | int n, val; 49 | cout << "Enter the number of nodes: "; 50 | cin >> n; 51 | if (n <= 0) return NULL; 52 | 53 | cout << "Enter the value of node 1: "; 54 | cin >> val; 55 | ListNode* head = new ListNode(val); 56 | ListNode* current = head; 57 | 58 | for (int i = 2; i <= n; ++i) { 59 | cout << "Enter the value of node " << i << ": "; 60 | cin >> val; 61 | current->next = new ListNode(val); 62 | current = current->next; 63 | } 64 | 65 | return head; 66 | } 67 | 68 | int main() { 69 | ListNode* head = createList(); 70 | 71 | head = oddEvenList(head); 72 | cout << "Reordered list: "; 73 | printList(head); 74 | 75 | return 0; 76 | } 77 | -------------------------------------------------------------------------------- /day35.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | // Ques 1. There is a road, which can be represented as a number line. You are located in the point 0 of the number line, and you want to travel from the point 0 to the point 𝑥, and back to the point 0. 7 | 8 | // You travel by car, which spends 1 liter of gasoline per 1 unit of distance travelled. When you start at the point 0, your car is fully fueled (its gas tank contains the maximum possible amount of fuel). 9 | 10 | // There are 𝑛 gas stations, located in points 𝑎1,𝑎2,…,𝑎𝑛. When you arrive at a gas station, you fully refuel your car. Note that you can refuel only at gas stations, and there are no gas stations in points 0 and 𝑥. 11 | 12 | // You have to calculate the minimum possible volume of the gas tank in your car (in liters) that will allow you to travel from the point 0 to the point 𝑥 and back to the point 0. 13 | 14 | // Input 15 | // The first line contains one integer 𝑡 (1≤𝑡≤1000) — the number of test cases. 16 | 17 | // Each test case consists of two lines: 18 | 19 | // the first line contains two integers 𝑛 and 𝑥 (1≤𝑛≤50; 2≤𝑥≤100); 20 | // the second line contains 𝑛 integers 𝑎1,𝑎2,…,𝑎𝑛 (0<𝑎1<𝑎2<⋯<𝑎𝑛<𝑥). 21 | // Output 22 | // For each test case, print one integer — the minimum possible volume of the gas tank in your car that will allow you to travel from the point 0 to the point 𝑥 and back. Input :- 3 23 | // 3 7 24 | // 1 2 5 25 | // 3 6 26 | // 1 2 5 27 | // 1 10 28 | // 7 29 | // Output :- 4 30 | // 3 31 | // 7 32 | 33 | 34 | int q1(int n, int x, const vector& gasStations) { 35 | int maxDistance = max(gasStations[0], x - gasStations[n - 1]); 36 | for (int i = 1; i < n; ++i) { 37 | maxDistance = max(maxDistance, gasStations[i] - gasStations[i - 1]); 38 | } 39 | return maxDistance; 40 | } 41 | 42 | int main() { 43 | int t; 44 | cin >> t; 45 | while (t--) { 46 | int n, x; 47 | cin >> n >> x; 48 | vector gasStations(n); 49 | for (int i = 0; i < n; ++i) { 50 | cin >> gasStations[i]; 51 | } 52 | cout << q1(n, x, gasStations) << endl; 53 | } 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /day22.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Ques 1: Write C++ program that create a stack using Array. 5 | 6 | class Stack { 7 | int top; 8 | int size; 9 | int *stack; 10 | 11 | public: 12 | Stack(int s) { 13 | this->top = -1; 14 | this->size = s; 15 | this->stack = new int[size]; 16 | } 17 | 18 | ~Stack() { 19 | delete[] stack; 20 | } 21 | 22 | void push(int data) { 23 | if(top >= size - 1) { 24 | cout << "Stack Overflow\n"; 25 | return; 26 | } 27 | ++top; 28 | stack[top] = data; 29 | cout << data << " was pushed\n"; 30 | } 31 | 32 | int pop() { 33 | if (top == -1) { 34 | cout << "Stack Underflow\n"; 35 | return -1; 36 | } 37 | return stack[top--]; 38 | } 39 | 40 | int peek() { 41 | return stack[top]; 42 | } 43 | 44 | }; 45 | 46 | // Ques 2 : Write C++ program that take n from the user the number of elements and insert all the value inside stack after inserting all value inside stack pop all the values and print. 47 | void q2() { 48 | int n; 49 | cout << "Enter the size of stack "; 50 | cin >> n; 51 | Stack myStack(n); 52 | int value; 53 | for (int i = 0; i < n; ++i) { 54 | cin >> value; 55 | myStack.push(value); 56 | } 57 | cout << "Popping all values:\n"; 58 | while (true) { 59 | int poppedValue = myStack.pop(); 60 | if (poppedValue == -1) break; 61 | cout << poppedValue << " was popped\n"; 62 | } 63 | } 64 | 65 | int main() 66 | { 67 | Stack myStack(5); 68 | myStack.push(4); 69 | myStack.push(3); 70 | myStack.push(1); 71 | myStack.push(2); 72 | myStack.push(12); 73 | myStack.push(15); 74 | cout << "Top element is " << myStack.peek() << endl; 75 | cout << myStack.pop() << " was popped\n"; 76 | cout << myStack.pop() << " was popped\n"; 77 | cout << "Top element is " << myStack.peek() << endl; 78 | cout << myStack.pop() << " was popped\n"; 79 | cout << myStack.pop() << " was popped\n"; 80 | cout << myStack.pop() << " was popped\n"; 81 | myStack.pop(); 82 | 83 | 84 | q2(); 85 | return 0; 86 | } -------------------------------------------------------------------------------- /day36.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | // Ques 1. Turtle and Piggy are playing a game on a sequence. They are given a sequence 𝑎1,𝑎2,…,𝑎𝑛, and Turtle goes first. Turtle and Piggy alternate in turns (so, Turtle does the first turn, Piggy does the second, Turtle does the third, etc.). 6 | 7 | // The game goes as follows: 8 | 9 | // Let the current length of the sequence be 𝑚. If 𝑚=1, the game ends. 10 | // If the game does not end and it's Turtle's turn, then Turtle must choose an integer 𝑖 such that 1≤𝑖≤𝑚−1, set 𝑎𝑖 to max(𝑎𝑖,𝑎𝑖+1), and remove 𝑎𝑖+1. 11 | // If the game does not end and it's Piggy's turn, then Piggy must choose an integer 𝑖 such that 1≤𝑖≤𝑚−1, set 𝑎𝑖 to min(𝑎𝑖,𝑎𝑖+1), and remove 𝑎𝑖+1. 12 | // Turtle wants to maximize the value of 𝑎1 in the end, while Piggy wants to minimize the value of 𝑎1 in the end. Find the value of 𝑎1 in the end if both players play optimally. Each test contains multiple test cases. The first line contains the number of test cases 𝑡 (1≤𝑡≤104). The description of the test cases follows. 13 | 14 | // The first line of each test case contains a single integer 𝑛 (2≤𝑛≤105) — the length of the sequence. 15 | 16 | // The second line of each test case contains 𝑛 integers 𝑎1,𝑎2,…,𝑎𝑛 (1≤𝑎𝑖≤105) — the elements of the sequence 𝑎. 17 | 18 | // It is guaranteed that the sum of 𝑛 over all test cases does not exceed 105. 19 | 20 | // Output 21 | // For each test case, output a single integer — the value of 𝑎1 in the end if both players play optimally. 22 | // Input :- 5 23 | // 2 24 | // 1 2 25 | // 3 26 | // 1 1 2 27 | // 3 28 | // 1 2 3 29 | // 5 30 | // 3 1 2 2 3 31 | // 10 32 | // 10 2 5 2 7 9 2 5 10 7 33 | 34 | // Output:- 2 35 | // 1 36 | // 2 37 | // 2 38 | // 7 39 | 40 | 41 | int q1(std::vector& a) { 42 | int n = a.size(); 43 | for (int i = n - 1; i > 0; --i) { 44 | if (i % 2 == 1) { 45 | a[i - 1] = std::max(a[i - 1], a[i]); 46 | } else { 47 | a[i - 1] = std::min(a[i - 1], a[i]); 48 | } 49 | } 50 | return a[0]; 51 | } 52 | 53 | int main() { 54 | int t; 55 | cin >> t; 56 | while (t--) { 57 | int n; 58 | cin >> n; 59 | std::vector a(n); 60 | for (int i = 0; i < n; ++i) { 61 | cin >> a[i]; 62 | } 63 | cout << q1(a) << endl; 64 | } 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /day20.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | // Ques 1: You are given a image in n*m matrix now you have to rotate the image by 90 degree. 8 | void reverse(vector& row) { 9 | int start = 0; 10 | int end = row.size()-1; 11 | 12 | while(start < end) { 13 | swap(row.at(start), row.at(end)); 14 | ++start; 15 | --end; 16 | } 17 | } 18 | 19 | void swap(int& a, int& b) { 20 | int temp = a; 21 | a = b; 22 | b = temp; 23 | } 24 | 25 | void rotate(vector>& matrix) { 26 | 27 | for(int i = 0; i < matrix.size(); ++i) { 28 | for(int j = i+1; j < matrix.at(0).size(); ++j) { 29 | swap(matrix[i][j], matrix[j][i]); 30 | } 31 | } 32 | 33 | for(int i = 0; i> sort2D(vector> matrix) { 40 | vector flatMatrix; 41 | int rows = matrix.size(); 42 | int columns = matrix.at(0).size(); 43 | 44 | for(int i = 0; i < rows; ++i) { 45 | for(int j = 0; j < columns; ++j) { 46 | flatMatrix.push_back(matrix.at(i).at(j)); 47 | } 48 | } 49 | sort(flatMatrix.begin(), flatMatrix.end()); 50 | vector> sortedMatrix(rows, vector(columns)); 51 | int count = 0; 52 | for(int i = 0; i < rows; ++i) { 53 | for(int j = 0; j < columns; ++j) { 54 | sortedMatrix[i][j] = flatMatrix[count]; 55 | ++count; 56 | } 57 | 58 | } 59 | return sortedMatrix; 60 | } 61 | 62 | int main() 63 | { 64 | vector> rotatematrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; 65 | rotate(rotatematrix); 66 | 67 | for(int i = 0; i < rotatematrix.size(); ++i) { 68 | for(int j = 0; j < rotatematrix[0].size(); ++j) { 69 | cout << rotatematrix[i][j] << " "; 70 | } 71 | cout << endl; 72 | } 73 | cout << endl; 74 | 75 | vector> matrix = {{7,4,1}, {8,5,2}, {9,6,3}}; 76 | vector> sorted = sort2D(matrix); 77 | 78 | for(int i = 0; i < sorted.size(); ++i) { 79 | for(int j = 0; j < sorted[0].size(); ++j) { 80 | cout << sorted[i][j] << " "; 81 | } 82 | cout << endl; 83 | } 84 | 85 | return 0; 86 | } -------------------------------------------------------------------------------- /day11.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | // Ques 1: You are given an array and target value. Your task is to find the target value that exists in the array. If it exists inside an array then print YES otherwise NO. 7 | 8 | // If the array is unsorted 9 | void isPresentInUnsorted(vector array, int target) { 10 | for(int i : array) { 11 | if (i == target) { 12 | cout << "YES" << '\n'; 13 | return; 14 | } 15 | } 16 | cout << "NO" << '\n'; 17 | } 18 | 19 | // If the array is sorted 20 | void isPresentInSorted(vector array, int target) { 21 | int start = 0; 22 | int end = array.size() - 1; 23 | int middle; 24 | while (start <= end) 25 | { 26 | middle = (start+end)/2; 27 | if(target == array.at(middle)) { 28 | cout << "YES" << '\n'; 29 | return; 30 | } 31 | else if(target < array.at(middle)) { 32 | end = middle - 1; 33 | } 34 | else { 35 | start = middle + 1; 36 | } 37 | } 38 | cout << "NO" << '\n'; 39 | } 40 | 41 | 42 | // Ques 2: You are given an array and target value. You have to find the index of target value. If target doest exist inside the array then print -1.Assume indexing is 1 based. 43 | 44 | // If the array is unsorted 45 | int findIndexUnsorted(vector array, int target) { 46 | int index = 0; 47 | for(int i : array) { 48 | if (i == target) { 49 | return index+1; 50 | } 51 | else { 52 | ++index; 53 | } 54 | } 55 | return -1; 56 | } 57 | 58 | // If the array is sorted 59 | int findIndexSorted(vector array, int target) { 60 | int start = 0; 61 | int end = array.size() - 1; 62 | int middle; 63 | while (start <= end) 64 | { 65 | middle = (start+end)/2; 66 | if(target == array.at(middle)) { 67 | return middle+1; 68 | } 69 | else if(target < array.at(middle)) { 70 | end = middle - 1; 71 | } 72 | else { 73 | start = middle + 1; 74 | } 75 | } 76 | return -1; 77 | } 78 | 79 | 80 | int main() { 81 | vector array = {1, 8, 6, 2, 12, 4, 9}; 82 | vector array1 = {1, 2, 3, 4, 5, 6, 7}; 83 | isPresentInUnsorted(array, 3); 84 | isPresentInSorted(array1, 3); 85 | cout << findIndexUnsorted(array, 8) << '\n'; 86 | cout << findIndexSorted(array1, 7) << '\n'; 87 | } -------------------------------------------------------------------------------- /day21.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Ques 1: Write C++ program to create a doubly linkedlist and insert a node at any position . 5 | class Node { 6 | public: 7 | int data; 8 | Node* next; 9 | Node* prev; 10 | 11 | Node(int val) : data(val), next(nullptr), prev(nullptr) {} 12 | }; 13 | 14 | Node* createDoublyLinkedList() { 15 | int n; 16 | cout << "Enter the number of nodes: "; 17 | cin >> n; 18 | 19 | if (n <= 0) return nullptr; 20 | 21 | Node* head = nullptr; 22 | Node* tail = nullptr; 23 | 24 | for (int i = 0; i < n; ++i) { 25 | int data; 26 | cout << "Enter data for node " << i + 1 << ": "; 27 | cin >> data; 28 | 29 | Node* newNode = new Node(data); 30 | if (head == nullptr) { 31 | head = newNode; 32 | tail = newNode; 33 | } else { 34 | tail->next = newNode; 35 | newNode->prev = tail; 36 | tail = newNode; 37 | } 38 | } 39 | 40 | return head; 41 | } 42 | 43 | void insertAtPosition(Node*& head, int data, int position) { 44 | Node* newNode = new Node(data); 45 | if (position == 1) { 46 | newNode->next = head; 47 | if (head != nullptr) { 48 | head->prev = newNode; 49 | } 50 | head = newNode; 51 | return; 52 | } 53 | 54 | Node* temp = head; 55 | int count = 1; 56 | while(temp->next != nullptr && count < position - 1) { 57 | temp=temp->next; 58 | } 59 | if (temp->next != nullptr) { 60 | newNode->next = temp->next; 61 | temp->next->prev = newNode; 62 | } 63 | temp->next = newNode; 64 | newNode->prev = temp; 65 | } 66 | 67 | 68 | void printList(Node* head) { 69 | Node* temp = head; 70 | while (temp != nullptr) { 71 | cout << temp->data << " "; 72 | temp = temp->next; 73 | } 74 | cout << endl; 75 | } 76 | 77 | // Ques 2: Write C++ program that detect the cycle inside a given linkedlist if their is any cycle present then print true otherwise print false. 78 | bool isCyclic(Node* head) { 79 | Node* slow = head; 80 | Node* fast = head; 81 | while(fast!=nullptr && fast->next != nullptr) { 82 | slow = slow->next; 83 | fast = fast->next->next; 84 | if(slow == fast) return true; 85 | } 86 | return false; 87 | } 88 | 89 | 90 | int main() 91 | { 92 | Node* head = createDoublyLinkedList(); 93 | printList(head); 94 | cout << isCyclic(head); 95 | return 0; 96 | } -------------------------------------------------------------------------------- /day29.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Ques 1. You are required to create a basic system to simulate different types of musical instruments using polymorphism. Implement a base class Instrument and two derived classes Piano and Violin. Each instrument can be played in different ways but should share a common interface. 5 | 6 | // Requirements: 7 | 8 | // Base Class: Instrument 9 | // Methods: 10 | // void playSound(): A pure virtual method that will be overridden in derived classes to define how the instrument produces sound. 11 | // void display(): A virtual method to display the instrument type. This can be overridden in derived classes. 12 | // Derived Class: Piano (inherits from Instrument) 13 | // Methods: 14 | // void playSound(): Override the method to print "Playing a melodious piano tune." 15 | // void display(): Override the method to print "This is a Piano." 16 | // Derived Class: Violin (inherits from Instrument) 17 | // Methods: 18 | // void playSound(): Override the method to print "Playing a classical violin tune." 19 | // void display(): Override the method to print "This is a Violin." 20 | // Main Functionality: 21 | // Create an array of pointers to Instrument objects. 22 | // Dynamically allocate Piano and Violin objects and store them in the array. 23 | // Use polymorphism to call playSound() and display() methods for each object through the base class pointer. 24 | // Ensure proper memory management by deallocating all dynamically allocated objects at the end. 25 | 26 | class Instrument { 27 | public: 28 | virtual void playSound() = 0; 29 | virtual void display() { 30 | cout << "This is an instrument." << endl; 31 | } 32 | virtual ~Instrument() {} 33 | }; 34 | 35 | class Piano : public Instrument { 36 | public: 37 | void playSound() override { 38 | cout << "Playing a melodious piano tune." << endl; 39 | } 40 | void display() override { 41 | cout << "This is a Piano." << endl; 42 | } 43 | }; 44 | 45 | class Violin : public Instrument { 46 | public: 47 | void playSound() override { 48 | cout << "Playing a classical violin tune." << endl; 49 | } 50 | void display() override { 51 | cout << "This is a Violin." << endl; 52 | } 53 | }; 54 | 55 | int main() { 56 | Instrument* instruments[2]; 57 | instruments[0] = new Piano(); 58 | instruments[1] = new Violin(); 59 | 60 | for (int i = 0; i < 2; ++i) { 61 | instruments[i]->display(); 62 | instruments[i]->playSound(); 63 | } 64 | 65 | for (int i = 0; i < 2; ++i) { 66 | delete instruments[i]; 67 | } 68 | 69 | return 0; 70 | } 71 | -------------------------------------------------------------------------------- /day02.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int findLargestNumber(int num1, int num2, int num3) { 5 | if (num1 >= num2 && num1 >= num3) { 6 | return num1; 7 | } else if (num2 >= num1 && num2 >= num3) { 8 | return num2; 9 | } else { 10 | return num3; 11 | } 12 | } 13 | void findGrade(int score) { 14 | if (score >= 90 && score <= 100) { 15 | cout << "Grade: A" << endl; 16 | } else if (score >= 80 && score <= 89) { 17 | cout << "Grade: B" << endl; 18 | } else if (score >= 70 && score <= 79) { 19 | cout << "Grade: C" << endl; 20 | } else if (score >= 60 && score <= 69) { 21 | cout << "Grade: D" << endl; 22 | } else if (score >= 0 && score <= 59) { 23 | cout << "Grade: F" << endl; 24 | } else { 25 | cout << "Invalid score" << endl; 26 | } 27 | } 28 | 29 | void findMonth(int month) { 30 | switch (month) { 31 | case 1: 32 | cout << "The month is: January" << endl; 33 | break; 34 | case 2: 35 | cout << "The month is: February" << endl; 36 | break; 37 | case 3: 38 | cout << "The month is: March" << endl; 39 | break; 40 | case 4: 41 | cout << "The month is: April" << endl; 42 | break; 43 | case 5: 44 | cout << "The month is: May" << endl; 45 | break; 46 | case 6: 47 | cout << "The month is: June" << endl; 48 | break; 49 | case 7: 50 | cout << "The month is: July" << endl; 51 | break; 52 | case 8: 53 | cout << "The month is: August" << endl; 54 | break; 55 | case 9: 56 | cout << "The month is: September" << endl; 57 | break; 58 | case 10: 59 | cout << "The month is: October" << endl; 60 | break; 61 | case 11: 62 | cout << "The month is: November" << endl; 63 | break; 64 | case 12: 65 | cout << "The month is: December" << endl; 66 | break; 67 | default: 68 | cout << "Invalid input" << endl; 69 | break; 70 | } 71 | } 72 | 73 | int main() { 74 | int num1, num2, num3; 75 | 76 | cout << "Enter the first number: "; 77 | cin >> num1; 78 | 79 | cout << "Enter the second number: "; 80 | cin >> num2; 81 | 82 | cout << "Enter the third number: "; 83 | cin >> num3; 84 | 85 | int largest = findLargestNumber(num1, num2, num3); 86 | cout << "The largest number is: " << largest << endl; 87 | 88 | 89 | int score; 90 | cout << "Enter the score: "; 91 | cin >> score; 92 | findGrade(score); 93 | 94 | 95 | int month; 96 | cout << "Enter a number: "; 97 | cin >> month; 98 | findMonth(month); 99 | 100 | return 0; 101 | } -------------------------------------------------------------------------------- /day30.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | // Ques 1. Write C++ program that take an array of size n from the user and the array contains both positive and negative number. Your task is to calculate the maximum sum of subarray. Print the maximum sum value. 9 | // Input : - n=9 array =-2,1,-3,4,-1,2,1,-5,4 10 | // Output:- 6 11 | // Explanation The subarray [4,-1,2,1] has the largest sum 6. 12 | 13 | int maxSubArray(const vector& nums) { 14 | int maxSum = nums[0]; 15 | int currentSum = 0; 16 | 17 | for (int i = 0; i < nums.size(); ++i) { 18 | currentSum += nums[i]; 19 | if (currentSum > maxSum) { 20 | maxSum = currentSum; 21 | } 22 | if (currentSum < 0) { 23 | currentSum = 0; 24 | } 25 | } 26 | 27 | return maxSum; 28 | } 29 | 30 | // Ques 2. In an array, the next greater element for a given element x is defined as the first element to the right of x that is larger than x. 31 | 32 | // You are provided with two distinct 0-indexed integer arrays, nums1 and nums2, where nums1 is a subset of nums2. 33 | 34 | // For each element in nums1, find its corresponding position in nums2, and then identify the next greater element that appears to the right of it in nums2. If no such element exists, return -1 for that element. 35 | 36 | // Your task is to return an array ans, where each element ans[i] represents the next greater element for nums1[i] as found in nums2. If no next greater element is found, the corresponding entry in ans should be -1. Note all the required input from the user. 37 | // Sample :- n1=3 , n2 =4 nums1 = [4,1,2], nums2 = [1,3,4,2] 38 | // Output :- [-1,3,-1]. 39 | // Explanation :- The next greater element for each value of nums1 is as follows: 40 | // - 4 is underlined in nums2 = [1,3,4,2]. There is no next greater element, so the answer is -1. 41 | // - 1 is underlined in nums2 = [1,3,4,2]. The next greater element is 3. 42 | // - 2 is underlined in nums2 = [1,3,4,2]. There is no next greater element, so the answer is -1. 43 | 44 | 45 | vector nextGreaterElement(const vector& nums1, const vector& nums2) { 46 | unordered_map greaterMap; 47 | stack s; 48 | 49 | for (int i = 0; i < nums2.size(); ++i) { 50 | while (!s.empty() && nums2[i] > s.top()) { 51 | greaterMap[s.top()] = nums2[i]; 52 | s.pop(); 53 | } 54 | s.push(nums2[i]); 55 | } 56 | 57 | vector result; 58 | for (int num : nums1) { 59 | result.push_back(greaterMap.count(num) ? greaterMap[num] : -1); 60 | } 61 | 62 | return result; 63 | } 64 | 65 | 66 | int main() { 67 | // q1 68 | vector nums = {-2, 1, -3, 4, -1, 2, 1, -5, 4}; 69 | cout << "Maximum sum of subarray: " << maxSubArray(nums) << endl; 70 | 71 | // q2 72 | vector nums1 = {4, 1, 2}; 73 | vector nums2 = {1, 3, 4, 2}; 74 | 75 | vector result = nextGreaterElement(nums1, nums2); 76 | for (int val : result) { 77 | cout << val << " "; 78 | } 79 | cout << endl; 80 | 81 | return 0; 82 | } 83 | -------------------------------------------------------------------------------- /day28.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Ques 1. You are tasked with creating a simple vehicle system in C++ using object-oriented programming principles. Implement a base class Vehicle and two derived classes Car and Bike. Each vehicle can perform certain common actions, but they also have specific attributes and methods. 5 | 6 | // Requirements: 7 | 8 | // Base Class: Vehicle 9 | // Attributes: 10 | // string make: The manufacturer of the vehicle. 11 | // int year: The year the vehicle was manufactured. 12 | // float fuel: The amount of fuel left in the vehicle. 13 | // Methods: 14 | // void refuel(float amount): Increases the fuel by amount. 15 | // void display(): A virtual method that displays the vehicle's details. 16 | // Derived Class: Car (inherits from Vehicle) 17 | // Attributes: 18 | // int doors: Number of doors in the car. 19 | // Methods: 20 | // void display(): Override the display() method to show car-specific details, including the number of doors. 21 | // void openDoors(): A method that prints "Doors are opened." 22 | // Derived Class: Bike (inherits from Vehicle) 23 | // Attributes: 24 | // bool hasCarrier: Indicates whether the bike has a carrier. 25 | // Methods: 26 | // void display(): Override the display() method to show bike-specific details, including whether it has a carrier. 27 | // void kickStart(): A method that prints "Bike kick-started." 28 | // Main Functionality: 29 | // Create objects of Car and Bike with relevant attributes. 30 | // Refuel both vehicles and display their details. 31 | // Call their specific methods (openDoors() for Car and kickStart() for Bike). 32 | 33 | class Vehicle { 34 | protected: 35 | string make; 36 | int year; 37 | float fuel; 38 | 39 | public: 40 | Vehicle() {}; 41 | Vehicle(string m, int y, float f) : make(m), year(y), fuel(f) {}; 42 | 43 | void refuel(float amount) { 44 | if(amount <= 0) { 45 | cout << "Invalid"; 46 | return; 47 | } 48 | cout << "Fuel level increased from " << fuel << " to " << fuel + amount << '\n'; 49 | fuel += amount; 50 | } 51 | 52 | virtual void display() = 0; 53 | 54 | }; 55 | 56 | class Car : public Vehicle { 57 | int doors; 58 | 59 | public: 60 | Car() {}; 61 | Car(string m, int y, float f, int d) : Vehicle(m,y,f), doors(d) {}; 62 | 63 | void display() override { 64 | cout << "Car's Make: " << make << '\n'; 65 | cout << "Manufactured in: " << year << '\n'; 66 | cout << "Number of doors: " << doors << '\n'; 67 | } 68 | 69 | void openDoors() { 70 | cout << "Doors are opened.\n"; 71 | } 72 | 73 | }; 74 | 75 | class Bike : public Vehicle { 76 | bool hasCarrier; 77 | 78 | public: 79 | Bike() {}; 80 | Bike(string m, int y, float f, bool hc) : Vehicle(m,y,f), hasCarrier(hc) {}; 81 | void display() override { 82 | cout << "Bike's Make: " << make << '\n'; 83 | cout << "Manufactured in: " << year << '\n'; 84 | cout << "Bike has carrier " << hasCarrier << '\n'; 85 | } 86 | 87 | void kickStart() { 88 | cout << "Bike kick-started.\n"; 89 | } 90 | 91 | }; 92 | 93 | int main() 94 | { 95 | Car Tata("Tata", 1990, 10, 4); 96 | Bike Hero("Hero", 2003, 20, true); 97 | 98 | Tata.display(); 99 | Tata.openDoors(); 100 | Tata.refuel(5); 101 | 102 | Hero.display(); 103 | Hero.kickStart(); 104 | Hero.refuel(8); 105 | 106 | return 0; 107 | } --------------------------------------------------------------------------------