Unit - 4 Array (28th March 2025)
Unit - 4 Array (28th March 2025)
1
C++ Arrays
Arrays are used to store multiple values in a single variable, instead of declaring
separate variables for each value.
To declare an array, define the variable type, specify the name of the array
string cars[4];
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"};
Note: Array indexes start with 0: [0] is the first element. [1] is the second
element, etc.
Int marks[500]
2
Declaring an array in C++
int arr[5];
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
Method 2:
int arr[] = {10, 20, 30, 40, 50};
Method 3:
int arr[5] = {10, 20, 30, 40, 50, 60};
Array index starts with 0, which means the first array element is at index 0,
second is at index 1 and so on. We can use this information to display the array
elements. See the code below:
3
#include <iostream>
using namespace std;
int main(){
int arr[] = {11, 22, 33, 44, 55};
cout<<arr[0]<<endl;
cout<<arr[1]<<endl;
cout<<arr[2]<<endl;
cout<<arr[3]<<endl;
cout<<arr[4]<<endl;
return 0;
}
Output:
11
22
33
44
55
Although this code worked fine, displaying all the elements of array like this is
not recommended. When you want to access a particular array element then this
is fine but if you want to display all the elements then you should use a loop like
this:
#include <iostream>
using namespace std;
int main(){
int arr[] = {11, 22, 33, 44, 55};
int n=0;
while(n<=4){
cout<<arr[n]<<endl;
n++;(n=n+1)
}
return 0;
}
4
Change an Array Element
Example
cars[0] = "Opel";
Example
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
cout << cars[0];
// Now outputs Opel instead of Volvo
You can loop through the array elements with the for loop.
Example
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
The following example outputs the index of each element together with its
value:
Example
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
for(int i = 0; i < 4; i++) {
cout << i << ": " << cars[i] << "\n";
}
5
Omit Array Size
You don't have to specify the size of the array. But if you don't, it will only be
as big as the elements that are inserted into it:
This is completely fine. However, the problem arise if you want extra space for
future elements. Then you have to overwrite the existing values:
If you specify the size however, the array will reserve the extra space:
Now you can add a fourth and fifth element without overwriting the others:
cars[3] = "Mazda";
cars[4] = "Tesla";
string cars[5];
cars[0] = "Volvo";
cars[1] = "BMW";
...
6
Two dimensional array
Lets see how to declare, initialize and access Two Dimensional Array elements.
int myarray[2][3];
Initialization:
We can initialize the array in many ways:
Method 1:
Row(1) 10 D 11 E 12 F
Method 2:
This way of initializing is preferred as you can visualize the rows and columns
here.
7
Example: Two dimensional array in C++
#include <iostream>
using namespace std;
int main(){
int arr[2][3] = {{11, 22, 33}, {44, 55, 66}};
for(int i=0; i<2;i++){
for(int j=0; j<3; j++){
cout<<"arr["<<i<<"]["<<j<<"]: "<<arr[i][j]<<endl;
}
}
return 0;
}
Output:
arr[0][0]: 11
arr[0][1]: 22
arr[0][2]: 33
arr[1][0]: 44
arr[1][1]: 55
arr[1][2]: 66
Array is a data structure that is used to store variables that are of similar data
types at contiguous locations. The main advantage of the array is random access
and cache friendliness. There are mainly three types of the array:
One Dimensional (1D) Array
Two Dimension (2D) Array
8
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:
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.
10
A Simple C++ program to add two Matrices
Here we are asking user to input number of rows and columns of matrices and
then we ask user to enter the elements of both the matrices, we are storing the
input into a multidimensional array for each matrix and after that we are adding
corresponding elements of both the matrices and displaying them on screen.
#include<iostream>
using namespace std;
int main()
{
int row, col, m1[10][10], m2[10][10], sum[10][10];
cout<<"Output: ";
for (int i = 0;i<row;i++ ) {
for (int j = 0;j<col;j++ ) {
sum[i][j]=m1[i][j]+m2[i][j];
cout<<sum[i][j]<<" ";
}
}
return 0;
}
11
Output:
#include <iostream>
using namespace std;
int main(){
float percentage[4];
percentage[0] = 56.3;
percentage[1] = 99.12;
percentage[2] = 78.32;
percentage[3] = 61.3;
cout<<"3rd element is "<<percentage[2];
return 0;
}
Output
3rd element is 78.32
12
Example 2: Take Inputs from User and Store Them in an Array
#include <iostream>
using namespace std;
int main() {
int numbers[5];
Example 3: Display Sum and Average of Array Elements Using for Loop
#include <iostream>
using namespace std;
int main() {
double sum = 0;
double count = 0;
double average;
cout << "The numbers are: ";
// print array elements
// use of range-based for loop
for (const double &n : numbers) {
cout << n << " ";
13
// calculate the sum
sum += n;
return 0;
}
Output
14