It is currently Wed Aug 23, 2017 6:56 am

All times are UTC - 5 hours




 Page 1 of 1 [ 11 posts ] 
Author Message
 Post subject: summing values in a vector
PostPosted: Wed Feb 22, 2012 2:03 am 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 448
I know this may be a simple task but my mind is drawing a blank. I have a std::vector of some arbitrary value with defined size. I am trying to add together all values in this vector and print out the total.
Any tips ?


Offline
 Profile  
 
 Post subject: Re: C++ VMK 10 - Standard Template Library
PostPosted: Wed Feb 22, 2012 5:54 am 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1105
Location: Ontario Canada
skilz80 wrote:
I know this may be a simple task but my mind is drawing a blank. I have a std::vector of some arbitrary value with defined size. I am trying to add together all values in this vector and print out the total.
Any tips ?


Have a look in the numeric header for the accumulate function:
http://msdn.microsoft.com/en-us/library/aawk6wsh.aspx


Offline
 Profile  
 
 Post subject: Re: C++ VMK 10 - Standard Template Library
PostPosted: Wed Feb 22, 2012 6:59 am 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 448
Thank You for the reply. This greatly helps. I was trying to go through a second for loop and indexing the vectors but was having trouble getting the correct answer without having a debug insertion failure.

The accumulate function works great only problem is it returns an int, and the vector i am using is containing doubles or floats, and the values after the decimal are important. I don't want to truncate the values.


Offline
 Profile  
 
 Post subject: Re: C++ VMK 10 - Standard Template Library
PostPosted: Wed Feb 22, 2012 9:11 am 

Joined: Thu Sep 27, 2007 5:51 pm
Posts: 48
skilz80 wrote:
Thank You for the reply. This greatly helps. I was trying to go through a second for loop and indexing the vectors but was having trouble getting the correct answer without having a debug insertion failure.

The accumulate function works great only problem is it returns an int, and the vector i am using is containing doubles or floats, and the values after the decimal are important. I don't want to truncate the values.


Nope, it's a STL function, so It's templated and returns whatever you want.

#include <iostream>
#include <vector>
#include <numeric>

int main()
{
   std::vector<double> v;

   v.push_back(33.48);
   v.push_back(17.8925);
   v.push_back(2.0);

   double sum = std::accumulate(v.begin(), v.end(), 0.0f);

   std::cout << sum << std::endl;

   return 0;
}


Offline
 Profile  
 
 Post subject: Re: C++ VMK 10 - Standard Template Library
PostPosted: Wed Feb 22, 2012 9:29 am 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 448
I thought it was templated, for some reason the function was giving me a return value of an int while moving the mouse cursor over it. After I closed Visual Studio and reopened it, it then displayed the double or float, I guess the intellisense never caught up lol. How ever the answer I am getting is correct, but the values after the decimal are .00 and the value with the numbers I am using are .47, i tried with both float and doubles. The std:: function or algorithm does work, but my values are getting truncated :(


Offline
 Profile  
 
 Post subject: Re: C++ VMK 10 - Standard Template Library
PostPosted: Wed Feb 22, 2012 11:14 am 

Joined: Thu Sep 27, 2007 5:51 pm
Posts: 48
Could you post your code?


Offline
 Profile  
 
 Post subject: Re: C++ VMK 10 - Standard Template Library
PostPosted: Wed Feb 22, 2012 11:51 am 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 448
It is pretty straightforward, the true answer should be 21474836.47 as this was done on a TI-84.
the basic concept is starting at 0.01 for the first iteration and each iteration after is double the previous, for 31 iterations. Then adding all 31 elements. A basic expoential equation with a sum of values. Here is the c++ code.

#include "stdafx.h"
#include <vector>
#include <numeric>

int _tmain(int argc, _TCHAR* argv[])
{
   using namespace std;
   
   vector<long double> vAmount;
   long double dInit = 0.01f;
   long double dTotal = 0.0f;
   vAmount.push_back( dInit );
   for ( int i = 1; i <= 30; i++ ) {
      dInit *= 2;
      vAmount.push_back( dInit );
   }


   dTotal = accumulate( vAmount.begin(), vAmount.end(), 0.0f );

   printf( "%1.2f\n", dTotal );
   

   return 0;
}


Offline
 Profile  
 
 Post subject: Re: C++ VMK 10 - Standard Template Library
PostPosted: Thu Feb 23, 2012 3:35 am 

Joined: Thu Sep 27, 2007 5:51 pm
Posts: 48
Hmm, that's strange, I tried it with STL's setprecision and setf too, and it wouldn't work. And it shouldn't be too big a value to keep the precision. Hope Marek can answer this.


Offline
 Profile  
 
 Post subject: Re: C++ VMK 10 - Standard Template Library
PostPosted: Wed Feb 29, 2012 9:32 pm 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1105
Location: Ontario Canada
Sorry for the late reply, I just saw this message on the forums. Don't forget that when you are working with double precision numbers you will won't get exactly the number that you think you are getting because the computer is storing your floating numbers in IEEE format.

If you run the code below you'll see what are the actual values that you are storing in the array and why things add up they way they do.

std::vector<double> vAmount;
   double dInit = 0.01f;
   vAmount.push_back( dInit );
   std::cout << std::setprecision( 10 ) << dInit << std::endl;

   for ( int i = 1; i <= 30; i++ ) {
      dInit *= 2;
      vAmount.push_back( dInit );
      std::cout << std::setprecision( 10 ) << dInit << std::endl;
   }

   double dTotal = std::accumulate( vAmount.begin(), vAmount.end(), 0.0 );

   std::cout << "Total: " << std::setprecision( 10 ) << dTotal << std::endl;


Offline
 Profile  
 
 Post subject: Re: C++ VMK 10 - Standard Template Library
PostPosted: Wed Feb 29, 2012 9:41 pm 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1105
Location: Ontario Canada
If you don't want to worry about all the rounding errors, just use integers like so:

std::vector<int> vAmount;
   int iInit = 1;
   vAmount.push_back( iInit );
   std::cout << iInit << std::endl;

   for ( int i = 1; i <= 30; i++ ) {
      iInit *= 2;
      vAmount.push_back( iInit );
      std::cout << iInit << std::endl;
   }

   int iTotal = std::accumulate( vAmount.begin(), vAmount.end(), 0 );

   std::cout << "Total: " << iTotal << " -> " << std::setprecision( 10 ) << static_cast<double>( iTotal / 100.0 ) << std::endl;


Offline
 Profile  
 
 Post subject: Re: summing values in a vector
PostPosted: Thu Mar 01, 2012 1:13 am 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 448
Thank you for the reply, this does give me the correct answer I am looking for. This tells me that my algorithm was correct, only I was missing the setprecision function from the std library. Very nice!


Offline
 Profile  
 
Display posts from previous:  Sort by  
 Page 1 of 1 [ 11 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to:  

cron