I have a graph with 12 different trendlines. I want to produce a single trendline by adding together the values of all 12 trendlines, like this:
for (i = 0, cumulativeTrendValue = 0; i < 12; ++i)
{
cumulativeTrendValue += trendLine[i];
}
I then feed the values of this cumulativeTrendLine into another function. That is straightforward enough. But now I want to add variations to this by assigning 1 of 3 different weights to each of the 12 trendlines. For example, the first cumulative trendline would be created by assigning a weight of 1 to each trendline; the next would be created by assigning a weight of 2 to the first trendline, and 1 to the rest the trendlines, etc, ad nauseum.
Now I'm sure this has been asked and answered here before, but I have spent 2 hours trying to find an implementation in c, but cannot. (Btw, this is not homework, in case you were wondering. You can look at my other questions over the last several years to affirm that.)
So, my question is: what keywords can I use to find where this question has previously been answered. Or, if you are in an especially giving vein today, can you provide a link to the question/answer here on SO.
The most obvious solution to me is to add another array for the weights, to match the tredLine
array.
However if you want to do it algorithmically as outlined in the question, you could have another loop outside the current, and a counter in the old inner loop that counts down. Something like this:
for (weight = 1; weight < 12; weight++)
{
int currentWeight = weight;
for (i = 0, cumulativeTrendValue = 0; i < 12; i++)
{
cumulativeTrendValue += trendLine[i] * currentWeight;
if (currentWeight > 1)
currentWeight--;
}
/* Use cumulativeTrendValue someway */
}