0% found this document useful (0 votes)
2 views

Array in CPP

The document explains arrays in C++, detailing their definition, types (single and multidimensional), and key characteristics. It provides examples of single-dimensional and two-dimensional arrays, highlighting their differences and applications. Additionally, it outlines the advantages of using arrays in C++ programming.

Uploaded by

technicalvcpt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Array in CPP

The document explains arrays in C++, detailing their definition, types (single and multidimensional), and key characteristics. It provides examples of single-dimensional and two-dimensional arrays, highlighting their differences and applications. Additionally, it outlines the advantages of using arrays in C++ programming.

Uploaded by

technicalvcpt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

C++ Programming

Topperworld.in

Array In C++

⚫ In C++, an array is a variable that can store multiple values of the same type.
⚫ A collection of related data items stored in adjacent memory places is
referred to as an array in the C++ programming language or any other
programming language for that matter.
⚫ Elements of an array can be accessed arbitrarily using its indices. They can
be used to store a collection of any type of primitive data type, including int,
float, double, char, etc.
⚫ An array in C++ can also store derived data types like structures, pointers,
and other data types, which is an addition.

❖ Types of Array
There are 2 types of arrays in C++ programming:

• Single Dimensional Array


• Multidimensional Array

©Topperworld
C++ Programming

➢ Single Dimensional Array


• It is a list of the variable of similar data types.
• It allows random access and all the elements can be accessed with the
help of their index.
• The size of the array is fixed.
• For a dynamically sized array, vector can be used in C++.

Example:

#include <iostream>
int main() {
// Declare and initialize a single-dimensional array
int numbers[5] = {10, 20, 30, 40, 50};
// Access and print elements of the array
std::cout << "Array elements: ";
for (int i = 0; i < 5; ++i) {
std::cout << numbers[i] << " ";
}
return 0;
}

Output:
Array elements: 10 20 30 40 50

©Topperworld
C++ Programming

➢ Multi-dimensional array
• A two-dimensional array is the most basic type of multidimensional array;
it also qualifies as a multidimensional array. There are no restrictions on
the array's dimensions.

Two Dimensional Array:


• It is a list of lists of the variable of the same data type.
• It also allows random access and all the elements can be accessed with
the help of their index.
• It can also be seen as a collection of 1D arrays. It is also known as the
Matrix.
• Its dimension can be increased from 2 to 3 and 4 so on.
• They all are referred to as a multi-dimension array.
• The most common multidimensional array is a 2D array.
• Representation of 2 D array:

©Topperworld
C++ Programming

Example:

#include <iostream>

int main() {
// Declare and initialize a 2D array
int matrix[3][3] = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};

// Access and print elements of the 2D array


std::cout << "Matrix elements:" << std::endl;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
std::cout << matrix[i][j] << " ";
}
std::cout << std::endl;
}

return 0;
}

Output:

Matrix elements:
123
456
789

©Topperworld
C++ Programming

❖ Difference between One and Two Dimension Array

Basis One Dimension Array Two Dimension Array

Store a single list of


Store a ‘list of lists’ of the
Definition the element of a
element of a similar data type.
similar data type.

Represent multiple data items


Represent multiple
Representation as a table consisting of rows and
data items as a list.
columns.

The declaration varies


The declaration varies for
for different
different programming
programming
language:
Declaration language:
For C++,
For C++,
datatype
datatype
variable_name[row][column]
variable_name[row]

Dimension One Two

size of(datatype of the size of(datatype of the variable


Size(bytes) variable of the array) * of the array)* the number of
size of the array rows* the number of columns.

int arr[5]; //an array int arr[2][5]; //an array with


with one row and five two rows and five columns will
columns will be be created.
Example
created. a b c d e
{a , b , c , d , e} f g h i j

©Topperworld
C++ Programming

❖ Important things to remember while using arrays in C++


✓ The array's indexes begin at 0. Meaning that the first item saved at index
0 is x[0].
✓ The final element of an array with size n is kept at index (n-1). This
example's final element is x[5].
✓ An array's elements have sequential addresses. Consider the scenario
where x[0beginning ]'s address is 2120.

❖ Applications of Arrays:
• 2D Arrays are used to implement matrices.
• Arrays can be used to implement various data structures like
a heap, stack, queue, etc.
• They allow random access.
• They are cache-friendly.

❖ Advantages of C++ Array


• Code Optimization (less code)
• Random Access
• Easy to traverse data
• Easy to manipulate data
• Easy to sort data etc.

©Topperworld

You might also like