Vector calculations

This is a discussion on Vector calculations within the C++ Programming forums, part of the General Programming Boards category; I'm trying to write a function which performs calculations with a vector containing data from a CSV file. The result ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    12

    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!

  2. #2
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    452
    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
    Last edited by nimitzhunter; 04-20-2011 at 04:07 PM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,213
    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

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    12
    Quote Originally Posted by phantomotap View Post
    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!

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,535
    You need to fix your indentation.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ calculations
    By Learner87 in forum C++ Programming
    Replies: 1
    Last Post: 04-29-2008, 08:51 AM
  2. Help with calculations
    By Taka in forum C Programming
    Replies: 14
    Last Post: 09-01-2007, 07:00 PM
  3. pi calculations
    By nbice in forum C++ Programming
    Replies: 7
    Last Post: 09-30-2002, 01:48 PM
  4. calculations
    By wayko in forum Windows Programming
    Replies: 8
    Last Post: 10-11-2001, 06:19 AM
  5. calculations
    By wayko in forum C++ Programming
    Replies: 1
    Last Post: 10-09-2001, 10:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21