Arrays in C++
ONE-DIMENSIONAL ARRAY
Arrays are used to store multiple values in a single variable.
This avoids declaring separate variables for each value.
Each element in an array can be accessed by its index.
WHAT IS AN ARRAY?
Declaring an array involves allocating memory to store multiple elements of a specific data type.
Syntax:
```
dataType arrayName[arraySize];
## DECLARATION OF AN ARRAY
## PROPERTIES OF 1D ARRAY IN C++
* Index-Based Access
* Fixed Size
* Linear Structure
### INDEX-BASED ACCESS
* Elements in the array are accessed using an index.
* The index is a numeric value indicating the element's position 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 is determined at compile time, and cannot be changed during runtime after declaration.
### LINEAR STRUCTURE
* 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 (with rows and columns), 1D 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
* Specify the data type of the elements.
* Followed by the array name and the array size in square brackets.
* Example:
```cpp
int arr[5];
* This declares an array of integers with five elements.
ACCESSING AND MODIFYING ELEMENTS
Elements are accessed using indices, starting from 0 for the first element.
Use square brackets
[]to access and modify elements.Example:
```cpp
int value = arr[2]; // Accessing the element at index 2
arr[3] = 60; // Modifying the element at index 3 to 60
### EXAMPLE OF ONE-DIMENSIONAL ARRAY IN C++
cpp
include
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: 30
Modified element at index 3: 60
Sum of all elements: 170
TWO-DIMENSIONAL ARRAYS
A 2-dimensional array in C++: a collection of elements arranged in rows and columns, forming a table or grid-like structure.
It is an array of arrays where each element is accessed by two indices: one for the row and one for the column.
TWO-DIMENSIONAL ARRAYS
Syntax:
```cpp
dataType arrayName[numberofrows][numberofcolumns];
## PROPERTIES OF 2D ARRAY IN C++
* Index based access
* Fixed Size
* Processing Elements Row by Row or Column by Column
### INDEX-BASED ACCESS
* Arrays use index-based access.
* Elements are accessed using indices.
* The first element is accessed with the index 0.
* 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.
* Specify the size in the declaration.
### PROCESSING ELEMENTS ROW BY ROW OR COLUMN BY COLUMN
* Nested loops are commonly used to access or process all elements of a 2D array.
* The outer loop iterates over rows, and the inner loop iterates over columns.
## EXAMPLE OF TWO-DIMENSIONAL ARRAY IN C++
cpp
include
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;
}
* Output:
1 2 3
4 5 6
Sum of all elements: 21
```