One Dimensional Arrays in C++
Last Updated :
27 May, 2024
One-dimensional arrays are like a row of boxes where you can store things where each box can hold one item, such as a number or a word. For example, in an array of numbers, the first box might hold 5, the second 10, and so on. You can easily find or change what's in each box by referring to its position, called an index. Arrays are handy because they let you store lots of related data in one place and access it quickly.
In this article, we will learn about One Dimensional Arrays in C++.
Syntax of 1D Array in C++
The one dimensional array can be declared as shown below:
element_type array_name [size]
Properties of 1D Array in C++
One-dimensional arrays are organized as a sequence of contiguous memory locations, each capable of holding a single data element of the same type. Following are the main properties of 1D array in C++:
Contiguous Memory Allocation
When you create an array in C++, it allocates a block of memory to hold all the elements. This means the elements are stored right next to each other in memory, without any gaps. For example, if you have an array of integers, each integer occupies a fixed-size space in memory, and they're arranged one after the other.
Index-Based Access
Elements in the array are accessed using an index, which is a numeric value indicating the position of the element within the array. The index starts from 0 for the first element and goes up to one less than the size of the array. So, if you have an array with five elements, the indices range from 0 to 4.
Fixed Size
One-dimensional arrays have a fixed size, meaning once you declare an array with a certain number of elements, you can't change its size during runtime. The size of the array needs to be specified at compile time.
Linear Structure
The elements in a one-dimensional array are arranged in a linear or one-dimensional structure. This means there's only one dimension to consider when accessing or arranging the elements. Unlike multi-dimensional arrays, which have rows and columns, one-dimensional arrays have a single row or sequence of elements.
One-dimensional arrays in C++ are organized as a contiguous block of memory locations, accessed using indices, with a fixed size determined at compile time. Their linear structure and index-based access make them efficient for storing and accessing collections of data in computer programs.
How to Use 1d Array in C++
Usage of one-dimensional arrays in C++ involves declaring the array, initializing it with values (optional), accessing and modifying elements, and performing various operations on the array.
Declaring an Array
To declare an array in C++, you specify the data type of the elements followed by the array name and the size of the array in square brackets. For example, to declare an array of integers with five elements:
int arr[5];
This creates an array named arr capable of holding five integers.
Initializing the Array
You can optionally initialize the array with values at the time of declaration or later using a loop or by assigning values individually. For example:
int arr[5] = {10, 20, 30, 40, 50}; // Initializing with values
This initializes the array arr with five integers: 10, 20, 30, 40, and 50.
Accessing and Modifying Elements
Elements in the array are accessed using indices, starting from 0 for the first element. You can access and modify elements using square brackets []. For example:
int value = arr[2]; // Accessing the element at index 2 (30)
arr[3] = 60; // Modifying the element at index 3 to 60
Performing Basic Operations
You can perform various operations on arrays, such as searching for an element, sorting the elements, or calculating the sum of all elements. These operations typically involve iterating through the array using loops and applying the necessary logic. For example, to find the sum of all elements in the array:
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += arr[i];
}
This loop iterates through each element of the array and adds it to the sum variable.
Dynamic Memory Allocation (Optional)
In addition to fixed-size arrays, you can also dynamically allocate memory for arrays using pointers. This allows you to create arrays of variable sizes at runtime. For example:
int* dynamicArr = new int[5]; // Dynamically allocating memory for an array of 5 integers
Remember to deallocate the memory using delete[] when you're done using the dynamically allocated array to avoid memory leaks.
Complexity Analysis of Basic Array Operations
Several basic operations can be performed on one-dimensional arrays in C++, each with its associated time and space complexity:
Accessing an Element by Index
Time Complexity: O(1). Since arrays provide constant-time access to elements based on their indices, accessing an element by index takes constant time regardless of the size of the array.
Space Complexity: O(1). This operation requires no additional space beyond the memory used by the array itself.
Modifying an Element by Index
Time Complexity: O(1). Similar to accessing an element, modifying an element by index takes constant time regardless of the size of the array.
Space Complexity: O(1). No additional space is required beyond the memory used by the array.
Insertion/Deletion
Time Complexity:
Insertion/Deletion at the beginning or middle of the array: O(n). Since shifting elements requires iterating through and potentially moving each element, the time complexity is linearly proportional to the size of the array.
Insertion/Deletion at the end of the array (if space is available): O(1). If there's available space at the end of the array, inserting or deleting an element requires only updating the array's size and doesn't involve shifting elements.
Space Complexity: O(1). Inserting or deleting an element in the array doesn't require additional space beyond the memory used by the array itself.
Understanding the time and space complexities of these basic operations is essential for designing efficient algorithms and data structures that utilize one-dimensional arrays.
Example of One Dimensional Array in C++
C++
#include <iostream>
using namespace std;
int main() {
// Declaration and initialization of an array
int arr[5] = {10, 20, 30, 40, 50};
// Accessing elements of the array
cout << "Element at index 2: " << arr[2] << endl;
// Modifying elements of the array
arr[3] = 60;
cout << "Modified element at index 3: " << arr[3] << endl;
// Calculating the sum of all elements
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += arr[i];
}
cout << "Sum of all elements: " << sum << endl;
return 0;
}
OutputElement at index 2: 30
Modified element at index 3: 60
Sum of all elements: 170
Applications of One Dimensional Array
One-dimensional arrays in C++ find wide-ranging applications across various domains due to their simplicity, efficiency, and versatility. Here are some common applications of one-dimensional arrays:
- Data Storage and Retrieval
- Numerical Computations
- Algorithm Design and Implementation
- Input and Output Operations
- Memory Management'
Similar Reads
One Dimensional Arrays in C In C, an array is a collection of elements of the same type stored in contiguous memory locations. This organization allows efficient access to elements using their index. Arrays can also be of different types depending upon the direction/dimension they can store the elements. It can be 1D, 2D, 3D,
5 min read
C++ Multidimensional Array A multidimensional array is an array with more than one dimension. It means that it can grow in different directions i.e. instead of changing the length only, it can also change in width, depth or more.Syntax of Multidimensional ArraysC++data_type array_name[s1][s2]...[sn];where s1, s2,â¦, sn are the
8 min read
Pass Array to Functions in C++ In C++, a collection of elements stored in contiguous memory locations and having the same data type is called an array. Passing arrays to functions is done to perform various operations on array elements without messing up with the main code. In C++, an array can be passed in a function using a poi
5 min read
Array Type Manipulation in C++ This article demonstrates some of the inbuilt functions that can be used to query and manipulate array types, even a multidimensional array. These functions can be useful in cases we need information or manipulate array we initiated with different dimensions. These functions are defined in header fi
5 min read
Initialization of Multidimensional Arrays in C++ In C++, multidimensional arrays are the type of arrays that have multiple dimensions, i.e., they can expand in multiple directions. In this article, we will discuss how to initialize the multidimensional arrays in C++. Methods to Initialize Multidimensional Array in C++We can initialize multidimensi
3 min read
Pointer to an Array in C++ Pointers in C++ are variables that store the address of another variable while arrays are the data structure that stores the data in contiguous memory locations. In C++, we can manipulate arrays by using pointers to them. These kinds of pointers that point to the arrays are called array pointers or
6 min read
How to Take Input in Array in C++? Arrays in C++ are derived data types that can contain multiple elements of the same data type. They are generally used when we want to store multiple elements of a particular data type under the same name. We can access different array elements using their index as they are stored sequentially in th
3 min read
C++ Omit Array Size Prerequisite: Array in C++ The array is a type of data structure that can store data of similar data types. The most significant feature is fixed size. There are two conditions by which we can check the initialization of the array. Methods for Declaring Array 1. Declaring size during initialization:
2 min read
array get() function in C++ STL The array::get() is a built-in function in C++ STL which returns a reference to the i-th element of the array container. Syntax: get(array_name) Parameters: The function accepts two mandatory parameters which are described below. i - position of an element in the array, with 0 as the position of the
2 min read
Array of list in C++ with Examples What is an array? An array in any programming language is a data structure that is used to store elements or data items of similar data types at contiguous memory locations and elements can be accessed randomly using indices of an array. Arrays are efficient when we want to store a large number of e
5 min read