# Vector calculations

• 04-20-2011
stobbz
Vector calculations
I'm trying to write a function which performs calculations with a vector containing data from a CSV file. The result is to be stored in another vector.

If a vector element obtained from the CSV file is negative, the corresponding element in the new vector should equal to zero. If the element from the original vector is within a certain range, it should perform the calculation as normal and finally, if it's above a certain number it should again be set to zero.

I have written some code which performs the calculation when the element is in range:

Code:

```void TurPw(vector<double> &Data){                        double Pproduct = 0.5*RotorArea*AirDensity;                    vector<double> vec2;                 for( vector<double>::const_iterator i=Data.begin(); i!=Data.end(); ++i){                 vec2.push_back( pow(*i, 3) );         }                 for( vector<double>::iterator iter = vec2.begin(); iter!=vec2.end(); ++iter){                 *iter *= Pproduct;                 }         }```
The vector 'Data' contains the orginal data from the CSV. This piece of code cubes the elements and multiplies them by another product.

How could I implement the other conditions?

Any help appreicated,
Thanks!
• 04-20-2011
nimitzhunter
How about you try using std::find_if?

Code:

```if ( find_if(Data.begin() , Data.end() , check the condition) != Data.end() )     do something; else   normal calculation```
• 04-20-2011
phantomotap
Quote:

I have written some code which performs the calculation when the element is in range
Where is this code then? (The code you posted always does the calculation for every element.)

As a matter of interest, what exactly are you doing? I only ask because the datum contained by `vec2' looks as it will grow, in some direction, very fast.

Soma
• 04-20-2011
stobbz
Quote:

Originally Posted by phantomotap
Where is this code then? (The code you posted always does the calculation for every element.)

As a matter of interest, what exactly are you doing? I only ask because the datum contained by `vec2' looks as it will grow, in some direction, very fast.

Soma

Thanks for the reply guys, I managed to get the correct result using a couple of if statements. Over thinking the problem as usual!

Well, 'vec2' contains around 150,000 elements representing recorded wind speeds. This piece of code is in place to process the wind data and help determine whether a turbine is capable of generating power certain speeds.

Thanks again!
• 04-23-2011
Elysia
You need to fix your indentation.