Arrays help us store many values in one place. Common operations let us add, find, or change these values easily.
Common array operations in C++
#include <iostream> using namespace std; class Array { public: int* data; int size; int capacity; Array(int capacity) { this->capacity = capacity; size = 0; data = new int[capacity]; } ~Array() { delete[] data; } void add(int value) { if (size < capacity) { data[size] = value; size++; } else { cout << "Array is full!" << endl; } } int find(int value) { for (int i = 0; i < size; i++) { if (data[i] == value) { return i; } } return -1; // not found } void update(int index, int newValue) { if (index >= 0 && index < size) { data[index] = newValue; } else { cout << "Index out of range!" << endl; } } void print() { for (int i = 0; i < size; i++) { cout << data[i] << " "; } cout << endl; } };
This class creates a simple array with fixed capacity.
Operations include adding, finding, updating, and printing values.
Array numbers(5); numbers.add(10); numbers.add(20); numbers.print();
Array emptyArray(3); emptyArray.print();
Array singleElement(2); singleElement.add(5); singleElement.update(0, 15); singleElement.print();
Array numbers(3); numbers.add(1); numbers.add(2); numbers.add(3); numbers.add(4);
This program creates an array with capacity 5. It adds three numbers, finds one, updates one, tries to add more than capacity, and prints the array at each step.
#include <iostream> using namespace std; class Array { public: int* data; int size; int capacity; Array(int capacity) { this->capacity = capacity; size = 0; data = new int[capacity]; } ~Array() { delete[] data; } void add(int value) { if (size < capacity) { data[size] = value; size++; } else { cout << "Array is full!" << endl; } } int find(int value) { for (int i = 0; i < size; i++) { if (data[i] == value) { return i; } } return -1; } void update(int index, int newValue) { if (index >= 0 && index < size) { data[index] = newValue; } else { cout << "Index out of range!" << endl; } } void print() { for (int i = 0; i < size; i++) { cout << data[i] << " "; } cout << endl; } }; int main() { Array numbers(5); cout << "Initial array: "; numbers.print(); numbers.add(10); numbers.add(20); numbers.add(30); cout << "After adding 3 elements: "; numbers.print(); int index = numbers.find(20); cout << "Index of 20: " << index << endl; numbers.update(1, 25); cout << "After updating index 1 to 25: "; numbers.print(); numbers.add(40); numbers.add(50); numbers.add(60); // should print full message cout << "Final array: "; numbers.print(); return 0; }
Adding an element is O(1) if space is available.
Finding an element is O(n) because it checks each item.
Updating an element is O(1) if index is valid.
Common mistake: adding beyond capacity without resizing causes errors.
Use dynamic arrays or vectors if you need flexible size.
Arrays store multiple values in order.
Common operations: add, find, update, print.
Always check array size before adding new elements.
Practice
int arr[5] = {1, 2, 3, 4, 5};Solution
Step 1: Understand array declaration
The code declares an integer array named arr with size 5.Step 2: Check initialization
The array is initialized with values 1, 2, 3, 4, 5 in order.Final Answer:
Creates an array of 5 integers with values 1 to 5 -> Option AQuick Check:
Array declaration with initialization = Creates array with values [OK]
- Confusing array with vector
- Thinking it's a pointer declaration
- Assuming array is uninitialized
arr in C++?Solution
Step 1: Recall zero-based indexing
Arrays in C++ start at index 0, so the third element is at index 2.Step 2: Identify correct syntax for access
Array elements are accessed using square brackets, so arr[2] is correct.Final Answer:
arr[2]; -> Option DQuick Check:
Third element index = 2, use square brackets [OK]
- Using parentheses instead of brackets
- Using 1-based indexing
- Using curly braces for access
int arr[4] = {10, 20, 30, 40};
int sum = 0;
for(int i = 0; i < 4; i++) {
sum += arr[i];
}
std::cout << sum;Solution
Step 1: Identify array elements and loop
The array has elements 10, 20, 30, 40 and the loop runs from i=0 to 3.Step 2: Calculate sum of elements
sum = 10 + 20 + 30 + 40 = 100Final Answer:
100 -> Option AQuick Check:
Sum of all elements = 100 [OK]
- Stopping loop early
- Using wrong indices
- Printing sum before loop
int arr[3] = {5, 10, 15};
for(int i = 1; i <= 3; i++) {
std::cout << arr[i] << " ";
}Solution
Step 1: Analyze loop indices
The loop runs i from 1 to 3 inclusive, so i=3 is included.Step 2: Check array bounds
Array indices are 0,1,2. Accessing arr[3] is out-of-bounds and causes error.Final Answer:
Using <= causes out-of-bounds access -> Option CQuick Check:
Loop must use i < size, not <= [OK]
- Starting loop at 1 instead of 0 (not error but unusual)
- Ignoring out-of-bounds access
- Assuming array size is wrong
int arr[6] = {2, 4, 6, 8, 10, 12};. How do you update the element with value 8 to 9 using a loop?Solution
Step 1: Understand the problem
We want to find the element with value 8 and update it to 9 using a loop.Step 2: Check loop and condition
Loop from i=0 to 5 checks each element; if arr[i] == 8, assign arr[i] = 9.Final Answer:
for(int i=0; i<6; i++) { if(arr[i] == 8) arr[i] = 9; } -> Option BQuick Check:
Loop with condition to find and update element [OK]
- Using wrong indices in loop
- Assigning wrong array index
- Not using loop to find value
Ready to prove it?
Pass the test and this pattern moves unknown
