Single-Dimensional
Dimensional Arrays are the simplest form of arrays in C++. They consist of a sequence of elements of the same data type, stored in contiguous memory locations. Each element in a single-dimensional array is accessed using a unique index, starting from zero.
1. Declaration and Initialization
- Declaration: You declare a single-dimensional array by specifying the data type, the array name, and the size (number of elements).
int myArray[5]; // Declares an array of 5 integers
Initialization: You can initialize the array at the time of declaration.
int myArray[5] = {1, 2, 3, 4, 5}; // Initializes the array with values
If fewer initializers are provided than the size of the array, the remaining elements are automatically initialized to zero.
int myArray[5] = {1, 2}; // Initializes to {1, 2, 0, 0, 0}
2. Accessing and Modifying Elements
- Accessing: You access elements using the index, which starts from zero.
int firstElement = myArray[0]; // Accesses the first element
int thirdElement = myArray[2]; // Accesses the third element
Modifying: You can modify elements by assigning new values to specific indices.
myArray[1] = 10; // Sets the second element to 10
Iterating Through Single-Dimensional Arrays
- Using Loops: You can use loops like for, while, or do-while to iterate over the elements of the array.
for (int i = 0; i < 5; i++) {
std::cout << myArray[i] << " ";
}
- This will output: 1 10 3 4 5.
4. Passing Single-Dimensional Arrays to Functions
- Passing by Reference: You can pass an array to a function by reference, allowing the function to modify the array elements.
void modifyArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
arr[i] *= 2;
}
}
int main() {
int myArray[5] = {1, 2, 3, 4, 5};
modifyArray(myArray, 5); // Array elements are modified
}
5. Common Operations on Single-Dimensional Arrays
- Finding the Length: The length of a static array can be found using sizeof in relation to the size of an element.
int length = sizeof(myArray) / sizeof(myArray[0]); // Calculates the number of elements
Searching: Implementing searching algorithms like linear search to find an element in the array.
int linearSearch(int arr[], int size, int target) {
for (int i = 0; i < size; i++) {
if (arr[i] == target) {
return i;
}
}
return -1; // Return -1 if the element is not found
}
- Sorting: Sorting the array using algorithms like bubble sort or quicksort.
void bubbleSort(int arr[], int size) {
for (int i = 0; i < size-1; i++) {
for (int j = 0; j < size-i-1; j++) {
if (arr[j] > arr[j+1]) {
std::swap(arr[j], arr[j+1]);
}
}
}
}
6. Memory Considerations
- Stack Memory Allocation: Single-dimensional arrays are typically allocated on the stack. The size of the array must be known at compile time, and large arrays can lead to stack overflow in systems with limited stack memory.
- Array Decay: When passed to a function, arrays decay into pointers, losing their size information. It’s crucial to pass the array size as an additional argument when passing arrays to functions.
7. Advantages and Limitations
- Advantages:
- Simple and easy to use.
- Efficient for small to medium-sized arrays.
- Limitations:
- Fixed size, which cannot be changed after the array is declared.
- Lack of built-in bounds checking, which can lead to undefined behavior if accessed out of bounds.
Example:
Here's a complete example demonstrating various operations on a single-dimensional array:
#include <iostream>
int main() {
// Declaration and initialization
int myArray[5] = {1, 2, 3, 4, 5};
// Accessing elements
std::cout << "First element: " << myArray[0] << std::endl;
std::cout << "Third element: " << myArray[2] << std::endl;
// Modifying an element
myArray[1] = 10;
std::cout << "After modification, second element: " << myArray[1] << std::endl;
// Iterating over the array
std::cout << "Array elements: ";
for (int i = 0; i < 5; i++) {
std::cout << myArray[i] << " ";
}
std::cout << std::endl;
return 0;
}
Output:
First element: 1
Third element: 3
After modification, second element: 10
Array elements: 1 10 3 4 5
This example illustrates the basic concepts and operations related to single-dimensional arrays in C++.