array and their types

In this page we will discuss,

 

array

The array is a user defined data type. array is a collection of data types, in which we store the same type of value in a variable. In it, the value is stored as a series or sequence.

 

array vs normal variable

As we know, one variable can be stored one value at the same time, while using the array you can store a lot of value (which are of the same type) in a variable at the same time.

for example,

int x=5; // simple variable
int x[5] = {3,2,1,2,5}; //array-variable

such as,

array-in-cpp

As you can see, where normal variable is just storing a single value 5, an array-variable is storing the same type of 5 values (3,2,1,2,5).

  • In array stored values are called Elements.
  • To store and print values in an array variable, we use the loop while accessing these element we use their index value.


Type of array

array has two types in C++

 


single dimensional array

This is a simple form of array. You can also called list.


Declaration of single dimensional array

Here are 3 rule to declare a array-

  • Because array name is an identifier that means you can declare the array with any name (except for the reserve keyword).
  • When you declare an array, you have to declare its size too.
  • Declare the array with its name, size and data type. which are as follows.
storage_class data type array_name[size];

Example:-

int arr1[5];
char arr2[6]; 
float arr3[4];

Here arr1 , arr2 and arr3 is an identifier, as is the array of similer data type, so here arr1 digit, arr2 character and arr3 will store the value of decimal only.

When we declare an array , we tell the compiler how much value, we want to store in that array, i.e.

int arr1[5]; // will store 5 numeric value
char arr2[6]; // will store 6 character value
float arr3[4] // will store 4 decimal value


initialization of array

as we know, store values are called array elements in the array. kike a normal variables we can initialize array variables.

The syntex is as follows-

Storage-class data-type array_name[size]={list};

The list is representing the array element in which array elements are initialized using a comma in a sequence.

Example:-
Example, int arr1 variable has 6 digits and float arr2 variable will have 4 decimal type values store.

int arr1[6]={3,1,8,3,5,6};
float arr2[4]={2.3,4.3,1.5,6.7};

But it is not always necessary that all the elements of an array be initialized as–

int arr[6]={3,5,4,2};

array-initialization

As you can see, we have only taken 4 values but because of taking the size of array 6, the array has taken space in memory for 6 element (value). Which is its disadvantage.

Its program is given below, where memory size of an array variable has been detected using the sizeof operator-

In below program, array size 6 is given, whereas value has only been initialized to 4.

int arr[6]={3,4,5,2};

So the memory size of the array variable was 8 byte (2 byte of each element). But the array size is 6 so the size of the array variable will be 12 byte. because array size (6) x 2 byte = 12 byte

#include<iostream.h>
#include<conio.h>

void main()
{
   clrscr();
   int arr[6]={3,4,5,2};         // initialization of array elment with 6 size of array
   cout<<"size of int               : "<<sizeof(int);            // int take 2 byte
   cout<<"\nsize of int array: "<<sizeof(arr);             // now arr is int type so it take 6x2=12 byte
getch();
}
OUTPUT:-
size of int      : 2
size of int array: 12


accessing of single dimensional array element

The element of the array is accessed from its index value. For which we use the loop. You can understand this with the help of the program given below.

Example: – In this program we have given the size 5 of the array variable [5] so that it will store 5 input from the program user. In this 2 loop is used in which the first loop is stored by the user in the arr [5] while the second loop is used to print those values.

#include<iostream.h>
#include<conio.h>

void main()
{
  int i,arr[5];              // array variable declaration
  clrscr();
   for(i= 0; i<5; i++)
   {
      cout<<"Enter "<<i+1<<" Number: ";
      cin>>arr[i];                 // store 5 value at a time
   }

 cout<<"\nDisplay the array element:\n";
  for(i= 0; i<5; i++)
  {
     cout<<"\nDisplay "<<"arr["<<i<<"] Element: "<<arr[i];           // display values
  }

getch();
}
OUTPUT:-
Enter 1 Number: 2
Enter 2 Number: 4
Enter 3 Number: 3
Enter 4 Number: 1
Enter 5 Number: 4

Display the array element:

Display arr[0] Element: 2
Display arr[1] Element: 4
Display arr[2] Element: 3
Display arr[3] Element: 1
Display arr[4] Element: 4

 

things to know

  • In an array type variable, we can store multiple values at the same time, whereas in a simple variable we can store the same value at a time

Example-
we have to store marks of five subjects of a student. , Where in a simple variable we have to declare 5 int type variable (a variable for each subject), whereas in an int type array-variable, we just declare an array-variable (int subject [5]) by declaring all 5 subject marks can store in a single variable. Also there are two disadvantage of array.

  • An array variable is a collection of similar data type, which means we can store only one type of value at a time like we can only store the string in the char type, numeric type array in the int type array, that is,

Example:-
if We have to store a subject name with a student’s marks detail, so in this case we have to declare two array-variable variables, where the first array-variable marks and the second array-variable subject name will be stored. Can not store both together in a single variable. We can use the structure to solve this problem. This is explained further.

  • array-variable has a memory static type, that is, when we declare an array, we have to define its size too. Which we can not change at run-time. That’s a disadvantage.

Example:-
As we have declared an array-variable int subject [5] to store 5 subjects marks of a student, it can only store marks of those students who have 5 subjects i.e. if we have to store marks of such a student For those whose subject is greater than 5 or less, we need to modify the size of the array-variable in declaration, which will not be considered as a better method. Yes, if the subject is less than 5 and the array size is greater than 5 or less than 5, then in this case we can store 5 subject name, but in such a situation, memory wastage will be done.

But in Dynamic memory management in C++, we can define the size of an array according to the need of run-time. This is explained further about this.

 

Related Exercise

advance

more about array,


previous-continue and exit Statement

next-Structure in C++