0% found this document useful (0 votes)
8 views6 pages

Array in C++

Uploaded by

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

Array in C++

Uploaded by

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

ARRAY IN C++

In C++, an array is a data structure that is used to store multiple values of similar
data types in a contiguous memory location. array in C++ is a group of similar
types of elements that have contiguous memory location.

Array Declaration in C++


In C++, we can declare an array by simply specifying the data type first and then
the name of an array with its size.
data_type array_name[Size_of_array];
Example
int arr[5];
Here,
 int: It is the type of data to be stored in the array. We can also use other data
types such as char, float, and double.
 arr: It is the name of the array.
 5: It is the size of the array which means only 5 elements can be stored in the
array.
To declare an array, define the variable type, specify the name of the array
followed by square brackets and specify the number of elements it should store:
string cars[4];

ANSHUL JAIN
We have now declared a variable that holds an array of four strings. To insert
values to it, we can use an array literal - place the values in a comma-separated
list, inside curly braces:
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};

To create an array of three integers, you could write:

int myNum[3] = {10, 20, 30};


Access the Elements of an Array

You access an array element by referring to the index number inside square
brackets [].

This statement accesses the value of the first element in cars:

string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};


cout << cars[0];
Change an Array Element

To change the value of a specific element, refer to the index number:

cars[0] = "Opel";
Example
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
cout << cars[0];

Properties of Arrays in C++


 An Array is a collection of data of the same data type, stored at a contiguous
memory location.
 Indexing of an array starts from 0. It means the first element is stored at the
0th index, the second at 1st, and so on.
 Elements of an array can be accessed using their indices.
 Once an array is declared its size remains constant throughout the program.

ANSHUL JAIN
 An array can have multiple dimensions.
 The size of the array in bytes can be determined by the sizeof operator using
which we can also find the number of elements in the array.

// C++ Program to Illustrate How to Access Array Elements


#include <iostream>

using namespace std;

int main()

int arr[3];

// Inserting elements in an array

arr[0] = 10;

arr[1] = 20;

arr[2] = 30;

// Accessing and printing elements of the array

cout << "arr[0]: " << arr[0] << endl;

cout << "arr[1]: " << arr[1] << endl;

cout << "arr[2]: " << arr[2] << endl;

return 0;

Size of an Array in C++


we can calculate the size of an array using sizeof() operator

data_type size = sizeof(Array_name) / sizeof(Array_name[index]);

// C++ Program to Illustrate How to Find the Size of an Array

#include <iostream>

using namespace std;

int main()

int arr[] = { 1, 2, 3, 4, 5 };

ANSHUL JAIN
// Size of one element of an array

cout << "Size of arr[0]: " << sizeof(arr[0]) << endl;

// Size of array 'arr'

cout << "Size of arr: " << sizeof(arr) << endl;

// Length of an array

int n = sizeof(arr) / sizeof(arr[0]);

cout << "Length of an array: " << n << endl;

return 0;

C++ Array Types

1. Single Dimensional Array


2. Multidimensional Array

1. Single-dimensional array: It is a collection of elements of the same data


typestored in a contiguous block of memory.

There are various ways to do this:

 Initialize at the time of declaration using {}.

int a[5] = {1, 2, 3, 4, 5};

 Initialize an array without specifying its size at declaration time.

int a[] = {1, 2, 3, 4, 5};

ANSHUL JAIN
1. Multi-dimensional array: It is an array that contains one or more arrays as
its elements. In this type of array, two indexes are there to describe each
element, the first index represents a row, and the second index represents
a column.

data_Type array_name[n][m];
Where,

n: Number of rows.
m: Number of columns.

// c++ program to illustrate the two-dimensional array

#include <iostream>

using namespace std;

int main()

// Declaring 2D array

int arr[4][4];

// Initialize 2D array using loop

for (int i = 0; i < 4; i++) {

for (int j = 0; j < 4; j++) {

arr[i][j] = i + j;

ANSHUL JAIN
}

// Printing the element of 2D array

for (int i = 0; i < 4; i++) {

for (int j = 0; j < 4; j++) {

cout << arr[i][j] << " ";

cout << endl;

return 0;

ANSHUL JAIN

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy