Loading lang_c_ex_1101...
void transformReals(double *values, int valueCount, /* ...function pointer... */ fnct, /* ...generic pointer... */ params) { for(int i=0; i<valueCount; ++i) { values[i]= /* ...use fnct with values[i] and params... */; } }
double scaleAndAdd(double value, /* ...generic pointer... */ params) { const double factor= /* ...obtain from params... */; const double increment= /* ...obtain from params... */; return value*factor+increment; }
void test_transformReals(void) { printf("\n~~~~ %s() ~~~~\n", __func__); double values[]={0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ,9.0}; const int valueCount=(int)(sizeof(values)/sizeof(values[0]));
/* ...use transformReals() to apply scaleAndAdd() to each element in values[] with any suitable factor and increment... */
for(int i=0; i<valueCount; ++i) { printf("%g ", values[i]); } printf("\n"); }
int // found index or -1 findFirstInteger(const int *values, int valueCount, /* ...function pointer... */ fnct, /* ...generic pointer... */ params) { for(int i=0; i<valueCount; ++i) { if( /* ...use fnct with values[i] and params... */ ) { return i; } } return -1; // not found }
bool between(int value, /* ...generic pointer... */ params) { const int low= /* ...obtain from params... */; const int high= /* ...obtain from params... */; return value>=low && value<=high; }
void test_findFirstInteger(void) { printf("\n~~~~ %s() ~~~~\n", __func__); const int values[]={7, 2, 6, 9, 4, 3, 1, 8, 0, 5}; const int valueCount=(int)(sizeof(values)/sizeof(values[0]));
/* ...use findFirstInteger() to get the index of the first element in values[] for which between() reports that it stands between two suitable bounds... */ const int idx= /* ...found index */;
if(idx>=0) { printf("first element between %d and %d is %d (at index %d)\n", /* low bound */, /* high bound */, values[idx], idx); } }
void combineReals(const double *valuesX, const double *valuesY, double *valuesZ, int valueCount, /* ...function pointer... */ fnct, /* ...generic pointer... */ params) { for(int i=0; i<valueCount; ++i) { valuesZ[i]= /* ...use fnct with valuesX[i], valuesY[i] and params... */; } }
double combine(double x, double y, /* ...generic pointer... */ params) { const double xFactor= /* ...obtain from params... */; const double yFactor= /* ...obtain from params... */; return x*xFactor + y*yFactor; }
void test_combineReals(void) { printf("\n~~~~ %s() ~~~~\n", __func__); const double valuesX[10]={0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ,9.0}; const double valuesY[10]={1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8 ,1.9}; double valuesZ[10]; const int valueCount=(int)(sizeof(valuesZ)/sizeof(valuesZ[0]));
/* ...use combineReals() to apply combine() to valuesX[], valuesY[] and valuesZ[] with any suitable xFactor and yFactor... */
for(int i=0; i<valueCount; ++i) { printf("%g ", valuesZ[i]); } printf("\n"); }
//----------------------------------------------------------------------------
#ifndef GENERIC_H #define GENERIC_H
#include <stdbool.h>
typedef double (*TransformRealFnct)(double, void *);
void transformReals(double *values, int valueCount, TransformRealFnct fnct, void *params);
typedef bool (*IntegerCriterionFnct)(int, void *);
int // found index or -1 findFirstInteger(const int *values, int valueCount, IntegerCriterionFnct fnct, void *params);
typedef double (*RealCombinationFnct)(double, double, void *);
void combineReals(const double *valuesX, const double *valuesY, double *valuesZ, int valueCount, RealCombinationFnct fnct, void *params);
#endif // GENERIC_H
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include "generic.h"
void transformReals(double *values, int valueCount, TransformRealFnct fnct, void *params) { for(int i=0; i<valueCount; ++i) { values[i]=fnct(values[i], params); } }
int // found index or -1 findFirstInteger(const int *values, int valueCount, IntegerCriterionFnct fnct, void *params) { for(int i=0; i<valueCount; ++i) { if(fnct(values[i], params)) { return i; } } return -1; // not found }
void combineReals(const double *valuesX, const double *valuesY, double *valuesZ, int valueCount, RealCombinationFnct fnct, void *params) { for(int i=0; i<valueCount; ++i) { valuesZ[i]=fnct(valuesX[i], valuesY[i], params); } }
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include "generic.h"
#include <stdio.h>
double scaleAndAdd(double value, void *params) { const double *coefs=(const double *)params; const double factor=coefs[0]; const double increment=coefs[1]; return value*factor+increment; }
void test_transformReals(void) { printf("\n~~~~ %s() ~~~~\n", __func__); double values[]={0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ,9.0}; const int valueCount=(int)(sizeof(values)/sizeof(values[0])); double coefs[]={1.1, 20.0}; transformReals(values, valueCount, scaleAndAdd, coefs); for(int i=0; i<valueCount; ++i) { printf("%g ", values[i]); } printf("\n"); }
bool between(int value, void *params) { const int *bounds=(const int *)params; const int low=bounds[0]; const int high=bounds[1]; return value>=low && value<=high; }
void test_findFirstInteger(void) { printf("\n~~~~ %s() ~~~~\n", __func__); const int values[]={7, 2, 6, 9, 4, 3, 1, 8, 0, 5}; const int valueCount=(int)(sizeof(values)/sizeof(values[0])); int bounds[]={8, 12}; const int idx=findFirstInteger(values, valueCount, between, bounds); if(idx>=0) { printf("first element between %d and %d is %d (at index %d)\n", bounds[0], bounds[1], values[idx], idx); } }
double combine(double x, double y, void *params) { const double *coefs=(const double *)params; const double xFactor=coefs[0]; const double yFactor=coefs[1]; return x*xFactor + y*yFactor; }
void test_combineReals(void) { printf("\n~~~~ %s() ~~~~\n", __func__); const double valuesX[10]={0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ,9.0}; const double valuesY[10]={1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8 ,1.9}; double valuesZ[10]; const int valueCount=(int)(sizeof(valuesZ)/sizeof(valuesZ[0])); double coefs[]={10.0, 0.5}; combineReals(valuesX, valuesY, valuesZ, valueCount, combine, coefs); for(int i=0; i<valueCount; ++i) { printf("%g ", valuesZ[i]); } printf("\n"); }
int main(void) { test_transformReals(); test_findFirstInteger(); test_combineReals(); return 0; }
//----------------------------------------------------------------------------