Arrays in C++

Arrays in C++

What are Arrays in c++?

Β·

4 min read

Hello everyone πŸ™‹β€β™‚οΈ in this article I will be talking about arrays like creating arrays, input, output, updating, etc, etc in a very simple way, so let us start 😊

Arrays Introduction

An array is the collection of elements of the same type placed in continuous memory locations.

array.png

We will look at how we can perform different operations on the array :

  • creation
  • input - output
  • update

Creating an array

#include<iostream>
using namespace std;
int main(){

    int a[10]; 
    int b[10]={5};  
    int c[10]={4,5,6};
    int d[]={2,3,5};
    return 0;
}

How to find the size of an array ❓

#include<iostream>
using namespace std;
int main(){

    int a[100]={1,2,4};
    int n= sizeof(a)/sizeof(int);
    cout<<n<<endl;
    return 0;
}
output:  100

arrayexplain.png

How to print elements of the Array ❓

#include<iostream>
using namespace std;
int main(){

    int a[100]={1,2,4};
    int n= sizeof(a)/sizeof(int);

    //printing elements of the array

    for(int i=0;i<n;i++){
        cout<<a[i]<<',';

    }
    cout<<endl;


    return 0;
}

How to input elements for the arrays ❓

#include<iostream>
using namespace std;
int main(){
    //taking array as an user input
    int n;
    cout<<"enter number of participants :"<<endl;
    cin>>n;
    int a[n];
    for(int i=0;i<n;i++){
        cout<<"enter value for index "<<i<<" : "<<endl;
        cin>>a[i];
    }

    for(int i=0;i<n;i++){
        cout<<a[i]<<',';
    }
    cout<<endl;
    return 0;
}

Passing Arrays to Function.

#include<iostream>
using namespace std;

void printArray(int* arr,int n){
        arr[2]=100;
        cout<<"in fun"<<endl;
        for(int i=0;i<n;i++){
        cout<<arr[i] <<endl;
    }


}



int main(){
    // Passing Arrays To functions
    int arr[]={1,2,4,5,6,7};
    int n= sizeof(arr)/sizeof(int);
    printArray(arr,n);
    cout << "In Main" << sizeof(arr)<<endl;

    for(int i=0;i<n;i++){
        cout<<arr[i] <<endl;
    }


    return 0;
}
Output :
in fun
1
2
100
5
6
7
In Main24
1
2
100
5
6
7

⚠️ Important concept πŸ’‘:

When passing arrays to the function the arrays are passed by reference i.e the array which you pass, and the new array in the function scope will be sharing the same memory which means they will be the same,

In the code given above πŸ‘† you can see that, I have changed the value of arr[2] in the scope of the print array() function, as both are sharing the same memory printing the elements of the array will give the same output.

If you don't know what is passed by reference you can read this πŸ‘‰ article C++ Pointers and pass by reference for more knowledge πŸ₯Έ.

I tried to explain by this πŸ‘‡diagram, hope you will get it πŸ‘.

arraycodeex.png

Printing Reverse on Array

#include<iostream>
using namespace std;
void reverse(int * arr, int n){
    for(int i = (n-1) ; i>=0 ; i--){
        cout<<arr[i]<<',';
    }
    cout<<endl;
}
int main(){
    //printing reverse of an array
    int n;
    cout<<"Enter number of elements :"<<endl;
    cin>>n;
    int arr[n];
    //taking input
    for(int i=0;i<n;i++){
        cout<<"enter element for "<<i<<"th index :"<<endl;
        cin>>arr[i];
    }
    reverse(arr,n);
    return 0;
}

Reversing the original array

There is a difference between printing the reverse of an array and reversing the original array, for the same we can use the concept of the pointer, see below πŸ‘‡code.

#include<iostream>
using namespace std;
void reversearr(int * arr,int n){

    int start=0;
    int end= n-1;
    while(start!= end){
        swap(arr[start],arr[end]);
        start++;
        end--;
    }
}

int main(){
int n;
        cout<<"Enter number of elements :"<<endl;
        cin>>n;
        int arr[n];
        //taking input
        for(int i=0;i<n;i++){
            cout<<"enter element for "<<i<<"th index :"<<endl;
            cin>>arr[i];
        }
        reversearr(arr,n);

        for(int i=0;i<n;i++){
        cout<<arr[i]<<" , " ;
    }
    cout<<endl;

}

arr in the reversearr() function is pointing to the arr in the main function so changing in the scope of the reversearr() function is affecting the arr since both are pointing to the same array. see the below diagram. πŸ‘‡

reverse_array_png.png

What are subarrays?

A subarray is the small continuous part of the array, which means you can start the sub-arrays at any index and end it at any index.

Total_number_of_subarrays_png.png

Now let's see how we can print every possible subarray

#include<iostream>
using namespace std;
int main(){
    int arr[]={1,2,4,5,7};     
    int n=sizeof(arr)/sizeof(int);
    for(int i=0;i<n;i++){
        for(int j=i; j<n;j++){
            for(int k=i;k<=j;k++){
                cout<<arr[k]<<',';
            }
            cout<<endl;
        }
        cout<<endl;
    }
    return 0;
}
output:

1,
1,2,
1,2,4,
1,2,4,5,
1,2,4,5,7,

2,
2,4,
2,4,5,
2,4,5,7,

4,
4,5,
4,5,7,

5,
5,7,

7,

See the output πŸ‘†, the total number of subarrays is 15, we can use the formula (nc2, where n is 5 ) to find the total number of subarrays

Thank you for reading I hope you understood this concept .

Bye. πŸ‘‹ OkByeByeByeGIF (2).gif

Did you find this article valuable?

Support Welcome to my Blog by becoming a sponsor. Any amount is appreciated!

Β