ADVERTISEMENT
ADVERTISEMENT

valarray sqrt() Function in C++ with Examples

C++ valarray sqrt() Function: Here, we will learn about the sqrt() function, its usages, syntax and examples.
Submitted by Shivang Yadav, on May 13, 2022

The valarray class in C++ is a special container that is used for holding elements like an array and performing operations on them.

Mathematical sqrt value (Square root):

The square root of a number is the factor value which when multiplied by itself gives the number.

std::sqrt(std::valarray) Function

The sqrt() function of the valarray class is used to calculate the value of the square root of elements of the valarray. It returns a valarray with each element's square root.

Syntax:

template< class T >
valarray<T> sqrt( const valarray<T>& va );

// or
sqrt(valarrayName);

Parameter(s): It accepts a single parameter which is the original valarray.

Return Value: It returns a valarray with the square root value of each element of the original valarray.

C++ valarray sqrt() Function Example 1:

#include <iostream>
#include <valarray>
using namespace std;

int main()
{
    // Declaring valarray
    valarray<int> myvalarr = { 0, 16, 729, 100, 1 };

    // Printing the elements of valarray
    cout << "The elements of orignal valarray are : ";
    for (int& ele : myvalarr)
        cout << ele << " ";

    // Creating a new valarray
    valarray<int> sqrtValArray;
    sqrtValArray = sqrt(myvalarr);

    cout << "\nThe square root elements valarray are : ";
    for (int& ele : sqrtValArray)
        cout << ele << " ";

    return 0;
}

Output:

The elements of orignal valarray are : 0 16 729 100 1 
The square root elements valarray are : 0 4 27 10 1

C++ valarray sqrt() Function Example 2:

#include <iostream>
#include <valarray>
using namespace std;

int main()
{
    // Declaring valarray
    valarray<double> myvalarr = { 0, 16, 34.65, 5.76, -1 };

    // Printing the elements of valarray
    cout << "The elements of orignal valarray are : ";
    for (double& ele : myvalarr)
        cout << ele << " ";

    // Creating a new valarray
    valarray<double> sqrtValArray;
    sqrtValArray = sqrt(myvalarr);

    cout << "\nThe square root elements valarray are : ";
    for (double& ele : sqrtValArray)
        cout << ele << " ";

    return 0;
}

Output:

The elements of orignal valarray are : 0 16 34.65 5.76 -1 
The square root elements valarray are : 0 4 5.88643 2.4 -nan 

Note: The square root of a negative value is nan (not a number).


ADVERTISEMENT


ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.