Arrays: When there is a need to use many variables, then there is a big problem because we will Conflict with the name of variables. So that in this situation where we want to Operate on many numbers, we can use array. The Number of Variables also increases the complexity of the Program so that we use Arrays.
Arrays Set of Elements having the same data type, or we can Say that Arrays are Collections of Elements having the same name and same data type. But Always Remember Arrays Always started From their index value, and the index of the array starts From 0 to n-1.
If we want to Access the 5th Element of the array, we will use the 4th Element Because Arrays are Start From 0, and arrays always stored in Continuous Memory Locations. The Subscript of array Elements identifies the Number of Elements and Types of the array.
Arrays in C++ must be defined before they can be used to store information. Arrays are defined in the same manner as that of basic types except that each array name must be associated with its size specification. Array names must follow the same conventions as that of an identifier. The general form for defining an array is
type array_name[size];
Here, type specifies the data type of each element of the array, followed by the name of the array array_name. The size specifies the maximum number of elements that can be stored in an array. The size must be an integer constant greater than 0.
For example, the statement.
Defines an int array named marks that can store marks of 10 students in a subject. Since only a single subscript is used, so it represents a one-dimensional array. The storage space allocated to the above array is equal to the maximum number of elements (i.e., 10) multiplied by the size of the data type used, in our example int (i.e., 2). So, at the time of array definition, a total of 20 bytes are allocated for the above array. All the array elements are stored in contiguous memory locations (i.e., one after the other).
Each box represents an element of an array of marks. Each array element is represented by an array name followed by a unique index or subscript that specifies the position within the array. The array elements are numbers starting from 0. As there are 10 elements in this array, so the index number ranges from 0 to 9.
Some more examples of array definitions are given below.
float salary[5];
It defines an array salary containing 5 elements of float type,
double sale[20] ,temp[10];
It defines two arrays, sale and temp, containing 20 and 10 elements respectively of double type.
It is sometimes convenient to define an array size in terms of symbolic constants rather than a fixed integer quantity. This makes it easy to modify the program by simply changing the value of the symbolic constant.
Let us consider an example,
#include<iostream.h> #define n 20 int main() { int s[n]; //valid as value of n is pre-defined ........................... ........................... }
Here, n is used as a symbolic constant which is defined using preprocessor directive #define. It is initialized to a value 20 so the size of the array s is 20.
We’ll be covering the following topics in this tutorial:
Access Array Elements
Once the array is created, you can access an array element by using the name of the array followed by an index enclosed between a pair of square brackets. The index or subscript of an array indicates the position of an element in the array. The index of first element in the array is always 0 (zero), the second element has an index 1 and so on. The index of last element is always one less than the number of elements in the array. The syntax for accessing an array element is
array_name [index];
Here, index is an integer constant or an expression that evaluates to an int.In the above Figure, the first element of the array is marks [0],the second element is marks [1] and last element is marks[9].
Similarly, if the user needs to access the 6th element of array marks, he has to write marks [5]. Thus, the individual elements of array marks can be referred as marks [0], marks[1], marks[2], marks[3] ,……., marks[9].
One should make sure that array indexes must be between 0 and number of elements minus 1.
Similarly, to assign value to an element of an one dimensional array, use the following syntax
array_name [index] = value;
For example, in order to assign a value 25 to an element of array marks at index 1, you need to write
marks[1] = 25;
#include <iostream.h> #include <conio.h> int main() { clrscr(); int a[5]; //defines 5 elements array cout<<"Enter elements of an array : "; for(int i=0;i<5;i++)//reading array elements { cin>>a[i]; } for(i=0;i<5;i++)//displaying array elements { cout<<"a["<<i<<"]="<<a[i]<<"\n"; } getch(); return 0; } Output : Enter elements of an array : 5 10 -2 3 12 a[O] = 5 a[l] = 10 a[2] = -2 a[3] = 3 a[4] = 12
Explanation: The above program first reads the elements of an array and then prints the values stored in them. The a[i] corresponds to the (i-1)th element of an array a. The loop variable i in for loop starts from 0 (representing index for first element) to 4 so as to input or display 5 elements of array a.
The statement cin>>a[i]; in the for loop inputs various elements of array a corresponding to each value of index i, which are then displayed using another for loop.
When i=0 then in the statement cin>>a [i]; i is replaced with 0 and value for 1st element of array is inputted. Similarly for other elements.
Initialization of Array
Definition of an array doesn’t automatically results in the initialization of the array elements. It only allocates contiguous memory to an array with each element assigned some garbage value. We can initialize an array definition by following the definition with an equal to (=) sign followed by comma separated list of values enclosed in curly braces. The order of assignment of values to array elements will be the same as they appear in the initialization list.
The syntax of array initialization is
type array_name[size] = {valuel,value2, .... };
Let us consider an example,
int a[3]={21,31,4};
The above statement defines an integer array named a of size 3 and also initialize the elements to 21, 31 and 4. The elements are assigned in the order of their appearance
So 21 is assigned to a[0]
31 is assigned to a[1]
and 4 is assigned to a[2]
The size of an array can be omitted during initialization. The compiler automatically determines the array size by the number of elements enclosed in curly braces.
So, the following statement is valid.
int[]={21,31,4};
In, the above statement, the size of array is considered as 3, as there are three values specified in the initialization list. This feature of not specifying the size of array provides the benefit that on adding more elements during initialization, we· don’t need to change the size specification of an array.
If the size of an array is specified, the number of elements specified in the initialization list must not be greater than that size. Otherwise, the compiler generates an error. However, if the size of an array is greater than the number of elements specified then the remaining elements are initialized to 0. For example :
int a[3]={4,6,2,7};//Invalid int a [4]={14,16};//Valid
The first statement is invalid as size of array is less than the number of values specified in initialization list. The second statement is valid and the elements are assigned as follows,
a[0] = l4,a[1] = l6,a[2] = 0,a[3] = 0
If the user wishes to assign 10, 20 and 30 values to the second, third and fourth element of an array and skip the first element using the following statement,
int a[4]={,10,20,30}; // is illegal
#include<iostream.h> #include<conio.h> int main() { clrscr(); int x[6] = {l0,ll,-5,20,15,18}; cout<<"Display even elements\n"; for(int i;0;i<6;i++) { if(x[i]%2 ==0) cout<<x[i]<<"\t"; } getch(); return 0; }
Explanation: In the above program, we first define and intialize an array x and then display the even numbers stored in it. To check whether a given number is even or not, divide the number (which is represented by x [i]) by 2 and check the remainder. If the remainder is 0 then the number is even which is then displayed.
The Various types of Array those are provided by C++ as Follows:
1. Single Dimensional Array
2. Two Dimensional Array
3. Three Dimensional array
4. Character Array or Strings.
A dimensional is used representing the elements of the array for example
int a[5]
The [] is used for dimensional or the sub-script of the array that is generally used for declaring the elements of the array For Accessing the Element from the array we can use the Subscript of the Array like this
a[3]=100
This will set the value of 4th element of array. So there is only the single bracket then it called the Single Dimensional Array. This is also called as the Single Dimensional Array
Two Dimensional Array or the Matrix
The Two Dimensional array is used for representing the elements of the array in the form of the rows and columns and these are used for representing the Matrix A Two Dimensional Array uses the two subscripts for declaring the elements of the Array
Like this int a[3][3]
So This is the Example of the Two Dimensional Array In this first 3 represents the total number of Rows and the Second Elements Represents the Total number of Columns The Total Number of elements are judge by Multiplying the Numbers of Rows * Number of Columns in The Array in the above array the Total Number of elements are 9
Multidimensional or the Three Dimensional Array :The Multidimensional Array are used for Representing the Total Number of Tables of Matrix A Three dimensional Array is used when we wants to make the two or more tables of the Matrix Elements for Declaring the Array Elements we can use the way like this
int a[3][3][3]
In this first 3 represents the total number of Tables and the second 3 represents the total number of rows in the each table and the third 3 represents the total number of Columns in the Tables So this makes the 3 Tables having the three rows and the three columns
The Main and very important thing about the array that the elements are stored always in the Contiguous in the memory of the Computer
Character Array of String: Like an integer characters are also be in the Array The Array of Characters are called as the Strings They are Generally used for representing the Strings Always Remember that a String is Terminated with the \0 or Null Character.
There are the built in string Operation those are provided by the C Language in the String.h Header file Like
1) strlen: For Getting the Length or Total Numbers of Characters in String.
2) strconcat: This is Used for Joining the two Strings or This function is used for Concatenating the two Strings.
3) strrev: This Function is used for obtaining the Reverse of the String.
4) strcmp: This Function is used for Comparing the Two Strings and it gives us the Result as follows after comparing the Two Strings.
it Returns us + value if String1 is Greater than String2 it Returns us the - Value if String1 is Less than String2 it Returns us the 0 if string1 is Equals to String2
Like The array elements of Integer Types The Character array also are the Single Dimensional or The Two Dimensional Array.
Single Dimensional Array The Single Dimensional array are used for creating the Number of characters like
char name[10]
in this we can use the 10 characters on the name variable Means we can give the name as 10 characters long
Two Dimensional array :-When we talk about the Two Dimensional of the Character array The first Subscript of the array if used for representing the Total Numbers of Strings and the second Subscript is used for defining the length of the array Characters in the String like This
char name[5][10]
It declares the 5 names and each having the characters up to 10 So the First Subscript is used for defining the total number of the Strings and the Second is used for Length of the Character.