Arrays in C++.
Purpose of Array.
Declaration of an Array.
Accessing Array Elements.
Example CPP programs using Array.
Multi-dimensional Array.
Advantages and Disadvantages of an Array.
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
Arrays in C++
1. ARRAYS IN C++
1 | P a g e
ARRAY:
Arrays are used to store multiple values in a single variable, instead of declaring separate
variables for each value.
They are used to store similar type of elements; the data type must be same for all the
elements.
They can be used to store collection of primitive data types such as int, float, double, char, etc.
PURPOSE OF ARRAY:
We can use normal variables (x, y, z) when we have a small number of objects, but if we want
to store many instances, it becomes difficult to manage them with normal variables. The idea of
an array is to represent many instances in one variable.
DECLARING AN ARRAY:
• Define the variable type.
• Specify the name of the array followed by square brackets.
• Specify the number of elements it should store/array size.
There are various ways in which we can declare an array. It can be done by specifying its type
and size, by initializing it or both.
1. ARRAY DECLARATION BY SPECIFYING SIZE:
2. ARRAY DECLARATION BY INITIALIZING ELEMENTS:
3. ARRAY DECLARATION BY SPECIFYING SIZE AND INITIALIZING ELEMENTS:
int array [10];
int array [10];
int array [6] = {10, 20, 30, 40}
OR
int n = 10;
int array [n];
2. ARRAYS IN C++
2 | P a g e
NOTE:
• The size and type of arrays cannot be changed after its declaration.
• The arraySize must be an integer constant greater than zero and type can be any valid
C++ data type.
SYNTAX:
FOR EXAMPLE,
• Here, we declared an array, value, of floating-point type and size 5. It can hold 5
floating-point values.
• We have now declared a variable that holds an array of four strings.
ACCESSING ARRAY ELEMENTS:
• Array elements are accessed by using an integer index.
• Array index starts with 0 and goes till size of array minus 1.
• An element is accessed by indexing the array name.
• This is done by placing the index of the element within square brackets after the name
of the array.
dataType arrayName [arraySize];
float value [5];
string cars [4] = {"Vitz", "BMW", "Toyota", "Surf"};
3. ARRAYS IN C++
3 | P a g e
EXPLANATION:
• Suppose an array “box” is declared, as below. The first element is box [0], second
element is box [1] and so on.
• Arrays have 0 as the first index, not 1. In this example, box [0] is the first element.
• If the size of an array is n, to access the last element, (n-1) index is used. In this
example, box [4] is the last element.
• The number of values between braces { } cannot be larger than the number of elements
that we declare for the array between square brackets [ ].
• In example above;
box [0] is equal to 10
box [1] is equal to 20
box [2] is equal to 35
box [3] is equal to 15
box [4] is equal to 25
box [0] box [1] box [2] box [3] box [4]
box [0] box [1] box [2] box [3] box [4]
10 20 35 15 25
4. ARRAYS IN C++
4 | P a g e
EXAMPLE PROGRAM-1:
A program printing the number of pancakes in a carton/box.
CODE FOR THE PROGRAM:
OUTPUT:
#include <iostream>
using namespace std;
int main()
{ int box [5] = {10,20,35,15,25};
cout<< "Box 1 has "<<box[0]<<" pancakes" <<endl
<<"Box 2 has " <<box[1] <<" pancakes" <<endl
<<"Box 3 has " <<box[2] <<" pancakes" <<endl
<<"Box 4 has " <<box[3] <<" pancakes" <<endl
<<"Box 5 has " <<box[4] <<" pancakes";
return 0;
}
5. ARRAYS IN C++
5 | P a g e
EXAMPLE PROGRAM-2:
C++ program to store and calculate the sum of 5 numbers entered by the user using arrays.
CODE FOR THE PROGRAM:
OUTPUT:
MULTIDIMENSIONAL ARRAYS:
• Multidimensional arrays can be described as "arrays of arrays".
• Multidimensional arrays are not limited to two indices (i.e., two dimensions). They can
contain as many indices as needed.
• For example, a bi-dimensional array can be imagined as a two-dimensional table made
of elements, all of them hold same type of elements.
SYNTAX:
data_type array_name[size1][size2]....[sizeN];
6. ARRAYS IN C++
6 | P a g e
EXPLANATION:
Table represents a bi-dimensional array of 3 per 5 elements of type int.
For example, the way to reference the second element vertically and fourth horizontally in an
expression would be:
ADVANTAGES OF AN ARRAY IN C/C++:
1. Random access of elements using array index.
2. Use of less line of code as it creates a single array of multiple elements.
3. Easy access to all the elements.
4. Traversal through the array becomes easy using a single loop.
5. Sorting becomes easy as it can be accomplished by writing less line of code.
DISADVANTAGES OF AN ARRAY IN C/C++:
1. Allows a fixed number of elements to be entered which is decided at the time of
declaration.
2. Insertion and deletion of elements can be costly since the elements are needed to be
managed in accordance with the new memory allocation.
Table [1][3]