Home > C++ > Arrays in C++

In C++ arrays are data structure that stores fixed size data of same type in sequential order. In general a C++ array is considered as collection of variables of same data type. Arrays are very helpful in storing same type of data. Instead of creating individual variables for each part of data we can create a single array to store those parts of data.
E.g.
To store a data which has 100 parts. Or we can say that we need to store 100 numbers. We need to create hundred 100 individual variables for each number like:

num1, num2, num3 …. num100. But we can create a single array to store these 100 numbers as:
num[100];

Declaration of a c++ array: –

Syntax for an array declaration is:

Datatype array-Name [array-Size];

Datatype  It indicates which type of data array will hold.
Array-Name is the name of array following the same rules as for Naming a variable.
[] these are used in declaring arrays.
Array-size is the size of array i.e. up to how many values or how much of data to be stored in array.
E.g.

int arr [100];

This above declaration can be used to store up to 100 values of integer data type in an array named arr.

Float myArr [7];

This above declaration of array named myArr can store 7 or less floating type values in it.

Array Initialization: –
You can initialize an array just like a simple variable. Here are some ways to initialize an array.

int arr[5]={10,20,30,40,50};

This initialization will assign values to each index as:

  • arr[0] = 10;
  • arr[1] = 20;
  • arr[2] = 30;
  • arr[3] = 40;
  • arr[4] = 50;
int arr[5]=50;

This initialization will initialize all values to 50 as:

  • arr[0] = 50;
  • arr[1] = 50;
  • arr[2] = 50;
  • arr[3] = 50;
  • arr[4] = 50;
int arr[]={10,20,30,40,50};

This initialization will automatically decide size of array based on given values and assign values to each index as:

  • arr[0] = 10;
  • arr[1] = 20;
  • arr[2] = 30;
  • arr[3] = 40;
  • arr[4] = 50;

Accessing Elements of c++ array: –

We can access any value from array using its index with the name of array as:

Array-Name[index];

Note: – Every array’s starting index is zero and its last index is Array-Size -1.
So our above declared array

int arr [100]; Will store first value at arr [0] and last value (100th) at arr [99];

Similarly,

 myArr [7]; Will store first value at myArr [0]; and last at myArr [6];

We can use arrays just like simple variables with cout and cin.
E.g.

#include "stdafx.h"
#include"iostream"

using namespace std;

int main()
{
     int arr[5];
     cout << "Please enter value: ";
     cin >> arr[0];
     cout << "Please enter value: ";
     cin >> arr[1];
     cout << "Please enter value: ";
     cin >> arr[2];
     cout << "Please enter value: ";
     cin >> arr[3];
     cout << "Please enter value: ";
     cin >> arr[4];
     cout << endl;
     cout << "**********Accessing Array Value using Index Numbers*********"<<endl;
     //accessing 1st Element
     cout << "Value at 0th index is :" << arr[0] << endl;
    //accessing 2nd Element
     cout << "Value at 1st index is :" << arr[1] << endl;
    //accessing 3rd Element
     cout << "Value at 2nd index is :" << arr[2] << endl;
    //accessing 4th Element
     cout << "Value at 3rd index is :" << arr[3] << endl;
    //accessing 5th Element
     cout << "Value at 4th index is :" << arr[4] << endl;
}

Output: –
c++ array example

I/O in arrays using loops: –

Most of the time we will use for loop to get input from a user to array and to show output to a user.
Below example will explain how can we do that.

 

#include "stdafx.h"
#include"iostream"

using namespace std;

int main()
{
     int arr[5];
     cout << "***Input in Array using Loop***"<<endl;
     for (int i = 0;i < 5;i++)
     {
          cout << "Plese Enter Value : ";
          cin >> arr[i];
     }
     cout << "***Input from Array using Loop***"<<endl;
     for (int i = 0;i &lt 5;i++)
     {
          cout << "Value at index "<<i+1<<" is  : ";
          cout << arr[i]<< endl;
     }
}

Output: –
arrays

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: