Loading lang_c_ex_0402...
void test_array_multiples(void) { printf("\n~~~~ test_array_multiples() ~~~~\n"); // ... }
void test_array_bounds(void) { printf("\n~~~~ test_array_bounds() ~~~~\n"); // ... }
void test_array_negative(void) { printf("\n~~~~ test_array_negative() ~~~~\n"); // ... }
//----------------------------------------------------------------------------
#ifndef ARRAYS_H #define ARRAYS_H
int countMultiples(const int *values, int valueCount, int divisor);
void cancelMultiples(int *values, int valueCount, int divisor);
int countOutOfBounds(const double *values, int valueCount, double low, double high);
void clampValues(double *values, int valueCount, double low, double high);
double negativeSum(const double *values, int valueCount);
void negativeScale(double *values, int valueCount, double factor);
#endif // ARRAYS_H
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include "arrays.h"
int countMultiples(const int *values, int valueCount, int divisor) { int count=0; for(int i=0; i<valueCount; ++i) { if(values[i]%divisor==0) { ++count; } } return count; }
void cancelMultiples(int *values, int valueCount, int divisor) { for(int i=0; i<valueCount; ++i) { if(values[i]%divisor==0) { values[i]=1; } } }
int countOutOfBounds(const double *values, int valueCount, double low, double high) { int count=0; for(int i=0; i<valueCount; ++i) { const double value=values[i]; if((value<low)||(value>high)) { ++count; } } return count; }
void clampValues(double *values, int valueCount, double low, double high) { for(int i=0; i<valueCount; ++i) { const double value=values[i]; if(value<low) { values[i]=low; } else if(value>high) { values[i]=high; } } }
double negativeSum(const double *values, int valueCount) { double accum=0.0; for(int i=0; i<valueCount; ++i) { const double value=values[i]; if(value<0.0) { accum+=value; } } return accum; }
void negativeScale(double *values, int valueCount, double factor) { for(int i=0; i<valueCount; ++i) { const double value=values[i]; if(value<0.0) { values[i]=value*factor; } } }
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include "arrays.h"
#include <stdio.h>
void test_array_multiples(void) { printf("\n~~~~ test_array_multiples() ~~~~\n"); int values[]={12, 7, 15, 8, 23, 27, 4, 11, 18}; const int valueCount=(int)(sizeof(values)/sizeof(values[0])); const int divisor=3; printf("%d multiples of %d in array\n", countMultiples(values, valueCount, divisor), divisor); cancelMultiples(values, valueCount, divisor); printf("%d multiples of %d in array\n", countMultiples(values, valueCount, divisor), divisor); }
void test_array_bounds(void) { printf("\n~~~~ test_array_bounds() ~~~~\n"); double values[]={12.4, 7.6, 15.1, 8.2, 23.5, 27.0, 4.4, 11.2, 18.3}; const int valueCount=(int)(sizeof(values)/sizeof(values[0])); const double low=5.2, high=17.3; printf("%d values out of [%g;%g] in array\n", countOutOfBounds(values, valueCount, low, high), low, high); clampValues(values, valueCount, low, high); printf("%d values out of [%g;%g] in array\n", countOutOfBounds(values, valueCount, low, high), low, high); }
void test_array_negative(void) { printf("\n~~~~ test_array_negative() ~~~~\n"); double values[]={12.4, -7.6, 15.1, -8.2, -23.5, 27.0, 4.4, -11.2, 18.3}; const int valueCount=(int)(sizeof(values)/sizeof(values[0])); printf("sum of negative values in array: %g\n", negativeSum(values, valueCount)); negativeScale(values, valueCount, 10.0); printf("sum of negative values in array: %g\n", negativeSum(values, valueCount)); }
int main(void) { test_array_multiples(); test_array_bounds(); test_array_negative(); return 0; }
//----------------------------------------------------------------------------