Posts

TRY AND CATCH FOR VOTING

 #include<iostream> using namespace std; int main() {     int age; cout<<"enter the age"<<"\n"; cin>>age; try {     if(age<=18)     throw "not eligible to vote";     cout<<"eligible to vote"; } catch(const char *ch) {     cout<<"age lesser"<<ch; } }

GLOBAL VARIABLES

 #include<iostream> using namespace std; int a,b; int multiply() {     a=198;     return 0; } int main() {     cout<<"BEFORE PROCESSING " ;     a=19;     cout<<a<<"\n";     multiply();     cout<<"AFTER PROCESSING "<<a;     return 0; }

SWITCH STATEMENTS

 #include<iostream> using namespace std; int main() {     int choice;     cout<<"enter the choice"<<"\n";     cin>>choice;     switch(choice)     {  case 1:     cout<<"1";     break;  case 2:     cout<<"2";     break;  default:      cout<<"unknown";     } }

PROGRAMS FOR TRY AND CATCH BLOCK

  TRY AND CATCH BLOCK WITH THE STRING July 09, 2024 TRY AND CATCH USING NUMBERS

SORTING PROGRAM

 #include <iostream> using namespace std; int main() {     int n[5], i, num_elements;     cout << "Enter the number of elements (up to 3): ";     cin >> num_elements;     if (num_elements > 3) {         cout << "Number of elements exceeds the limit of 3." << end;         return 1;     }     cout << "Enter " << num_elements << " elements:" << endl;     for (i = 0; i < num_elements; i++) {         cin >> n[i];     }     int temp, j;     for (i = 1; i < num_elements; i++) {         j = i;         while (j > 0 && n[j] < n[j - 1]) {             temp = n[j];             n[j] = n[j - 1];             n[j - 1] = temp;  ...

TREE ORDER TRAVERSAL

 #include <iostream> using namespace std; class Tree {     struct Node {         string data; // Changed 'type' to 'string' to match the data being inserted         Node* left;         Node* right;                  Node(string d) : data(d), left(nullptr), right(nullptr) {}     }; public:     Node* root = nullptr;     Tree() {         Node* f = new Node("F");         Node* g = new Node("G");         Node* h = new Node("H");         f->left = g;         f->right = h;         root = f; // Corrected 'Root' to 'root'     }     void preorder(Node* ptr) {         if (ptr != nullptr) {             cout << ptr->data << " ";   ...