CHPT 3 Array Complete
CHPT 3 Array Complete
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.
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).
#include <iostream>
int main()
{
// cout<<age[0]<<endl;
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.
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.
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
#include <iostream>
if (arr[i] == value) {
int main()
if (res != -1)
cout << "Element found at index " << res << endl;
}
else
return 0;
#include <iostream>
int main()
int age[] = { 1, 2, 3, 4, 5 };
cout<<endl;
return 0;
}
How to find largest element in array : *max_element(age, age + length)
#include <iostream>
int main()
cout<<"elements"<<endl;
cout<<endl;
return 0;