Arrays in C++ - Reader Mode
Arrays in C++ - Reader Mode
Arrays in C++
So, this is the concept for mathematics that if you have the list of
elements, you can give a single name and you differentiate them with
their index or subscript that is 0, 1, 2, and so on. The same concept is
applied in programming models. Let us come to C++. If we have to
store a single value then we have to declare a variable, so the variable
will have a data type.
int x = 5;
So, this is an integer type variable ‘x’ having value ‘5’. So, we know very
well that an integer takes 2 bytes. We are assuming that the integer
chrome-distiller://831526f4-c023-46de-8de4-5b016f8c0d22_6db52dd5355e6d9238b5e142c25d40266eb629e1b63af8115cfc7be45f93be79/?title=Arrays+in+C%2… 1/14
4/25/23, 10:33 AM Arrays in C++ - Reader Mode
takes 2 bytes. This will consume 2 bytes of memory and then in that
’5’ will be stored. If we write with address then,
So let us say the address of the first bite is ‘200’ and the next bite is
‘201’. Now next how to have the list of elements. So, this is the
concept of arrays. Arrays in programming or in C++.
What is an Array?
We already learned that variables are used to store the value. But
variables can hold only one value of a specific type at a time. For a
better understanding please have a look at the below diagram. In the
below example at any point of time x can hold only one type of value.
chrome-distiller://831526f4-c023-46de-8de4-5b016f8c0d22_6db52dd5355e6d9238b5e142c25d40266eb629e1b63af8115cfc7be45f93be79/?title=Arrays+in+C%2… 2/14
4/25/23, 10:33 AM Arrays in C++ - Reader Mode
I know you already feel this awkward. Yes, if the array kind of data
structure is not there then programming would be a bit more complex.
For everything we need to define a new variable even if it is of the
same type. But let’s see how the array solves this problem.
int employeeno[10]={1,2,3,4,5,6,7,8,9,10};
int empno[5]={1,2,3,4,5};
chrome-distiller://831526f4-c023-46de-8de4-5b016f8c0d22_6db52dd5355e6d9238b5e142c25d40266eb629e1b63af8115cfc7be45f93be79/?title=Arrays+in+C%2… 3/14
4/25/23, 10:33 AM Arrays in C++ - Reader Mode
See using this [] along with the variable name you are informing the
compiler that the variable is an array and allocate a block of memory
as specified by the array in memory. The normal array is stored in the
stack however it is possible to create an array in heap memory also
which will be discussed in future articles on dynamic memory
allocation.
Here we have created a variable A with the size of ‘5’. So, you can store
5 values with the same name A. How it looks like in the memory? It
will allocate memory for 5 integers. These all ‘5’ are types of ‘int’. For
that memory, indexing will start from ‘0’ onwards.
We got an array. So, all these 5 integers are side by side or contiguous.
Let us say the first byte’s address is ‘300’ and ‘int’ is taking 2 bytes
then,
chrome-distiller://831526f4-c023-46de-8de4-5b016f8c0d22_6db52dd5355e6d9238b5e142c25d40266eb629e1b63af8115cfc7be45f93be79/?title=Arrays+in+C%2… 4/14
4/25/23, 10:33 AM Arrays in C++ - Reader Mode
int n=5;
int A[n];
int A[]={1,2,3,4,5}
int A[5]={1,2,3,4,5}
Student: Author you said accessing all the elements in an array is easy
and it is through the index but I am not getting how to do it?
Author: Yes, don’t be in a hurry our next discussion is all about what is
an index of an array? And also, how to access all the elements in an
Array. Let us directly get into the details of it.
chrome-distiller://831526f4-c023-46de-8de4-5b016f8c0d22_6db52dd5355e6d9238b5e142c25d40266eb629e1b63af8115cfc7be45f93be79/?title=Arrays+in+C%2… 5/14
4/25/23, 10:33 AM Arrays in C++ - Reader Mode
int temp[5];
Here temp variable name stores the address of the first element of an
array.
int empno[5]={1,2,3,4,5};
chrome-distiller://831526f4-c023-46de-8de4-5b016f8c0d22_6db52dd5355e6d9238b5e142c25d40266eb629e1b63af8115cfc7be45f93be79/?title=Arrays+in+C%2… 6/14
4/25/23, 10:33 AM Arrays in C++ - Reader Mode
In the above example to print the value 4 if I just use the below
instruction is enough.
cout<<empno[3];
Note: Array index is an integer starting from 0. And zero index will
always be given by the name of the array.
int x = 5;
chrome-distiller://831526f4-c023-46de-8de4-5b016f8c0d22_6db52dd5355e6d9238b5e142c25d40266eb629e1b63af8115cfc7be45f93be79/?title=Arrays+in+C%2… 7/14
4/25/23, 10:33 AM Arrays in C++ - Reader Mode
This is how values will be stored in the array. Now how do you
differentiate with the values? We will do it as,
A [0] = 2,
A [1] = 4
And so on.
cout << A;
Will it print the whole array? No, we have to print each and every
element one by one separately, whichever one you want you to print.
Or if we want to print all, then we can print them. Now we’ll explain to
you how to print all these elements one by one.
chrome-distiller://831526f4-c023-46de-8de4-5b016f8c0d22_6db52dd5355e6d9238b5e142c25d40266eb629e1b63af8115cfc7be45f93be79/?title=Arrays+in+C%2… 8/14
4/25/23, 10:33 AM Arrays in C++ - Reader Mode
int main(){
int A[5] = {2,4,6,8,10};
for(int i = 0; i < 5; i++){
cout << A[i] << endl;
}
}
In this code, first, we have declared an array of size ‘5’ and at the same
time, we have initialized it with some values. Next, we want to print all
the values of the array, so here we used the ‘for’ loop. We can also use
other loops also but here we have used the ‘for’ loop.
In the ‘for’ loop, we are starting from ‘0’ to ‘size – 1’ as array indexing
starts from ‘0’ in C / C++. Then inside ‘for’ loop, we just write a
statement ‘cout << A[i] << endl’. So, it will print the whole array. And as
we created an array of size ‘5’ then memory will allocate inside stack
as shown in the below image.
This is the memory allocation of the array. The iteration of for loop will
be as,
chrome-distiller://831526f4-c023-46de-8de4-5b016f8c0d22_6db52dd5355e6d9238b5e142c25d40266eb629e1b63af8115cfc7be45f93be79/?title=Arrays+in+C%2… 9/14
4/25/23, 10:33 AM Arrays in C++ - Reader Mode
So, by using loops we can access every element of the array. After
accessing we can perform various operations on the array-like,
addition subtraction, and more. So, this is how all the array elements
are displayed in C++.
#include <iostream>
using namespace std;
int main()
{
int A[5] = { 1, 2, 3, 4, 5 };
cout << "to print value 1\t" << A[0] << endl;
cout << "to print value 2\t" << A[1] << endl;
cout << "to print value 3\t" << A[2] << endl;
cout << "to print value 4\t" << A[3] << endl;
cout << "to print value 5\t" << A[4] << endl;
return 0;
}
Output:
chrome-distiller://831526f4-c023-46de-8de4-5b016f8c0d22_6db52dd5355e6d9238b5e142c25d40266eb629e1b63af8115cfc7be45f93be79/?title=Arrays+in+C%… 10/14
4/25/23, 10:33 AM Arrays in C++ - Reader Mode
In the above program, you came to know that using array index we can
get the value associated with that index but the program looks
awkward when I try to print all the elements in an array.
Yes, you’re right I have used five cout statements to print the five
elements of an array. This looks similar to declaring five new variables
and initializing them and printing them separately. To avoid multiple
cout and also to read multiple input from the user for an array we need
to use counter loops. Yes, you guessed it right counter loops are
nothing but for loop and for each loop.
Since we know the first index of an array from the array name and
also, we know the array is contiguous and hence index is also
contiguous from 0 to the size of an array-1. We can make use of
Counter loops for traversal purposes. Let’s modify the above example:
#include <iostream>
using namespace std;
chrome-distiller://831526f4-c023-46de-8de4-5b016f8c0d22_6db52dd5355e6d9238b5e142c25d40266eb629e1b63af8115cfc7be45f93be79/?title=Arrays+in+C%… 11/14
4/25/23, 10:33 AM Arrays in C++ - Reader Mode
int main ()
{
int empno[5] = { 1, 2, 3, 4, 5 };
cout << "printing all the elements using for loop" << endl;
for (int i = 0; i < 5; i++)
{
cout << "to print the element at index\t" << i << "\tvalue at index is\t" <<
empno[i] << endl;
}
return 0;
}
Output:
Summary.
chrome-distiller://831526f4-c023-46de-8de4-5b016f8c0d22_6db52dd5355e6d9238b5e142c25d40266eb629e1b63af8115cfc7be45f93be79/?title=Arrays+in+C%… 12/14
4/25/23, 10:33 AM Arrays in C++ - Reader Mode
will show you how you can declare and initialize an array with
different data types, we will look at
int A[5];
float B[5];
char C[5];
Here we have declared 3 types of arrays i.e. ‘int’, ‘float’, and ‘char’. So,
you can have any data for an array. The array can be of any type. And
if it is ‘Int’, all five elements are ‘int’ only. So similar data elements, all
are float, all are characters. Next, we can initialize these as,
So, this is declaration plus initialization. Now one more thing I will
show you, can we have an array of size ‘5’ and we mentioned only two
or four elements? Yes, we can mention as,
Now only ‘3.0’ and ‘3.5’ will be filled, all the other values will be
automatically zero. Next, can we create an array without giving size
and give the value? Yes.
So, what size of the array will be created? 4 Elements we have given.
So, the array of size 4 will be created.
chrome-distiller://831526f4-c023-46de-8de4-5b016f8c0d22_6db52dd5355e6d9238b5e142c25d40266eb629e1b63af8115cfc7be45f93be79/?title=Arrays+in+C%… 13/14
4/25/23, 10:33 AM Arrays in C++ - Reader Mode
1. Array in C++ is fixed size and we need to define the size of an array
while declaration.
2. Insertion and deletion of an array could be a costly operation.
Note: To know more about time complexity and more insight into an
array as a data structure please refer to the Data Structure and
Algorithm course by DotNetTutorials.
chrome-distiller://831526f4-c023-46de-8de4-5b016f8c0d22_6db52dd5355e6d9238b5e142c25d40266eb629e1b63af8115cfc7be45f93be79/?title=Arrays+in+C%… 14/14