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

CHPT 3 Array Complete

sppu unit 2

Uploaded by

bhagwatgayal10
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)
17 views

CHPT 3 Array Complete

sppu unit 2

Uploaded by

bhagwatgayal10
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/ 10

Chapter 3: Array

In C++, an array is a data structure that is used to store multiple values of similar data or
homogonous data types in a contiguous memory location.
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.

Properties of Arrays in C++


1) An Array is a collection of data of the same data type, stored at a contiguous memory
location.
2) 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.
3) Elements of an array can be accessed using their indices.
4) Once an array is declared its size remains constant throughout the program.
5) An array can have multiple dimensions.

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.
Datatype[size_of_array] array_name;
or
data_type array_name[Size_of_array];
Example
int age[5];
string name[] ={“rahul”, “riya”};
Initialization of Array in C++
Three type for initialization of array

1) Taking fix size


2) Not fix size
3) For taking value from user

1. 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.
int arr[5] = {1, 2, 3, 4, 5};

2. 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.
int arr[] = {1, 2, 3, 4, 5};

3. 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.
for (int i = 0; i < N; i++)
{
arr[i] = value;
}
Types of array

1. 1-D array
2. 2-D array or multi-dimensional array

One-dimensional array
One-dimensional array is also called as single dimension array and it will be of a linear type
(Elements will be accessed in sequential order).

Simple program for 1D array

#include <iostream>

using namespace std;

int main()
{

int age[] = {10,20,30,40};

// cout<<age[0]<<endl;

//cout<<age[1]<<endl; //endl is used to print in next line

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

cout<<age[i]<<endl;

return 0;
}
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.
The following example represents the two-dimensional array.

int table = A [10] [20] A[row][column]

The above access the two-dimensional array of row value 10 and the column value 20. So the
table will be accessible with 10 rows and 20 columns.

Two Dimensional Array in C++


In C++, a two-dimensional array is a grouuping 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];

#include <iostream>
using namespace std;

int main()
{
int age[2][2] = {{1,2},{3,4}};
// cout<<age[0][1];
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
cout<<age[i][j]<<" ";
}
cout<<endl;
}

return 0;
}
0/p

Operations performed by Array:

Following are the basic operations supported by an array.

1) Traverse − print all the array elements one by one.

2) Insertion − Adds an element at the given index.

3) Deletion − Deletes an element at the given index.

4) Search − Searches an element using the given index or by the value.

5) Update − Updates an element at the given index.


How to search an element in array

#include <iostream>

using namespace std;

int search(int arr[], int length, int value)

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

if (arr[i] == value) {

return i; // Element found, return its index

return -1; // Element not found

int main()

int age[] = {10,20,30,40,50};

int length = sizeof(age) / sizeof(age[0]);

int value = 10;

int res = search(age, length, value);

if (res != -1)

cout << "Element found at index " << res << endl;

}
else

cout << "Element not found in the array." << endl;

return 0;

How to reverse elements in array

#include <iostream>

#include <algorithm> // for reverse function

using namespace std;

int main()

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

int length = sizeof(age) / sizeof(age[0]);


cout<<"before reserve function"<<endl;

for (int i = 0; i < length; i++)

cout << age[i] << " ";

// Using inbuilt method in C++ : for this include<algorithm> is imp

reverse(age, age + length);

cout<<endl;

cout<<"after reverse function"<<endl;

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

cout << age[i] << " ";

return 0;

}
How to find largest element in array : *max_element(age, age + length)

#include <iostream>

#include <algorithm> // for reverse function

using namespace std;

int main()

int age[] = { 10, 22, 63, 74, 85 };

int length = sizeof(age) / sizeof(age[0]);

cout<<"elements"<<endl;

for (int i = 0; i < length; i++)

cout << age[i] << " ";


}

cout<<endl;

cout<< "largest number"<<endl;

cout<<*max_element(age, age + length);

return 0;

You might also like