Show Menu
Cheatography

C++ (Weeks 6–9) Cheat Sheet (DRAFT) by

Source: ChatGPT. Covers: • functions • search algorithms • sorting algorithms • arrays • references • pointers • structures

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Functions

// Function declaration
return_type function_name(parameters);

// Function definition
return_type function_name(parameters) {
    // Function body
    // Code here
    return result; // Optional
}

Search Algorithms

Linear Search:
Linear search is a simple algorithm that scans through an array one element at a time, comparing each element with the target value. It continues this process until it finds the target or reaches the end of the array. Linear search is straig­htf­orward but not the most efficient for large datasets.
Binary Search:
Binary search works on a sorted array and follows a divide­-an­d-c­onquer approach. It starts with the middle element and compares it to the target. If they match, the search is succes­sful. If the target is smaller, it repeats the process on the left half of the array; if the target is larger, it looks in the right half. This process continues until the target is found or the search range becomes empty.
 

Sorting Algorithms

Bubble Sort: Bubble sort repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. It continues to do this until no more swaps are needed, indicating that the list is sorted. Bubble sort has poor perfor­mance for large lists and is mainly used for educat­ional purposes.
Selection Sort: Selection sort divides the list into a sorted and an unsorted region. It repeatedly selects the minimum element from the unsorted region and moves it to the end of the sorted region. The process continues until the entire list is sorted.

Arrays

// Declare an array
data_type array_name[size];

// Initialize an array
data_type array_name[] = {value1, value2, value3};

// Access elements
element = array_name[index];

// Modify elements
array_name[index] = new_value;

References

// Declare a reference
data_type& reference_name = original_variable;

// Use reference
reference_name = new_value; // Modifies the original variable
 

Pointers

// Declare a pointer
data_type* pointer_name;

// Initialize pointer
pointer_name = &variable;

// Dereference pointer
value = *pointer_name;

// Pointer arithmetic
pointer_name++; // Moves to the next element

// Dynamic memory allocation
data_type* dynamic_ptr = new data_type;
delete dynamic_ptr; // Release memory

Structures

// Declare a structure
struct StructName {
    data_type member1;
    data_type member2;
    // ...
};

// Create an instance
StructName instance_name;

// Access members
instance_name.member1 = value;

// Nested structures
struct NestedStruct {
    int inner_member;
};

struct OuterStruct {
    int outer_member;
    NestedStruct nested;
};