Show Menu
Cheatography

Module 3-Arrays and Vectors Cheat Sheet (DRAFT) by

This module introduces arrays and vectors in C++, which are essential data structures for storing and managing collections of data. You will learn how to declare, initialize, access, modify, and manipulate arrays and vectors efficiently.

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

Data Structures

Elementary or Scalar
Built-in data types supported by hardware
int, float, double, char
Structure
Groups multiple scalars, accessed via references
arrays, records, structs
Abstract
Develo­per­-de­fined data types
enumer­ations, type declar­ations
Arrays
a finite, ordered collection of homoge­neous elements, each accessed through a subscript value
Base Type
type of elements or components of the array
Index
type of the values used to access the individual elements of the array

Structures

Pointers

Function

Vectors

Arrays Ex

#include <iostream>
using namespace std;
int main() {
    int arr[] = {10, 20, 30, 40, 50};
    for (int i = 0; i < 5; i++) {
        cout << "Element at index " << i << ": " << arr[i] << endl;
    }
    return 0;
}

Functions Ex

#include <iostream>
using namespace std;
int add(int a, int b) {
    return a + b;
}
int main() {
    int result = add(5, 7);
    cout << "Sum: " << result;
    return 0;
}
 

Pointers

Pointers
data type that “points” to another value stored in memory
datatype *varia­ble­_name;
Reference Pointer
returns the variable’s address
pointe­r_v­ariable = &v­ari­able;
Derefence Pointer
returns the value stored in a memory address
pointe­r_v­ariable = &v­ari­able;

Arrays

Basic Operation

Extraction
X = arr[3];  // Gets the 4th element
Storing
arr[4] = 100;  // Stores 100 at index 4
Copying
for (int i = 0; i < size; i++) { 
newArr[i] = oldArr[i]; }

Sorting Algorithm

for (int i = 0; i < size - 1; i++) {
    int minIndex = i;
    for (int j = i + 1; j < size; j++) {
        if (arr[j] < arr[minIndex]) {
            minIndex = j;
        }
    }
    swap(arr[i], arr[minIndex]);
}

Bubble Sort

for (int i = 0; i < size - 1; i++) {
    for (int j = 0; j < size - i - 1; j++) {
        if (arr[j] > arr[j + 1]) {
            swap(arr[j], arr[j + 1]);
        }
    }
}

Vectors Ex

#include <iostream>
#include <vector>
using namespace std;
int main() {
    vector<int> v = {10, 20, 30};
    v.push_back(40);
    for (int I = 0; I < v.size(); i++) {
        cout << “Vector element at index “ << I << “: “ << v[i] << endl;
    }
    return 0;
}

Structures Ex

#include <iostream>
using namespace std;
struct Student {
    char name[50];
    int age;
    float grade;
};
int main() {
    Student s1 = {"John", 20, 89.5};
    cout << "Name: " << s1.name << " Age: " << s1.age << " Grade: " << s1.grade;
    return 0;
}