Array
Array
Imagine you have a parking lot at a mall that only allows two-wheelers. Now, think of an array
as a line-up of these two-wheelers in the parking lot. All the bikes are parked right next to
each other, one after the other. Each parking space is like a memory slot, and the entire line-
up is our array.
So, an array is like a row of identical things (in this case, two-wheelers) parked together in a
designated area. The cool thing is, you can easily access any bike in the line-up by knowing its
position, just like you access elements in an array using their index. It's a way of organizing
and keeping track of similar items in a sequence.
An Array is a data structure used to store blocks of information in contiguous
memory allocation. The data can be integer, strings, characters, class objects, etc.
In this memory representation, 2, 7, 4, 8,1, 6 are the elements of the array stored in adjacent
locations, and 0, 1, 2, 3, 4, 5 are the indexes of these elements. Index position starts from 0
and goes up to size-1. In this example, the index goes up to 5, but actually, there are a total
of 6 elements.
dataType arrayName[arraySize];
For example,
int x[6];
Here,
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[3];
return 0;
}
int main()
{
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main() {
int numbers[5];
return 0;
}
Example 3: Display Sum and Average of Array Elements Using for Loop
#include <bits/stdc++.h>
using namespace std;
int main() {
double sum = 0;
double average;
return 0;
}