Arrays Notes
Arrays Notes
PU College
Chapter-11
ARRAYS
➢ Introduction
• In everyday life we commonly group similar objects into units.
• In computer languages we also need to group together data items of the same type.
• The most basic mechanism that accomplishes this in C++ is the array.
• Arrays can hold a few data items or tens of thousands.
• The data items grouped in an array can be simple types such as int or float, or they can be user-
defined types such as structures and objects.
• Arrays exist in almost every computer language. Array in C++ are similar to those in other
languages and identical to those in C.
➢ Array Fundamentals:
• An array is collection of elements where all the elements are same data type under the same
name.
• The elements are numbered as 0, 1, 2….n-1.
• These numbers called as indices or subscripts.
• These numbers are used to locate the positions of elements within the array.
• If a is the name of the array, the elements can be directly accessed as a[0], a[1], a[2],……, a[n-1].
o a[0] is the name of the first element present at position 0.
o a[1] is the name of the second element present at position 1 and so on.
o In general, a[i] is the element present at position i.
• An array can be represented as
a 10 20 30 40 50
0 1 2 3 4
• In the above array, a is the name of the array,
o a[0] contains the element 10
o a[1] contains the element 20
o a[2] contains the element 30
o a[3] contains the element 40
o a[4] contains the element 50.
• The diagram represents a region of the memory, because the array elements are always stored in
contiguous sequence.
1|Page
Chapter 11- Arrays I PUC, Surana Ind. PU College
• The method of numbering the ith element with index i-1 is called as zero- based indexing.
• That is, the element is always same as the number of “steps“ from the initial element a[0] to that
element. For example, the element a[3] is 3 steps from the element a[0].
• In C++, the subscripts always start with 0. For example, if b is the name of the array b[0], b[1],
b[2] ......... are the elements and 0, 1, 2,… ..... are the subscripts.
➢ Types of Arrays:
• There are 3 types of arrays.
i. One-dimensional array
ii. Two-dimensional array
iii. Multi-dimensional array
✓ Two-dimensional array
• It is an array in which each element is accessed using 2-subscripts.
• The subscripts represent the position of the element in the array.
✓ Multi-dimensional array
• A multi-dimensional array is an array of n-dimensions.
• In other words, an array of arrays is called a multi dimensional array.
• A one dimensional array of one dimensional array is called a two dimensional array; a one
dimensional array to two dimensional array is called three dimensional array and so on.
➢ One-Dimensional Array:
• It is an array in which each element is accessed by using only one subscript.
• The only one subscript represents the position of the element in array.
• Like other variable, the array should also be defined before it is used to store elements.
• An array can be defined by specifying the data type of the elements followed by name of the array
and the number of elements that can be stored in the array.
• The number of elements that can be stored in the array is called the size of the array.
• The size of the array must be a constant or an expression that evaluates to a constant, and should
also be an integer.
• The size should be enclosed within square brackets.
2|Page
Chapter 11- Arrays I PUC, Surana Ind. PU College
4|Page
Chapter 11- Arrays I PUC, Surana Ind. PU College
• Example:
o The total size of the char array required is 1 * 5 = 5 bytes.
o The total size of the int array required is 2 * 3 = 6 bytes.
Practical Program 17: To find the sum and average of n number of the array:
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main( )
{
int a[50], i, n, sum;
float avg;
clrscr( );
cout<<"How many elements?"<<endl;
cin>>n;
cout<<"Enter the elements:";
for(i=0; i<n; i++) Output:
cin>>a[i]; How many elements? 5
sum = 0; Enter the elements:
for(i=0; i<n; i++)
sum = sum + a[i]; 10 20 30 40 50
avg=(float) sum / n; Sum = 150
cout<<"Sum = "<<sum<<endl; Average = 30
cout<<"Average = "<<avg<<endl;
getch( );
}
Column-number
0 1 2 3 …. n-1
0
Row-Number
1 a[2] [3]
2 * 2 → Row Number
3 → Column Number
….
m-1
5|Page
Chapter 11- Arrays I PUC, Surana Ind. PU College
➢ Declaration of Two -Dimensional array:
• A Two-dimensional array can be defined by specifying the data type of the elements followed by
name of the array followed by the number of rows (i.e. row-size) and number of columns (i.e.
column-size)in the array.
• The rows-size and column-size of the array must be constant are expressions that evaluates to an
integer.
• The rows-size and column-size should be separately enclosed within square brackets.
• Syntax: data_type array_name[row size] [column_size];
Row- 2
a[1][0] = 4 a[1][1] = 5 a[1][2] = 6 0 1 2 3
• Note: If the values are missing in an initialize, they are 1 4 5 6
automatically set to 0.
• Example: int b[2][3] = { Column - 3
{ 1, 2 } 0 1 2
{3}
Row- 2
}; 0 1 2 0
• It will initialize the first two elements to the first row, the next 1 3 0 0
element to the second row. The remaining elements are automatically set 0.
b[0][0] = 1 b[0][1] = 2 b[0][2] = 0
b[1][0] = 3 b[1][1] = 0 a[1][2] = 0
**************
7|Page