Built-in functions and Library utilities in arrays:

                Built-in functions and library utilities are predefined in standard files. It mainly manipulates arrays in terms of sorting, searching and so on.

The standard functions are given below.

Header file : <algorithm> and <numeric>

Functions:

‘std::sort(array,size)’ – this function sorts the array in ascending order.

‘std::reverse(array,size)’ – it reverses the array.

‘std:: accumulate(array, array + size, 0)’ – it adds the array elements together.

std::find(array, array + size, value)’- it finds a particular element in the array.

std::binary_search(array, array + size, value)’- it uses the binary search to find an element.

arr.size()’ -it gives you the size of the array.

arr.at(2)’- a particular element is accessed.

arr.fill(1)’- it fills 1 for all elements.

arr.swap(arr2)’ – it swaps the element between two arrays.

‘sizeof(array)’ – it gives the size of the array.

‘memset(array, 0, sizeof(array))’ -it initialises the value to array.

Let us create a c++ program using these functions.

1.‘std::sort(array,size)’

#include <iostream>

#include <algorithm>

using namespace std;

int main() {

       int x[10];

       int i,n,j;

       cout<<"Enter the n value:"<<endl;

       cin>>n;

       cout<<"Enter the array elements:"<<endl;

       for(i=0;i<n;i++)

       {

       cin>>x[i];

       }

       cout<<"The original array is:"<<endl;

       for(i=0;i<n;i++)

       {

       cout<<x[i]<< " ";

       }

        // Sort the array using std::sort()

    sort(x, x+n);

    cout<<endl<<"The sorted array is:"<<endl;

    for (i=0;i<n;i++){

        cout << x[i] << " ";

    }

    return 0;

}

Output:

Enter the n value:

5

Enter the array elements:

34

12

67

5

98

The original array is:

34 12 67 5 98

The sorted array is:

5 12 34 67 98

2. ‘std::reverse(array,size)’

              This function is used to reverse the array elements from back to front and vice versa.

Program:

#include <iostream>

#include <algorithm>

using namespace std;

int main() {

       int x[10];

       int i,n,j;

       cout<<"Enter the n value:"<<endl;

       cin>>n;

       cout<<"Enter the array elements:"<<endl;

       for(i=0;i<n;i++)

       {

       cin>>x[i];

       }

       cout<<"The original array is:"<<endl;

       for(i=0;i<n;i++)

       {

       cout<<x[i]<< " ";

       }

        // Reverse the array using std::reverse()

   reverse(x, x+n);

    cout<<endl<<"The reversed array is:"<<endl;

    for (i=0;i<n;i++){

        cout << x[i] << " ";

    }

    return 0;

}

Output:

Enter the n value:

5

Enter the array elements:

12

78

45

76

4

The original array is:

12 78 45 76 4

The reversed array is:

4 76 45 78 12

Hope, this code is useful to you. Remaining code parts are included in furthermore blog posts. Keep coding!!!!

Comments

Popular posts from this blog

How to create a XML DTD for displaying student details

How to write your first XML program?

Java NIO examples to illustrate channels and buffers.