arrays - assigning weights to trendlines in c -
i trying assign set of weights set of trendlines. weights in array, , trendlines in array. want end permutation: ie, each trendline in array gets assigned each weight. in code getting output:
output: 1 combination per line: 1.00, 1.00, 1.00, 1.00 1.50, 1.50, 1.50, 1.50 2.00, 2.00, 2.00, 2.00,
but trying permutations, excluding duplicates. want different possible combinations of weights/trendlines:
output: 1 combination per line: 1.00, 1.00, 1.00, 1.00 1.50, 1.00, 1.00, 1.00, 2.50, 1.00, 1.00, 1.00, 1.00, 1.50, 1.00, 1.00, 1.50, 1.50, 1.00, 1.00, 2.50, 1.50, 1.00, 1.00, ....etc
edit: question is: how alter code below can produce different combinations of trendlines , weights. example, if had 2 trendlines , 3 weights (1,2,3), combinations be:
1,1 1,2 1,3 2,1 2,2 2,3 3,1 3,2 3,3
here code:
#define trendline_count 4 #define weight_count 3 void hhour(void) { int trendline[trendline_count]; double weight[] = { 1, 1.5, 2 }; file *myfile = fopen("s:\\data\\testdump\\ww.txt", write); fprintf(myfile, "output: 1 combination per line :\n" ); (int weightindex = 0; wei`enter code here`ghtindex < weight_count; weightindex++) { double currentweight = weight[weightindex]; double cumulativetrendvalue = 0; (int trendlineindex = 0; trendlineindex < trendline_count; trendlineindex++) { cumulativetrendvalue += trendline[trendlineindex] * currentweight; fprintf(myfile, "%.02lf, ", currentweight); } fprintf(myfile, "\n"); // cumulativetrendvalue } fclose(myfile); }
i think want. there cleaner solutions out there i'm big fan of brute forcing solutions when possible. if want vary trendline... uh, maybe recursive variadic function... start charging when facing level of insantiy.
#include <stdio.h> #include <string.h> #define trendline_count 4 #define weight_count 3 double weight[] = { 1, 1.5, 2 }; int addone(int counter[trendline_count]) { int i=0; while(1) { if(counter[i]+1 >= weight_count) { if( i+1 >= trendline_count) { homecoming 1;} else { counter[i]=0; i++; } } else { counter[i]++; homecoming 0; } } homecoming 0; } void hhr() { int i; int counter[trendline_count]; memset(counter, 0, sizeof(int)*trendline_count); { for(i=0; i<trendline_count; i++) { //printf("%d ", counter[i]); printf("%f ", weight[counter[i]]); } printf("\n"); }while(!addone(counter)); } int main(int argc, char **argv) { hhr(); }
c arrays permutation
No comments:
Post a Comment