ADVERTISEMENT
ADVERTISEMENT

valarray exp() Function in C++ with Examples

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

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

std::exp(std::valarray) Function

The exp() function of the valarray class is used to calculate the value of e raised to the value of element. It returns a valarray with each element calculated using exp() function.

Syntax:

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

// or
exp(valarrayName)

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

Return Value: It returns a valarray of value e raised to the power of the elements of the original valarray.

C++ valarray exp() Function Example 1:

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

int main()
{
    // Declaring valarray
    valarray<int> myvalarr = { 3, 6, 2, 5, 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> expValarray;
    expValarray = exp(myvalarr);

    cout << "\nThe elements of exp valarray are : ";
    for (int& ele : expValarray)
        cout << ele << " ";

    return 0;
}

Output:

The elements of orignal valarray are : 3 6 2 5 1 
The elements of exp valarray are : 20 403 7 148 2 

C++ valarray exp() Function Example 2:

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

int main()
{
    // Declaring valarray
    valarray<double> myvalarr = { 3.1, -6, -0.2, 5, 1.7 };

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

    // Creating a new valarray
    valarray<double> expValarray;
    expValarray = exp(myvalarr);

    cout << "\nThe elements of exp valarray are : ";
    for (double& ele : expValarray)
        cout << ele << " ";

    return 0;
}

Output:

The elements of orignal valarray are : 3.1 -6 -0.2 5 1.7 
The elements of exp valarray are : 22.198 0.00247875 0.818731 148.413 5.47395 

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.