Arrays in c++ notes with examples
Arrays in C++:
Definition of Arrays
- An array
is a collection of elements of the same data type stored in contiguous
memory locations.
- Arrays
are useful for storing a fixed-size, ordered collection of data, such as a
list of integers, characters, or floating-point numbers.
Syntax for Declaring Arrays
data_type array_name[size];
- data_type:
The type of data (e.g., int, float, char).
- array_name:
The name of the array.
- size:
The number of elements the array can hold.
Example:
int numbers[5]; //
Declares an integer array of size 5
float grades[10]; //
Declares a float array of size 10
Initializing Arrays
- During
Declaration:
- int
numbers[5] = {1, 2, 3, 4, 5}; //
All elements initialized
- int
numbers[5] = {1, 2}; //
Partially initialized (others set to 0)
- int
numbers[] = {1, 2, 3}; //
Compiler determines the size (size = 3)
- After
Declaration:
- int
numbers[5];
- numbers[0]
= 10; // Assign values element by
element
- numbers[1]
= 20;
Accessing Array Elements
- Use
the index operator ([]) to access or modify elements.
- Indexing
starts from 0.
Example:
int numbers[5] = {10, 20, 30, 40, 50};
cout << numbers[0];
// Outputs 10
numbers[2] = 35;
// Updates the third element to 35
Types of Arrays
- One-Dimensional
Array:
- A
single row of elements.
- Syntax:
- int
arr[5] = {1, 2, 3, 4, 5};
- Multi-Dimensional
Array:
- Arrays
with multiple rows and columns.
- Syntax
for 2D Arrays:
- data_type
array_name[rows][columns];
- Example:
- int
matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
- cout
<< matrix[1][2]; // Outputs
6
- Character
Arrays (Strings):
- Character
arrays can be used to store strings.
- Example:
- char
name[10] = "John";
Array Characteristics
- Fixed
Size:
- Once
declared, the size cannot be changed during runtime.
- Contiguous
Memory:
- Elements
are stored in consecutive memory locations.
Advantages of Arrays
- Allows
efficient data storage and access.
- Useful
for iterating and processing multiple elements with loops.
Limitations of Arrays
- Fixed
size: Cannot adjust dynamically.
- Elements
must be of the same data type.
- Inserting
and deleting elements requires shifting and is not efficient.
Common Operations on Arrays
- Traversal:
Use loops to access and manipulate array elements.
- for
(int i = 0; i < 5; i++) {
- cout << numbers[i];
- }
- Search:
Use a loop to find an element.
- for
(int i = 0; i < size; i++) {
- if (numbers[i] == value) {
- cout << "Found at index
" << i;
- }
- }
- Sorting:
Use sorting algorithms like Bubble Sort, Selection Sort, or standard
library functions.
- sort(array,
array + size); // C++ STL function
2D Array
A 2D array in C++ is an array of arrays. It's used to
store data in a matrix-like structure, with rows and columns. Here’s a detailed
explanation and examples to help you understand how to work with 2D arrays in
C++.
Basic Syntax for Declaring a 2D Array
data_type array_name[rows][columns];
- data_type:
The type of data the array will hold (e.g., int, float, char).
- rows:
The number of rows in the array.
- columns:
The number of columns in the array.
Static Initialization
#include <iostream>
Using namespace std;
int main() {
int arr[3][3] =
{ // 3x3 matrix
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Accessing
elements
std::cout <<
"Element at row 1, column 2: " << arr[0][1] << std::endl;
// Output: 2
return 0;
}
Dynamic Initialization
#include <iostream>
int main() {
int arr[3][3]; //
Declare a 3x3 array
// Initialize
array using nested loops
int value = 1;
for (int i = 0; i
< 3; i++) { // Rows
for (int j =
0; j < 3; j++) { // Columns
arr[i][j]
;
value++;
}
}
// Display the
array
std::cout <<
"2D Array:" << std::endl;
for (int i = 0; i
< 3; i++) { // Rows
for (int j =
0; j < 3; j++) { // Columns
std::cout
<< arr[i][j] << " ";
}
std::cout
<< std::endl; // New line after each row
}
return 0;
}
3D Array
This C++ code demonstrates how to work with a 3D array.
Let's break it down step by step:
Code Breakdown:
- Declaring
the 3D Array:
cpp
CopyEdit
int arr[2][3][4]; // 2x3x4 array (2 2D arrays, each with 3
rows and 4 columns)
Here, arr is a 3D array with dimensions 2 x 3 x 4:
- 2
is the number of 2D arrays.
- 3
is the number of rows in each 2D array.
- 4
is the number of columns in each row.
So, the total size of this 3D array is 2 * 3 * 4 = 24
elements.
- Initializing
the 3D Array:
cpp
CopyEdit
int value = 1;
for (int i = 0; i < 2; i++) { // First dimension
for (int j = 0; j
< 3; j++) { // Second dimension
for (int k =
0; k < 4; k++) { // Third dimension
arr[i][j][k] = value++;
}
}
}
The array is initialized using nested loops:
- The
outer loop (i) iterates over the first dimension (2 2D arrays).
- The
middle loop (j) iterates over the second dimension (3 rows).
- The
inner loop (k) iterates over the third dimension (4 columns).
Inside the innermost loop, each element of the array
arr[i][j][k] is assigned a value. The variable value starts at 1 and is
incremented after each assignment (using value++).
After the loops are finished, the array will contain the
numbers from 1 to 24, spread across the 3D array in a sequential manner.
- Displaying
the 3D Array:
cpp
CopyEdit
std::cout << "3D Array Elements:" <<
std::endl;
for (int i = 0; i < 2; i++) {
std::cout <<
"2D Array " << i + 1 << ":" << std::endl;
for (int j = 0; j
< 3; j++) {
for (int k =
0; k < 4; k++) {
std::cout
<< arr[i][j][k] << " ";
}
std::cout
<< std::endl; // New line after each row
}
std::cout <<
std::endl; // New line after each 2D array
}
The array is displayed using nested loops:
- The
outer loop (i) iterates over the 2 2D arrays.
- The
middle loop (j) iterates over the rows (3 rows per 2D array).
- The
inner loop (k) iterates over the columns (4 columns per row).
Inside the innermost loop, each element arr[i][j][k] is
printed out, followed by a space. After each row, a new line is printed. After
each 2D array, another new line is printed.
Output Example:
Given that the array is initialized with values from 1 to
24, the output will look like this:
mathematica
CopyEdit
3D Array Elements:
2D Array 1:
1 2 3 4
5 6 7 8
9 10 11 12
2D Array 2:
13 14 15 16
17 18 19 20
21 22 23 24
- The
first 2D array (corresponding to arr[0]) is displayed first, and the
second 2D array (arr[1]) follows.
- Each
2D array contains 3 rows, and each row contains 4 elements.
Explanation of Key Concepts:
- 3D
Array: A 3D array can be thought of as a stack of 2D arrays. In this
case, arr consists of 2 stacks (the first dimension has size 2),
each containing 3 rows (second dimension) and 4 columns
(third dimension).
- Nested
Loops: The nested loops are essential for iterating through each of
the three dimensions of the array (first, second, and third) to initialize
or access its elements.
Comments
Post a Comment