I am dealing with my first ever code and I am blocked again on a new problem.
I have a bunch of values in a vectorA, and I want to perform the following while loop (in pseudo-code) :
"Make a variable double SUM = 0 and a variable int count.
Take the front value of the vector, add it to SUM"
Then erase the front value in the vector
The variable SUM should act as a capacitor: when SUM is superior to a given constant u,
SUM becomes equal to SUM - u
Another vector vectorB would store the value of count each time SUM > u
Right now I only have a VectorA that contains all my values and an export of the list into a .txt file.
I would like to find a way to put the front value of vectorA in a local variable to add it to SUM, and then erase this front value. Is that possible? Are there better ways to do this?
Here's the code :
#include <iostream>
#include <fstream>
#include <vector>
#include <iterator>
#include <cstdlib>
#include <string>
#include <sstream>
using namespace std;
// constant values
float Da=0.1; //densities
float Db=0.5;
float Dc=1;
double Dd=0.333333;
int l = 10; //width & height
int h = 10;
float u = 10; // UNIT
int main ()
{
// vectors
vector <double> VectorA;
vector <double> vectorB;
int I = 0; //itération pour la somme des valeurs du vecteur
double SUM = 0; //somme des éléments du vecteurA
float a = 0;
float b = 0; // Local variables
while (a<l+1, b<h+1){
//values for given a & b
double DL = Da-Da*(b/h)+Dc*(b/h);
double DR = Db-Db*(b/h)+Dd*(b/h);
double D = DL-DL*(a/l)+DR*(a/l);
//print
//cout<<D<<endl;
//store
VectorA.push_back (D);
// next pixel/unit & next line
a = a+u;
if (a>l) {
a = 0;
b = b+u;
}
}
// export values to .txt file
ofstream output_file("./step1.txt");
ostream_iterator<double> output_iterator(output_file, "\n");
copy(VectorA.begin(), VectorA.end(), output_iterator);
}
Let's make this a bit simpler by taking out all the domain-specific stuff, and just discuss the basic question:
[How do I] put the front value of vectorA in a local variable to add it to SUM, and then erase this front value?
Here is a simple approach:
vector <double> vectorA;
double SUM = 0.0;
// ...
while (!vectorA.empty())
{
const double curVal = vectorA.front(); // this strictly isn't necesarry. the compiler might optimize this away
SUM += curVal;
vectorA.erase (vectorA.begin());
}
Now let's incorporate u
:
vector <double> vectorA;
double SUM = 0.0;
const double u = /* ??? */;
// ...
while (!vectorA.empty())
{
const int curVal = vectorA.front(); // this strictly isn't necesarry. the compiler might optimize this away
if (curVal > SUM)
{
SUM = curVal - u;
}
vectorA.erase (vectorA.begin());
}
I'm not really sure how count
acts, or what values are being stored in to vectorB
, but as a wild guess I'm going to assume that count
is incremented every time curVal > SUM
, and the resulting new value for count
is inserted in to vectorB
. So let's try to implement that:
vector <double> vectorA;
double SUM = 0.0;
const double u = /* ??? */;
int count = 0;
vector <int> vectorB;
// ...
while (!vectorA.empty())
{
const int curVal = vectorA.front(); // this strictly isn't necesarry. the compiler might optimize this away
if (curVal > SUM)
{
SUM = curVal - u;
++count;
vectorB.push_back (count);
}
vectorA.erase (vectorA.begin());
}
There are opportunities for micro-optimization above, but recall Knuth's golden rule:
Micro-optimization is the root of all evil.
When building software, the best approach is to first select the correct algorithm with needed efficiency in mind (be they space or time efficiency) and build that in a robust, easily maintaned way. Then profile your program in a Release build, identify problem hotspots, and micro-optimize those sections of the code only where necesarry. If you select the correct algorithm to begin with, and write it well, you'll often find that no micro-optimization is needed.