Arrays
Before reading this tutorial, you should have knowledge of loop statements.
An array is a collection of several data items of the same data type. It consists of several contiguous memory locations storing data. It removes the cumbersome task of defining separate variables for each data item. A single variable can be used to store one or more data items. For example, if you want to store ages of 10 students of a class, you can easily declare an array of integer type of size 10 instead of declaring 10 variables of integer type each storing age of a student. The general form of the single dimension array is:-
type variable_name[size];
The type is the base type of the array and which is the type of each of the element of the array. The size is the no of the elements in the array. The variable_name is the name of the identifier holding the array. For example,
int age [10];
The age is an array of type integer whose size is 10. It can hold 10 data values. The specific element in the array can be accessed using an index. An index is the offset from the first element of the array. For example, first element has an index of 0 while the third element has an index of 2. The integer age has elements from age[0] to age[9]. Here is a program which illustrates the working of arrays.
#include<iostream>
|
#include<iostream>
using namespace std;
int
main ()
{
int
age[10];
int
i,sum=0, avg=0;
int
max=0,min=100;
for(i=0;i<10;i++)
{
cout << "Enter the age of the student " << i+1 <<endl;
cin >> age[i];
}
for(i=0;i<10;i++)
{
sum=sum+age[i];
if(age[i]>max)
{
max=age[i];
}
if(age[i]<min)
{
min=age[i];
}
}
avg=sum/10;
cout << "Average age of the students of the class : " << avg << endl;
cout << "Maximum age of the student of the class : " << max << endl;
cout << "Minimum age of the student of the class : " << min << endl;
return(0);
}
The result of the program is:-
In the program the array is declared by the statement
int age[10];
age is the identifier of the array which is of type integer and whose size is 10. In the for loop user is asked to enter the age of the student, the age is stored in the element of the array by the statement
cin >> age[i];
here i is the index of the array. The index starts from 0. In the next for loop, average age, minimum age and maximum age of the students is calculated. Each element is accessed by index i. The sum of the elements of the array is calculated by the statement
sum=sum+age[i];
age[i] is the (i+1)th element of the array. The maximum age is calculated by the if statement
if(max>age[i])
{
max=age[i];
}
The minimum age is calculated by the another if statement
if(min<age[i])
{
min=age[i];
}
The average is calculated by the statement
avg=sum/10;
The total no bytes used to store an array is equal to
Total bytes=size of(base type)×size of array.
Initializing Arrays
The arrays can be initialized by giving the initial values in a list and enclosed in curly brackets, which is placed after the equal sign which is put after the declaration of the array. For example,
int age[5]={12,13,10,24,15};
elements of the array have values 12, 13, 10, 24, 15. The elements age[0] has value 12 and age[4] will have 15. If the elements of the array are not initialized then they take some garbage value before they are assigned to some specified value. There is also another way of initializing all the elements of the array to be zero. For example,
int age[5]={0};
will initialize all the elements of the array age to be zero. If you want some of the elements to be initialized with specific value and rest of the values to be zero then here is an example.
int age[5]={12,13};
The elements age[0] and age[1] will be initialized to 12 and 13 respectively and rest of the elements will be initialized to zero.
Null Terminated Strings
The most common use of one dimensional array is for character strings. The null terminated string is a character array with a null character at the end. The characters form the string with a null character indicating the termination of the string. A null terminated string can be declared as
char name[10];
it will hold 10 characters with a null at the end. The size of the array can be more than the length of the array. The array can be initialized as
char name[10]={'j','e','s','u','s'};
The first 5 elements are initialized and rest elements are null characters. Here is a program which calculates the length of the string.
#include<iostream>
using namespace std;
int main()
{
char name[15];
int i=0;
cout << " Enter your name " << endl;
cin >> name;
while(name[i]!='\0')
{
i++;
}
cout << "Lenght of the name is : " << i << endl;
return(0);
}
The result of the program is
The character array is declared by the statement
char name[15];
The length of the name can be at most 15. The arrays is assigned by taking the input from the user by the statements
cout << " Enter your name " << endl;
cin >> name
Variable name is assigned the string entered by the user. Each element of the array is referenced and checked whether it is null or not by the statement
while(name[i]!='\0')
Until a null character is not encountered variable i is incremented by the statement
i++;
When a null character is encountered while loop is terminated and length is printed by the statement
cout << "Length of the name is : " << i << endl;
Multidimensional Arrays
The multidimensional arrays are arrays of arrays. The general form of a multidimensional array is: -
type variable_name[Size1][Size2]..[Size n];
The two dimensional array can be declared as
int age[2][5];
this array has two rows and 5 columns. The three dimensional array can be declared as
int age[2][3][5];
When an element of the array of n dimensional is referenced it uses n index values. For example first element of a two dimensional array declared above is referred as age[0][0] as the index starts from zero and last element is referred as age[1][4]. Here is a program which calculates the average of the elements of the row of the two dimensional array.
#include<iostream>
using namespace std;
int main ()
{
int age[2][5]= { {12,13,14,15,15}, { 12,16,17,13,12}};
int i,j;
int sum=0,avg=0;
for(i=0;i<2;i++)
{
for(j=0;j<5;j++)
{
sum=sum+age[i][j];
}
avg=sum/5;
cout << "Average of the elements of the row " << i+1 << " is " << avg << endl;
sum=0;
}
return(0);
}
The result of the program is: -
The program initializes and declares the two dimensional array by the statement
int age[2][5]= { {12,13,14,15,15}, { 12,16,17,13,12}};
Elements of the first row and second row are initialized as {12,13,14,15,15} and
{ 12,16,17,13,12} respectively. The elements are accessed using the statement as
sum=sum+age[i][j];
age[i][j] refers to the element of row i and column j. This statement calculates the sum of the elements of the row. After the sum of the elements of a row is calculated then average is calculated by the statement.
avg=sum/5;
sum is again initialized to zero. Then again the same process repeats for the second row.
After an introduction of arrays, let us move on to discuss pointers.
Go to the previous lesson or proceed to the next lesson