My Bookmarks

  • No bookmark found

Most Active Members

Array and Its types in C-Cpp2 min read

An array is a collection of variables of the same type that are referenced by a common name.

Array can be of 3 types:

  1. One-dimensional array
  2. Two-dimensional array
  3. Multi-dimensional array

For simplicity, we can think of an array a fleet of stairs where on each step is placed a value (let’s say one of your friends). Here, you can identify the location of any of your friends by simply knowing the count of the step they are on.

Remember: “Location of next index depends on the data type we use”.

Illustration of array

NEED OF ARRAY

Arrays are very much useful in a case where many elements of same types need to be stored and processed.

The element numbers in [] are called subscripts or indices.c++ array index numbering starts from 0.

  • Arrays allow random access of elements. This makes accessing elements by position faster.
  • Array have better cache locality that can make a pretty big difference in performance.

ONE-DIMENSIONAL ARRAY

A one-dimensional array is a group of elements having the same data-type and same name. Individual elements are referred to using common name and unique index of the elements.

SYNTAX:

array-name [size] ;

Where size specifies number of elements and array is the with which the array will be referenced.

DECLARATION OF ARRAY

The general form of any array declaration is as shown below:

 type array-name [size]; 

where type declare the base type(data type of array elements) .The size must be an integer value greater than 0.

Example;

 int marks[50]; 

In the following examples we will declare an array named marks which will store marks of 50 elements of integer type.

INITIALIZATION OF ARRAY

An array can be initialized in many ways:

  1. Run-time initialization: We can specify the array value at the run time using loops.
int i, num[5];
for(i=0;i<5;i++)
{
   cin>>num[i];
}

2) Compile-time initialization: It means specifying the value inside the program. It can be done in 2 ways:

a. By specifying size: When size of array is specified in array declaration. We can add only limited number of elements in it.

int num[5] ={2,4,6,8,10};

b. By not specifying size: can be declared without specifying initial size. We can add elements to each index. We can add unlimited number of elements in it as size is not specified.

int num[ ] ={2,4,6,8,10};

Or it can be initialized by other way:

num[0]= 2;
num[1]= 4;
num[2]= 5;
num[3]= 8;
num[4]= 10;

Now, let’s see a program to store marks of 5 students in an array.

void main(){
int i, marks[5];
cout<<”Enter marks of students”;
for(i=0;i<5;i++)
{
   cin>>marks[i];
}
for(i=0;i<5;i++){
cout<<”\n Marks of”<<i+1<<”student is”<<marks[i];
}
}

OUTPUT

Enter marks of students
60
98
76
59
90
Marks of 1 student is 60
Marks of 2 student is 98
Marks of 3 student is 76
Marks of 4 student is 59
Marks of 5 student is 90

If you liked my article please follow me on @priya123 to get latest post and article by me.

Bookmark(0)

Add a Comment

Your email address will not be published. Required fields are marked *