Types of Arrays

In this article, we are going to learn about the types of arrays present in cpp.

In C++, there are generally three types of arrays present. They are

  • One-dimensional array
  • Two-dimensional array
  • Multidimensional array

ONE-DIMENSIONAL ARRAY

A one-dimensional array in C++ is used to store the elements of the same data type in one dimension.

In the above example, all the elements are stored in contiguous memory one after another in one dimension.

Syntax of the one-dimensional array is.

Now, let’s take a look at a sample program implementing a one-dimensional array.

#include <iostream>
using namespace std;
int main(){
  int numbers[5] = {7, 5, 6, 12, 35};
  cout << "The numbers are: ";
  for (int i = 0; i < 5; ++i) {
    cout << numbers[i] << "  ";
  }
  return 0;
}

Output:

TWO-DIMENSIONAL ARRAY

A two-dimensional array is nothing but an array in which elements in the array can be accessed using two indices, the first index mentioning the row and the second index mentioning the column. In this type of array, the elements in the array are stored in 2 dimensions.

You can observe that in the above picture, the indices of each element are represented with two numbers, one is for the row and another one is for the column. The above diagram represents a two-dimensional array with a 3 x 3 matrix, which means there are three rows and three columns in the array.

Syntax of the two-dimensional array is.

Now let’s take a look at a sample program implementing a two-dimensional array.

See also  Operator Overloading in c++
#include <iostream>
using namespace std;
int main()
{
    int arr[3][3] = {{1 ,2 ,3} , {4, 5, 6} , {7, 8, 9}};
    for(int i = 0;i<3;i++){
        for(int j = 0;j<3;j++){
            cout<<arr[i][j]<<" ";
        }
        cout<<endl;
}
}

The output of the program is.

MULTI-DIMENSIONAL ARRAY

A multi-dimensional array is nothing but developing an array of the desired size we need. We can develop an array of any number of dimensions. However, the complexity of the code increases based on the increasing number of dimensions of the arrays.

Syntax of the multi-dimensional array is.

An increased number of dimensions makes accessing the element more difficult as traversing through each element increases the complexity of the code which in return decreases the efficiency.

Now let’s look at some of the popular algorithms which are implemented on arrays

SORTING OF ARRAY ELEMENTS

#include <iostream>
using namespace std;
int main()
{
    int numbers[10] = {33,15,47,5,98,45,57,44,79,66}, i ,j ,temp;

    for (i = 0; i < 10; ++i)
    {
        for (j = i + 1; j < 10; ++j)
        {
            if (numbers[i] > numbers[j])
            {
                temp =  numbers[i];
                numbers[i] = numbers[j];
                numbers[j] = temp;
            }
        }
    }
    cout<<"Sorting Order Array: \n";
    for (i = 0; i < 10; ++i)
        cout<<numbers[i]<<" ";
    return 0;
}

The output of the program is

See also  Pointers in c++

SEARCHING FOR AN ELEMENT IN AN ARRAY

#include<iostream>
using namespace std;
int main()
{
    int arr[10] = {33,15,47,5,98,45,57,44,79,66},k,ans=-1;
    cout<<"Enter element to be searched"<<endl;
    cin>>k;
    for(int i=0;i<10;i++)
    {
        if(arr[i]==k)
        {
            ans=i;
            break;
        }
    }
    if(ans!=-1)
    cout<<"The element "<<k<<" is present at index "<<ans;
    else
    cout<<"The element "<<k<<" is not there in the array";
    return 0;
}

The output of the program is.

IN-BUILT FUNCTIONS ON ARRAYS

There are some of the in-built functions which can be applied on arrays to get the specific result optimally. Some of the in-built functions applied on arrays are.

  • at(): at() function is used to access the elements in the array just as normal accessing of elements
  • get(): this is another method of accessing the elements in the array. get() function is not present in the array class but it can be implemented from the tuple class.
  • operator[]: this operator is used for normal accessing of array elements.

Let’s take a look at a sample program implementing the mentioned functions.

#include<iostream>
#include<array> 
#include<tuple> 
using namespace std;
int main()
{
    array<int,6> ar = {1, 2, 3, 4, 5, 6};
    cout << "using at() : ";
    for ( int i=0; i<6; i++)
    cout << ar.at(i) << " ";
    cout << endl;
    cout << "using get() : ";
    cout << get<0>(ar) << " " << get<1>(ar) << " ";
    cout << get<2>(ar) << " " << get<3>(ar) << " ";
    cout << get<4>(ar) << " " << get<5>(ar) << " ";
    cout << endl;
    cout << "normal accessing using [] : ";
    for ( int i=0; i<6; i++)
    cout << ar[i] << " ";
    cout << endl;
    return 0;
}

The output of the following code.

See also  C++ | Switch Statement

front(): front() function returns the first element of the array.

back(): back() function returns the last element of the array.

size(): size() function returns the size of the array. It means the number of elements present in the array.

Let’s take a look at a program implementing the above functions

#include<iostream>
#include<array>
using namespace std;
int main()
{
array<int,6> ar = {1, 2, 3, 4, 5, 6};                                                       
    cout << "First element of array is : ";
    cout << ar.front() << endl;                  
    cout << "Last element of array is : ";
    cout << ar.back() << endl;
    cout << "The number of array elements is : ";
    cout << ar.size() << endl;
    return 0;
}   

The output of the above code is.

CONCLUSION

So that’s it from this tutorial. Hope you find it interesting. Now we had a basic idea about arrays and their functionalities in C++ programming. You can learn more about arrays by practicing and solving more problems on arrays. Happy coding!

Leave a Comment

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

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

we provide projects, courses, and other stuff for free. in order for running we use Google ads to make revenue. please disable adblocker to support us.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock