Explain different types of Arrays

Estudies4you
Q24. Define Array. Explain different types of Arrays.
Answer:

Array
An array is a collection of similar datatype elements. It is capable of holding fixed values in contiguous memory locations. The general format of an array is,
a0
a1
a2
a3





an
Arrays are of three types, they are
1. One-dimensional array
2. Two-dimensional array
3.  Multi-dimensional array

1. One-dimensional Array
One-dimensional array is an array that store elements in contiguous memory locations which can be accessed using only one subscript.
Declaring One-dimensional Array
The process of allocating space for arrays in memory is called array declaration. The general format of declaring an array is as follows,
type array_name[size];
Here,
‘type’ specifies the data type of the array
‘array name? is the name of an array
‘size’ specifies the size of an array.
Example
int a[4];
Here, a is an array of 4 integers.
Initializing of One-dimensional Array
The process of assigning the values to an array is called array initialization. The general format of initializing an array is as follows,
type array_name[size] = {List of values separated by comma};
Here,
‘type’ specifies the data type of the array.
‘array_name’ is the name of an array.
‘size’ is an optional field which indicates the size of an array.

Example
An array can be initialized with anyone of the following ways, .
int a[4] = {21, 22, 23, 24};
float a[ ] = {12.5, - 22.4, 16.8, 11.4};
int a[3] = 12.4;
Accessing One-dimensional Array
Once the arrays are declared, every individual element can be accessed using the subscript which is an integer expression or an integer constant. The subscript specifies the position of element in the array this is also called index of array element. The array subscripts start at 0 and ends at “size of array — 1”.
The general format of accessing an array element is as follows,
array_name[i];
Here, variable ‘i’ refers to ith element of an array.
For example, a[0] refers to first element and a[1] refers to second element and so on. :

Program
#include<iostream.h>
#include<conio.h>
int main( )
{
int array1[10], array2[10], i, size;
cout<<“Enter the size of the array”;
cin>>size;
cout<<“Enter the elements of array1:” <<end1;
for (i = 0;.i < size; i+ +)
{
cin>>array | [i);
}
cout<<"Enter the elements of array2:”<<end1;
for(i = 0; i < size; i++)
{
cin>>array2[i];
}
cout<<“Addition of two arrays:”<<end1;
for(i = 0; i < size; i++)
{
cout<<array1[i] + array2[i] <<end1;
}
getch();
return0;
}

                
To Top