Arrays represent a fundamental data structure in C++ that stores collections of elements of the same type in contiguous memory locations. Each element can be accessed through its unique index, enabling efficient data manipulation and storage operations.
Array Fundamentals
A one-dimensional array organizes elements in a linear sequence. The key characteristics include:
- Homogeneous Elements: All elements must be of the same data type (int, float, char, etc.)
- Contiguous Memory: Elements are stored in consecutive memory locations, enabling fast access
- Fixed Size: Array size must be declared at compile time and cannot be changed dynamically
- Zero-Based Indexing: The first element is accessed at index 0, the last at index size-1
- Pointer Decay: Array names implicitly convert to pointers pointing to the first element
Array Definition
The basic syntax for declaring a one-dimensional array:
elementType arrayName[arraySize];
Example:
int numbers[50];
float temperatures[30];
char letters[26];
Initialization Methods
Static Initialization
Elements can be initialized during declaration:
int data[5] = {10, 20, 30, 40, 50};
When partial initialization occurs, remaining elements are set to zero:
int values[5] = {1, 2}; // Result: {1, 2, 0, 0, 0}
Individual element assignment:
int scores[10];
scores[0] = 95;
scores[1] = 87;
Dynamic Initialization
Using loops to populate arrays:
#include <iostream>
using namespace std;
int main() {
const int SIZE = 8;
int values[SIZE];
for (int i = 0; i < SIZE; i++) {
cin >> values[i];
}
return 0;
}
Output Operations
Forward Traversal
for (int i = 0; i < SIZE; i++) {
cout << values[i] << " ";
}
Reverse Traversal
for (int i = SIZE - 1; i >= 0; i--) {
cout << values[i] << " ";
}
Sorting Algorithms
Bubble Sort Implementation
Bubble sort repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order:
void bubbleSort(int arr[], int length) {
for (int i = 1; i < length; i++) {
for (int j = i + 1; j <= length; j++) {
if (arr[i] > arr[j]) {
swap(arr[i], arr[j]);
}
}
}
}
Time complexity is O(n²), making it suitable for small datasets.
Using Standard Library Sort
The <algorithm> header provides the sort function:
Ascending Order:
#include <algorithm>
sort(arrayName, arrayName + n + 1);
Descending Order:
sort(arrayName, arrayName + n + 1, greater<int>());
Element Insertion
Inserting at the Beginning
const int MAX = 100;
int arr[MAX] = {2, 4, 6, 8, 10};
int currentSize = 5;
int newValue = 1;
for (int i = currentSize; i > 0; i--) {
arr[i] = arr[i - 1];
}
arr[0] = newValue;
currentSize++;
Inserting at a Specific Position
int position = 3;
int insertValue = 5;
for (int i = currentSize; i > position; i--) {
arr[i] = arr[i - 1];
}
arr[position] = insertValue;
currentSize++;
Inserting at the End
int appendValue = 12;
arr[currentSize] = appendValue;
currentSize++;
Searching Techniques
Linear Search
int target;
int foundIndex = -1;
cin >> target;
for (int i = 0; i < n; i++) {
if (data[i] == target) {
foundIndex = i;
break;
}
}
Binary Search
Requires a sorted array. Works by repeatedly dividing the search interval in half.
Practical Applications
- Data Storage: Efficiently store and access large datasets
- Algorithm Implementation: Foundation for sorting, searching, and mathematical operations
- Statistical Analysis: Calculate sums, averages, min/max values
- Buffer Operations: Manage input/output buffers and temporary data storage
Example Problems
Problem 1: Find Maximum and Minimum
Input n numbers and output the maximum and minimum values (1 ≤ n ≤ 100).
#include <iostream>
#include <algorithm>
#include <climits>
using namespace std;
int main() {
const int MAX_SIZE = 100;
int n;
int elements[MAX_SIZE];
cin >> n;
for (int i = 0; i < n; i++) {
cin >> elements[i];
}
int minimum = INT_MAX;
int maximum = INT_MIN;
for (int i = 0; i < n; i++) {
minimum = min(minimum, elements[i]);
maximum = max(maximum, elements[i]);
}
cout << minimum << endl;
cout << maximum << endl;
return 0;
}
Problem 2: Caluclate Sum
Calculate the sum of n numbers (1 ≤ n ≤ 100).
#include <iostream>
using namespace std;
int main() {
const int MAX_SIZE = 100;
int n;
int elements[MAX_SIZE];
long long total = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> elements[i];
total += elements[i];
}
cout << "Sum: " << total << endl;
return 0;
}
Key Takeaways
One-dimensional arrays provide a powerful mechanism for organizing sequential data. Mastery of array operations—definition, initialization, traversal, sorting, insertion, and search—forms the cornerstone of effective C++ programming and serves as a prerequisite for understanding more complex data structures.