Posts

Showing posts from February, 2025

Sorting DSA in c++

  Bubble Sort   #include <iostream> using namespace std;   void bubbleSort(int arr[], int n) {     for (int i = 0; i < n - 1; i++) {         for (int j = 0; j < n - i - 1; j++) {             if (arr[j] > arr[j + 1]) {                 swap(arr[j], arr[j + 1]);             }         }     } }   int main() {     int arr[] = {64, 34, 25, 12, 22, 11, 90};     int n = sizeof(arr) / sizeof(arr[0]);     bubbleSort(arr, n);     cout << "Sorted array: ";     for (int i = 0; i < n; i++) cout << arr[i] << " ";     return 0; } ...

C++ Exception Handling and file handling

  C++ Exceptions When executing C++ code, different errors can occur: coding errors made by the programmer, errors due to wrong input, or other unforeseeable things.   When an error occurs, C++ will normally stop and generate an error message. The technical term for this is: C++ will throw an exception (throw an error).   C++ try and catch Exception handling in C++ consist of three keywords: try, throw and catch:   The try statement allows you to define a block of code to be tested for errors while it is being executed.   The throw keyword throws an exception when a problem is detected, which lets us create a custom error.   The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.   The try and catch keywords come in pairs:     try {   // Block of code to try   throw exception; // Throw an exception when a problem arise } catch () { ...

Basic DSA programs in c++

1. Linear Search #include <iostream> using namespace std;   int linearSearch(int arr[], int size, int key) {     for (int i = 0; i < size; i++) {         if (arr[i] == key)             return i; // Return index if found     }     return -1; // Return -1 if not found }   int main() {     int arr[] = {10, 20, 30, 40, 50};     int size = sizeof(arr) / sizeof(arr[0]);     int key;       cout << "Enter the element to search: ";     cin >> key;       int result = linearSearch(arr, size, key);     if (result != -1)         cout << "Element found at index " << result << endl;     else   ...

programs of oops in c++

  Inheritance   Single level inheritance:   #include <iostream>   using namespace std;     class Account {      public:      int a=8;    int b=56;     };     class Programmer: public Account {               public:               void display(){             cout<<a+b;    }   };        int main() {       Programmer p1;       p1.display();     return 0;   }       #include <iostream>   using namespace std;   class A   {       int a = 4;       int b = 5;     ...