Home > DeveloperSection > Blogs > Array in c++

Array in c


Visual C++ Array 
Ratings:
0 Comment(s)
 627  View(s)
Rate this:

Array  in c++

In this blog, I’m explaining about Array in c++

 

An array  is a data structure  that contains a group  of elements. Typically  these elements are all of the same data type, such  as an integer or string.

Types of array

Single   dimensional array

Multi dimensional array

Single dimensional array

The single  dimensional array is the simplest array that contains only one row for storing  data.                                   It has single  set of  square bracket (“[]”).

Example

 #include "stdafx.h"

#include<iostream>

using namespace std;

using namespace System;

 

int main(array<System::String ^> ^args)

{

            int size;

            cout<<"Enter the size of array";

            cin>>size;

            int arr[100];

            for (int i = 0; i < size; i++)

            {

                        cout<<"Enter a  no.";

                        cin>>arr[i];

            }

 

            for (int i = 0; i <size; i++)

            {

                        int temp=0;

                        for(int j=i+1;j<size;j++)

                        {

                        if(arr[i]>arr[j])

                        {

                                    temp=arr[i];

                                    arr[i]=arr[j];

                                    arr[j]=temp;

                        }

                        }

           

            }

           

            cout<<"Numbers in sorted order";

            for(int i=0;i<size;i++)

                        cout<<arr[i]<<endl;

           Console::ReadKey(0);

  

}

 Multi dimensional array

The multi dimensional array contains more than one row  to store data on it. It can be two dimensional array or three dimensional array or more.

Example:

 

int main(array<System::String ^> ^args)

{

           

            //Declaring  multi dimensional  array

            char arr1[3][4];

 

            cout<<endl;

            cout<<"\n Enter element for Row and column";

            for(int i=0;i<3;i++)

            {

                        for(int j=0;j<3;j++)

                        {

                                   

                                    cin>>arr1[i][j];

                        }

            }

            cout<<"\n All  the element of  array  is:\n";

            for (int i = 0; i <3; i++)

            {

                        cout<<i+1;

                        for (int j = 0; j< 3; j++)

                        {

                                    cout<<arr1[i][j];

                        }

                        cout<<endl;

            }

 

            Console::ReadKey(0);

  

}

 

 


Don't want to miss updates? Please click the below button!

Follow MindStick