One-dimensional arrays help you store many values of the same type in a single list. This makes it easy to organize and access data using a simple number called an index.
One-dimensional arrays in C++
class OneDimensionalArray { public: int size; int* elements; OneDimensionalArray(int array_size) { size = array_size; elements = new int[size]; } ~OneDimensionalArray() { delete[] elements; } void setElement(int index, int value) { if (index >= 0 && index < size) { elements[index] = value; } } int getElement(int index) { if (index >= 0 && index < size) { return elements[index]; } return -1; // Return -1 if index is invalid } void printArray() { for (int i = 0; i < size; i++) { std::cout << elements[i] << " "; } std::cout << std::endl; } };
The array size must be known when creating the array.
Indexing starts at 0, so the first element is at index 0.
OneDimensionalArray arr(5); arr.setElement(0, 10); arr.setElement(4, 50); arr.printArray();
OneDimensionalArray emptyArray(0);
emptyArray.printArray();OneDimensionalArray singleElementArray(1); singleElementArray.setElement(0, 99); singleElementArray.printArray();
OneDimensionalArray arr(3); arr.setElement(-1, 10); // ignored arr.setElement(3, 20); // ignored arr.printArray();
This program creates a one-dimensional array of size 5, initializes all elements to 0, then sets values at each index. It prints the array before and after setting values. It also shows how to get elements safely, returning -1 for invalid indexes.
#include <iostream> class OneDimensionalArray { public: int size; int* elements; OneDimensionalArray(int array_size) { size = array_size; elements = new int[size]; for (int i = 0; i < size; i++) { elements[i] = 0; // Initialize all elements to 0 } } ~OneDimensionalArray() { delete[] elements; } void setElement(int index, int value) { if (index >= 0 && index < size) { elements[index] = value; } } int getElement(int index) { if (index >= 0 && index < size) { return elements[index]; } return -1; // Return -1 if index is invalid } void printArray() { for (int i = 0; i < size; i++) { std::cout << elements[i] << " "; } std::cout << std::endl; } }; int main() { std::cout << "Creating array of size 5..." << std::endl; OneDimensionalArray numbers(5); std::cout << "Initial array: "; numbers.printArray(); std::cout << "Setting values..." << std::endl; numbers.setElement(0, 10); numbers.setElement(1, 20); numbers.setElement(2, 30); numbers.setElement(3, 40); numbers.setElement(4, 50); std::cout << "Array after setting values: "; numbers.printArray(); std::cout << "Trying to get element at index 2: "; std::cout << numbers.getElement(2) << std::endl; std::cout << "Trying to get element at invalid index 10: "; std::cout << numbers.getElement(10) << std::endl; return 0; }
Time complexity for accessing or setting an element by index is O(1) because you go directly to the position.
Space complexity is O(n), where n is the size of the array.
Common mistake: Accessing indexes outside the array size causes errors or undefined behavior, so always check index bounds.
Use one-dimensional arrays when you have a fixed number of elements and want fast access by position.
One-dimensional arrays store multiple values of the same type in a list accessed by index.
Indexes start at 0 and go up to size-1.
They provide fast access and are useful for simple collections of data.
Practice
Solution
Step 1: Understand array indexing in C++
Arrays in C++ always start indexing from 0, meaning the first element is at index 0.Step 2: Confirm the index range
Indexes go from 0 up to size-1, so the first index is always 0 regardless of array size.Final Answer:
0 -> Option CQuick Check:
Array indexing starts at 0 [OK]
- Thinking arrays start at 1
- Assuming negative indexes are valid
- Confusing with other languages
Solution
Step 1: Recall C++ array declaration syntax
In C++, a fixed-size array is declared using square brackets with size: int arr[5];Step 2: Check other options for correctness
int arr(5); uses parentheses which is invalid for arrays. array<int> arr(5); uses incorrect syntax (not standard array). int arr = new int[5]; uses dynamic allocation syntax but is incorrect without pointer.Final Answer:
int arr[5]; -> Option DQuick Check:
Fixed-size arrays use square brackets [OK]
- Using parentheses instead of brackets
- Confusing with vector syntax
- Incorrect dynamic allocation syntax
int arr[4] = {10, 20, 30, 40};
std::cout << arr[2];Solution
Step 1: Identify the array elements and indexes
The array arr has elements: arr[0]=10, arr[1]=20, arr[2]=30, arr[3]=40.Step 2: Access the element at index 2
arr[2] is 30, so std::cout will print 30.Final Answer:
30 -> Option AQuick Check:
arr[2] = 30 [OK]
- Confusing index 2 with 1 or 3
- Expecting output 20 or 40
- Thinking code causes error
int arr[3] = {1, 2, 3};
for(int i = 0; i <= 3; i++) {
std::cout << arr[i] << " ";
}Solution
Step 1: Analyze the loop range and array size
The array has size 3 with valid indexes 0,1,2. The loop runs from i=0 to i=3 inclusive, which means 4 iterations.Step 2: Identify out-of-bounds access
When i=3, arr[3] is accessed, which is outside the array bounds and causes undefined behavior.Final Answer:
Loop runs one time too many, causing out-of-bounds access -> Option BQuick Check:
Loop index must be less than array size [OK]
- Using <= instead of <
- Assuming array size is 4
- Ignoring out-of-bounds errors
arr of size n, which code snippet correctly calculates the sum of all elements?Solution
Step 1: Understand correct loop bounds for array traversal
Array indexes go from 0 to n-1, so loop must run i from 0 to less than n.Step 2: Check each option for correctness
int sum = 0; for(int i = 1; i < n; i++) sum += arr[i]; starts from 1, missing arr[0]. int sum = 0; for(int i = 0; i <= n; i++) sum += arr[i]; uses <= n, causing out-of-bounds access. int sum = 0; for(int i = 0; i < n; i++) sum += arr[i]; correctly sums all elements from 0 to n-1. int sum = arr[0]; for(int i = 1; i < n; i++) sum = arr[i]; overwrites sum instead of adding.Final Answer:
int sum = 0; for(int i = 0; i < n; i++) sum += arr[i]; -> Option AQuick Check:
Loop from 0 to n-1 sums all elements [OK]
- Starting loop at 1
- Using <= n causing errors
- Overwriting sum instead of adding
Ready to prove it?
Pass the test and this pattern moves unknown
