#include <iostream>
#include <math.h> //:: pow()
#include <vector> // :: vector<>
using namespace std;
double sideLengthOf_equilateralTriangle( const double& givenArea_A ) //***** ( A-1 )
{
const double constValueFormula { 2.0 / pow( 3.0 , ( 1. / 4. ) ) }; //***** ( A-2 )
return ( sqrt( givenArea_A ) * constValueFormula ); //***** ( A-3 )
}
template< typename T > //***** ( B-1 )
auto createVector( const int& length_A )
{
vector< T > myVector( length_A ); //***** ( B-2 )
int currentValue { -3 };
for ( auto& elem : myVector ) //***** ( B-3 )
{
elem = currentValue++;
}
return myVector; //***** ( B-4 )
}
int main()
{
const double givenArea { 16 * sqrt( 3 ) };
cout << "lenght = " << sideLengthOf_equilateralTriangle( givenArea ) << endl;
auto myVector { createVector< int >( 10 ) };
cout << "Vector size =" << myVector.size() << endl;
cout << "List of elements:" << endl;
for ( auto& elem : myVector )
{
cout << " [ " << elem << " ] ";
}
}
/*
*( A-1 )* :: Step 1: Simplify the formula to its simplest form.
*( A-2 )* :: There is a constant in our mathematical formula that we calculate.
(Speed up functions). It's a good idea to use a function that returns constexpr.
Then the expressions have the form: constexpr double constValueFormula { 2.0 / otherPowFunction( 3.0 , ( 1. / 4. ) ) };
*( A-3 )* :: As a result, we return our mathematical formula.
*( B-1 )* :: Vector type is undefined so i created template. Propably vector<int> is enough. So you can change here vector< int > myVector( length_A ) and remove template;
*( B-2 )* :: create vector of length N
*( B-3 )* :: assigning values to elements. Each element is by 3 smaller than the index of the element. So first element value = -3.
*( B-4 )* :: Vector filled, so just return.
*/