One-Dimensional Array
- Arrays store multiple values in a single variable.
- This avoids declaring separate variables for each value.
- Each element is accessed by an index.
What is an Array?
- Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.
- Each element can be accessed by an index.
Declaration of an Array
- Declaring an array means allocating memory to store multiple elements of a given data type.
- Syntax:
dataType arrayName[arraySize];
Properties of 1D Array in C++
- Index-Based Access
- Fixed Size
- Linear Structure
Index-Based Access
- Elements 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.
- It goes up to one less than the size of the array.
- For an array with five elements, the indices range from 0 to 4.
Fixed Size
- One-dimensional arrays have a fixed size.
- The size of the array is specified at compile time.
- Once declared with a certain number of elements, the size cannot be changed during runtime.
Linear Structure
- The elements are arranged in a linear or one-dimensional structure.
- There is 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.
How to Use 1D Array in C++
- Usage involves:
- Declaring the array.
- Initializing it with values (optional).
- Accessing and modifying elements.
- Performing various operations on the array.
Declaring an Array
- To declare an array in C++, specify the data type of the elements followed by the array name and the size of the array in square brackets.
- Example:
int arr[5]; declares an array of integers with five elements.
Accessing and Modifying Elements
- Elements are accessed using indices, starting from 0 for the first element.
- You can access and modify elements using square brackets
[]. - Example:
int value = arr[2]; // Accessing the element at index 2arr[3] = 60; // Modifying the element at index 3 to 60
Example of One-Dimensional Array in C++
#include <iostream>
using namespace std;
int main() {
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;
}
- Output:
Element at index 2: 30Modified element at index 3: 60Sum of all elements: 170
Two-Dimensional Arrays
- A 2-dimensional array in C++ is a collection of elements arranged in rows and columns, forming a table or grid-like structure.
- It is essentially an array of arrays where each element is accessed by two indices: one for the row and one for the column.
Two-Dimensional Arrays
- A 2-dimensional array in C++ is a collection of elements arranged in rows and columns, forming a table or grid-like structure.
- It is essentially an array of arrays where each element is accessed by two indices: one for the row and one for the column.
Syntax:
dataType arrayName[number_of_rows][number_of_columns];
Properties of 2D Array in C++
- Index based access
- Fixed Size
- Processing Elements Row by Row or Column by Column
Index-Based Access
- Elements of the array are accessed using indices.
- The first element is accessed with the index 0, and each subsequent element is accessed with an index that increases by 1.
Fixed Size
- When you declare a 2D or multidimensional array with a fixed size, the number of rows and columns must be known at compile time.
- This means you must specify the size in the declaration
Processing Elements Row by Row or Column by Column
- To access or process all elements of a 2D array, nested loops are commonly used.
- Usually with the outer loop iterating over rows and the inner loop over columns.
Example of Two-Dimensional Array in C++
#include <iostream>
using namespace std;
int main() {
const int rows = 2;
const int cols = 3;
int numbers[rows][cols] = {
{1, 2, 3},
{4, 5, 6}
};
int sum = 0;
// Print the array and calculate the sum
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << numbers[i][j] << " ";
sum += numbers[i][j];
}
cout << endl;
}
cout << "Sum of all elements: " << sum << endl;
}
1 2 3
4 5 6
Sum of all elements: 21