0% found this document useful (0 votes)
6 views11 pages

Unit 2 Part 3

The document provides an overview of arrays in C++, including their types (single-dimensional, two-dimensional, and multi-dimensional), syntax for declaration and initialization, and methods for accessing and updating elements. It explains properties of arrays, such as contiguous memory storage and fixed size, and illustrates how to find the size of an array using the sizeof operator. Additionally, examples of C++ programs demonstrate array initialization, element access, and traversal.

Uploaded by

soham patil
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)
6 views11 pages

Unit 2 Part 3

The document provides an overview of arrays in C++, including their types (single-dimensional, two-dimensional, and multi-dimensional), syntax for declaration and initialization, and methods for accessing and updating elements. It explains properties of arrays, such as contiguous memory storage and fixed size, and illustrates how to find the size of an array using the sizeof operator. Additionally, examples of C++ programs demonstrate array initialization, element access, and traversal.

Uploaded by

soham patil
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/ 11

Array in C++

• An array is an groups of elements that can be identifies as a similar types i.e array of integer
types ,array of floating types , array of Character types and so on.
• Array can be categorized into two part:
• Single Dimensional Array (1-D Array)
• Two Dimensional Array (2-D Array)
• Multi – Dimensional Array
• For example, if we have to store the marks of 4 or 5 students then we can easily store them by
creating 5 different variables but what if we want to store marks of 100 students or say 500
students then it becomes very challenging to create that numbers of variable and manage them.
Now, arrays come into the picture that can do it easily by just creating an array of the required
size.

Notes prepared by Prof. Anushree K. Rewale 106


• Syntax:
• 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.

Notes prepared by Prof. Anushree K. Rewale 107


• 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.

• 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.

• We can find the size of the type of elements stored in an array by subtracting adjacent addresses.

Notes prepared by Prof. Anushree K. Rewale 108


• Initialization of Array in C++:
• Initialize Array with Values in C++:
• We have initialized the array with values. The values enclosed in curly braces ‘{}’ are assigned to the array. Here, 1 is
stored in arr[0], 2 in arr[1], and so on. Here the size of the array is 5.
• Eg. int arr[5] = {1, 2, 3, 4, 5};

• Initialize Array with Values and without Size in C++


• We have initialized the array with values but we have not declared the length of the array, therefore, the length of an
array is equal to the number of elements inside curly braces.
• Eg. int arr[] = {1, 2, 3, 4, 5};

• Initialize Array after Declaration (Using Loops)


• We have initialized the array using a loop after declaring the array. This method is generally used when we want to
take input from the user or we cant to assign elements one by one to each index of the array. We can modify the loop
conditions or change the initialization values according to requirements.
• Eg. for (int i = 0; i < N; i++)
{
arr[i] = value;
}

Notes prepared by Prof. Anushree K. Rewale 109


• Accessing an Element of an Array in C++:
• Elements of an array can be accessed by specifying the name of the array, then the index of the element
enclosed in the array subscript operator []. For example, arr[i].
• Example 1: The 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;
}
Notes prepared by Prof. Anushree K. Rewale 110
• Update Array Element:
• To update an element in an array, we can use the index which we want to update enclosed within the
array subscript operator and assign the new value.

• Eg. arr[i] = new_value;

• Traverse an Array in C++:


• We can traverse over the array with the help of a loop using indexing in C++. First, we have initialized an array
‘table_of_two’ with a multiple of 2. After that, we run a for loop from 0 to 9 because in an array indexing starts
from zero. Therefore, using the indices we print all values stored in an array.

Notes prepared by Prof. Anushree K. Rewale 111


• Example 2: The C++ Program to Illustrate How to Traverse an Array
#include <iostream>
using namespace std;
int main()
{
// Initialize the array
int table_of_two[10] = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };

// Traverse the array using for loop


for (int i = 0; i < 10; i++)
{
// Print the array elements using indexing
cout << table_of_two[i] << " ";
}
return 0;
}

Notes prepared by Prof. Anushree K. Rewale 112


• Size of an Array in C++
• In C++, we do not have the length function as in Java to find array size but we can calculate the size
of an array using sizeof() operator trick. First, we find the size occupied by the whole array in the
memory and then divide it by the size of the type of element stored in the array. This will give us the
number of elements stored in the array.
• Syntax: data_type size = sizeof(Array_name) / sizeof(Array_name[index]);
• Example: The 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 };
cout << "Size of arr[0]: " << sizeof(arr[0]) << endl; // Size of one element of an array
cout << "Size of arr: " << sizeof(arr) << endl; // Size of array 'arr'
int n = sizeof(arr) / sizeof(arr[0]); // Length of an array
cout << "Length of an array: " << n << endl;
return 0;
}
Notes prepared by Prof. Anushree K. Rewale 113
• 2 – D Array:
• In C++, a two-dimensional array is a grouping of elements arranged in rows and columns. Each
element is accessed using two indices: one for the row and one for the column, which makes it easy
to visualize as a table or grid.
• Syntax of 2D array:
data_Type array_name[n][m];
• n: Number of rows.
• m: Number of columns.

Notes prepared by Prof. Anushree K. Rewale 114


• Example:
#include <iostream>
using namespace std;
int main()
{
int arr[3][3]; // Declaring 2D array
for (int i = 0; i <= 3; i++) { // Initialize 2D array using loop
for (int j = 0; j <= 3; j++) {
arr[i][j] = i + j;
}
}
for (int i = 0; i < 4; i++) { // Printing the element of 2D array
for (int j = 0; j < 4; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
return 0;
} Notes prepared by Prof. Anushree K. Rewale 115
• Multidimensional Arrays in C++
• Arrays declared with more than one dimension are called multidimensional arrays. The most widely used
multidimensional arrays are 2D arrays and 3D arrays. These arrays are generally represented in the form of
rows and columns.

• Multidimensional Array Declaration

Data_Type Array_Name[Size1][Size2]...[SizeN];

where,

• Data_Type: Type of data to be stored in the array.

• Array_Name: Name of the array.

• Size1, Size2,…, SizeN: Size of each dimension.

Notes prepared by Prof. Anushree K. Rewale 116

You might also like