Skip to content

Instantly share code, notes, and snippets.

@eeriemyxi
Last active September 18, 2024 13:37
Show Gist options
  • Select an option

  • Save eeriemyxi/8028b94ca42f919573123ee8836fef53 to your computer and use it in GitHub Desktop.

Select an option

Save eeriemyxi/8028b94ca42f919573123ee8836fef53 to your computer and use it in GitHub Desktop.

Revisions

  1. eeriemyxi revised this gist Sep 18, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cs-prac.md
    Original file line number Diff line number Diff line change
    @@ -226,7 +226,7 @@ public:
    if (topIndex >= 0) {
    return arr[topIndex];
    }
    return -1; // Return -1 if stack is empty
    return -1;
    }
    };
    ```
  2. eeriemyxi created this gist Sep 18, 2024.
    492 changes: 492 additions & 0 deletions cs-prac.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,492 @@
    Merge two sorted one-dimensional arrays into a single sorted array.
    ```cpp
    #include <iostream>
    #include <vector>
    using namespace std;

    vector<int> mergeSortedArrays(const vector<int>& arr1, const vector<int>& arr2) {
    vector<int> result;
    int i = 0, j = 0;
    while (i < arr1.size() && j < arr2.size()) {
    if (arr1[i] < arr2[j]) result.push_back(arr1[i++]);
    else result.push_back(arr2[j++]);
    }
    while (i < arr1.size()) result.push_back(arr1[i++]);
    while (j < arr2.size()) result.push_back(arr2[j++]);
    return result;
    }

    int main() {
    vector<int> arr1 = {1, 3, 5};
    vector<int> arr2 = {2, 4, 6};
    vector<int> merged = mergeSortedArrays(arr1, arr2);
    for (int num : merged) cout << num << " ";
    }
    ```
    Insert an element at a specific position in a two-dimensional array and shift the subsequent elements.
    ```cpp
    #include <iostream>
    #include <vector>
    using namespace std;
    void insertElement(vector<vector<int>>& arr, int row, int col, int value) {
    arr[row].insert(arr[row].begin() + col, value);
    for (int i = row; i < arr.size(); i++) {
    for (int j = (i == row ? col + 1 : 0); j < arr[i].size(); j++) {
    int temp = arr[i][j];
    arr[i][j] = value;
    value = temp;
    }
    }
    }
    int main() {
    vector<vector<int>> arr = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    insertElement(arr, 1, 1, 10);
    for (auto& row : arr) {
    for (int val : row)
    cout << val << " ";
    cout << endl;
    }
    return 0;
    }
    ```
    Delete an element from a one-dimensional array and shift the remaining elements to fill the gap.
    ```cpp
    #include <iostream>
    using namespace std;

    void deleteElement(int arr[], int& size, int index) {
    for (int i = index; i < size - 1; i++)
    arr[i] = arr[i + 1];
    size--;
    }

    int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int size = 5;
    deleteElement(arr, size, 2);
    for (int i = 0; i < size; i++)
    cout << arr[i] << " ";
    return 0;
    }
    ```
    Create a class Circle with a private data member radius and a public method to calculate the area using getArea() in C++.
    ```cpp
    #include <iostream>
    #include <cmath>
    using namespace std;
    class Circle {
    private:
    double radius;
    public:
    Circle(double r) : radius(r) {}
    double getArea() {
    return M_PI * radius * radius;
    }
    };
    int main() {
    Circle c(5.0);
    cout << "Area: " << c.getArea() << endl;
    return 0;
    }
    ```
    Define a class Student with public attributes name and age, and a method display() that prints these details.
    ```cpp
    #include <iostream>
    using namespace std;

    class Student {
    public:
    string name;
    int age;

    void display() {
    cout << "Name: " << name << ", Age: " << age << endl;
    }
    };

    int main() {
    Student s;
    s.name = "John";
    s.age = 20;
    s.display();
    return 0;
    }
    ```
    Implement a class Rectangle in C++ that includes private members length and width, and a method getPerimeter() that returns the perimeter.
    ```cpp
    #include <iostream>
    using namespace std;

    class Rectangle {
    private:
    double length, width;

    public:
    Rectangle(double l, double w) : length(l), width(w) {}

    double getPerimeter() {
    return 2 * (length + width);
    }
    };

    int main() {
    Rectangle rect(5.0, 3.0);
    cout << "Perimeter: " << rect.getPerimeter() << endl;
    return 0;
    }
    ```
    Write a class BankAccount with private data members accountNumber and balance, and a public method deposit(double amount) that updates the balance.
    ```cpp
    #include <iostream>
    using namespace std;

    class BankAccount {
    private:
    int accountNumber;
    double balance;

    public:
    BankAccount(int accNum, double bal) : accountNumber(accNum), balance(bal) {}

    void deposit(double amount) {
    balance += amount;
    }

    double getBalance() {
    return balance;
    }
    };

    int main() {
    BankAccount account(12345, 1000.0);
    account.deposit(500.0);
    cout << "Balance: " << account.getBalance() << endl;
    return 0;
    }
    ```
    Design a class Book with a constructor that initializes title and author, and a public method printDetails() to display the book information.
    ```cpp
    #include <iostream>
    using namespace std;

    class Book {
    string title, author;
    public:
    Book(string t, string a) : title(t), author(a) {}
    void printDetails() {
    cout << "Title: " << title << ", Author: " << author << endl;
    }
    };

    int main() {
    Book book("1984", "George Orwell");
    book.printDetails();
    return 0;
    }
    ```
    Implement a stack using a linear array in C++ with methods for push, pop, and top operations.
    ```cpp
    #include <iostream>
    using namespace std;
    class Stack {
    private:
    int* arr;
    int topIndex;
    int capacity;
    public:
    Stack(int size) : capacity(size), topIndex(-1) {
    arr = new int[capacity];
    }
    ~Stack() {
    delete[] arr;
    }
    void push(int value) {
    if (topIndex < capacity - 1) {
    arr[++topIndex] = value;
    }
    }
    void pop() {
    if (topIndex >= 0) {
    --topIndex;
    }
    }
    int top() {
    if (topIndex >= 0) {
    return arr[topIndex];
    }
    return -1; // Return -1 if stack is empty
    }
    };
    ```
    Create a stack using a circular array in C++ and handle overflow and underflow conditions.
    ```cpp
    #include <iostream>
    using namespace std;

    class CircularStack {
    private:
    int* arr;
    int top;
    int capacity;
    int size;

    public:
    CircularStack(int cap) : capacity(cap), size(0), top(-1) {
    arr = new int[capacity];
    }

    ~CircularStack() {
    delete[] arr;
    }

    bool isFull() {
    return size == capacity;
    }

    bool isEmpty() {
    return size == 0;
    }

    void push(int value) {
    if (isFull()) {
    cout << "Overflow\n";
    return;
    }
    top = (top + 1) % capacity;
    arr[top] = value;
    size++;
    }

    int pop() {
    if (isEmpty()) {
    cout << "Underflow\n";
    return -1;
    }
    int value = arr[top];
    top = (top - 1 + capacity) % capacity;
    size--;
    return value;
    }

    int peek() {
    if (isEmpty()) {
    cout << "Stack is empty\n";
    return -1;
    }
    return arr[top];
    }
    };
    ```
    Design a stack using a singly linked list in C++ with push and pop operations, ensuring you handle memory management correctly.
    ```cpp
    #include <iostream>
    using namespace std;

    class Node {
    public:
    int data;
    Node* next;
    Node(int val) : data(val), next(nullptr) {}
    };

    class Stack {
    Node* top;
    public:
    Stack() : top(nullptr) {}

    void push(int val) {
    Node* newNode = new Node(val);
    newNode->next = top;
    top = newNode;
    }

    void pop() {
    if (top) {
    Node* temp = top;
    top = top->next;
    delete temp;
    }
    }

    bool isEmpty() {
    return top == nullptr;
    }

    int peek() {
    if (top) return top->data;
    throw out_of_range("Stack is empty");
    }

    ~Stack() {
    while (top) {
    pop();
    }
    }
    };
    ```
    Write a C++ program to create a text file and write user input to it.
    ```cpp
    #include <iostream>
    #include <fstream>
    using namespace std;
    int main() {
    ofstream file("output.txt");
    if (file.is_open()) {
    string input;
    cout << "Enter text: ";
    getline(cin, input);
    file << input;
    file.close();
    }
    return 0;
    }
    ```
    Implement a C++ function to read the content of a binary file and display it as a string.
    ```cpp
    #include <iostream>
    #include <fstream>
    #include <string>

    using namespace std;

    string readBinaryFile(const string& filename) {
    ifstream file(filename, ios::binary | ios::ate);
    if (!file) return "";
    streamsize size = file.tellg();
    file.seekg(0, ios::beg);
    string buffer(size, '\0');
    file.read(&buffer[0], size);
    return buffer;
    }
    ```
    Write a C++ code snippet to append text to an existing file, ensuring the content is added to the end.
    ```cpp
    #include <fstream>
    using namespace std;
    int main() {
    ofstream file("example.txt", ios::app);
    file << "New text to append.\n";
    file.close();
    return 0;
    }
    ```
    Create a C++ program that reads a binary file and counts the number of bytes.
    ```cpp
    #include <iostream>
    #include <fstream>

    using namespace std;

    int main() {
    ifstream file("example.bin", ios::binary | ios::ate);
    if (file.is_open()) {
    streampos size = file.tellg();
    cout << "Number of bytes: " << size << endl;
    file.close();
    }
    return 0;
    }
    ```
    Develop a C++ function to update specific lines in a text file based on user input.
    ```cpp
    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include <vector>
    #include <string>

    using namespace std;

    void updateLineInFile(const string& filename, int lineNumber, const string& newContent) {
    ifstream fileIn(filename);
    if (!fileIn) return;
    vector<string> lines;
    string line;
    while (getline(fileIn, line)) lines.push_back(line);
    fileIn.close();
    if (lineNumber < 1 || lineNumber > lines.size()) return;
    lines[lineNumber - 1] = newContent;
    ofstream fileOut(filename);
    for (const auto& l : lines) fileOut << l << endl;
    }
    ```
    Write a program to find the sum of all even numbers from 1 to 100.
    ```cpp
    #include <iostream>
    using namespace std;
    int main() {
    int sum = 0;
    for (int i = 2; i <= 100; i += 2) {
    sum += i;
    }
    cout << sum;
    return 0;
    }
    ```
    Create a script that calculates the factorial of a number provided by the user.
    ```cpp
    #include <iostream>
    using namespace std;

    int factorial(int n) {
    if (n <= 1) return 1;
    return n * factorial(n - 1);
    }

    int main() {
    int num;
    cout << "Enter a number: ";
    cin >> num;
    cout << "Factorial of " << num << " is " << factorial(num) << endl;
    return 0;
    }
    ```
    Write a code snippet to find the largest element in an array.
    ```cpp
    #include <iostream>
    using namespace std;
    int main() {
    int arr[] = {5, 7, 2, 9, 3};
    int max = arr[0];
    for(int i = 1; i < sizeof(arr)/sizeof(arr[0]); i++) {
    if(arr[i] > max) max = arr[i];
    }
    cout << "Largest element: " << max << endl;
    return 0;
    }
    ```
    Develop a program to count the number of vowels in a given text.
    ```cpp
    #include <iostream>
    #include <string>
    using namespace std;

    int main() {
    string text;
    getline(cin, text);
    int count = 0;
    for (char c : text) {
    c = tolower(c);
    if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
    count++;
    }
    cout << count;
    return 0;
    }
    ```