array in C++ with examples

In this page we will discuss C++ array,


C++ array

The array is a user-defined data type which is a collection of similar data types, such as,

cpp-array with diagram

we can not store different data type in the array at a time. wherein the value is stored as a series or sequence.

why we need of array ?

let’s understand with a difference,

Difference between array and normal variable in C++

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,

using a normal variable

int x=5; // normal variable

difference-between-array-and-normal-array-cpp

while using array variable

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

why we use array in cpp

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

Remember

  • 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.
  • array name always refer the base address of its first element.

Type of array in C++

array has two types in C++

  • single dimensional array
  • multi-Dimensional array

single dimensional array in C++

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

declaration of a single dimensional array in C++

Because array name is an identifier that means you can declare the array with any name (except for the reserved keyword). 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];  // will store numeric
char arr2[6]; // will store character
float arr3[4]; // will decimal character

Here arr1, arr2 and arr3 are identifiers, in which arr1 will store numeric, arr2 character and arr3 will store only decimal value

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 in C++

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

The syntax 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
int arr1 variable has 6 digits and float arr2 variable will have 4 decimal type values store.

int arr1 variable में 6 number हैं और float arr2 variable में 4 decimal type value ही स्टोर होंगे-

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,

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

meaning,

array-initialization-in-cpp-1

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 the memory size of an array variable has been detected using the sizeof operator-

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

calculating array-size in C++

Its program is given below, where sizeof() operator is used to find the size of array-variable .The memory size of an array variable is determined using the sizeof operator

In the program below, the array size is given 6, while only 4 values are initialized.

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

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

Find out C++ array size in own sytem.

#include<iostream>
using namespace std;

int main()
{
   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</strong>
   cout<<"\nsize of int array: "<<sizeof(arr);  // now arr is int type so it take 6x2=12 byte

return 0;
}

OUTPUT

size of int      : 2
size of int array: 12

accessing of a 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 of Single dimensional array in C++

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.

OUTPUT

#include<iostream>
using namespace std;
int main()
{
  int i,arr[5];              // array variable declaration

   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
   }

return 0;
}

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

Disadvantages of Array in C++

  • In the  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. , wherein 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 disadvantages to the array.

  • an array variable is a collection of similar data type, which means we can store only one type of value at a time as 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 the 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 names, 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- C++ exit statement

Next- structure in C++

Like it?

Leave a Reply

Your email address will not be published.