Loading lang_c_ex_0602...
void displayArray(const char *title, const double *elements, int count) { printf("%s:", title); for(int i=0; i<count; ++i) { printf(" %g", elements[i]); } printf("\n"); }
void test_concatArray(void) { printf("\n~~~~ %s() ~~~~\n", __func__); int aCount=10; double *a=(double *)malloc(aCount*sizeof(double)); if(a==NULL) { abort(); } for(int i=0; i<aCount; ++i) { a[i]=1.1*i; } const double b[]={100.0, 102.2, 104.4, 106.6, 108.8}; const int bCount=(int)(sizeof(b)/sizeof(b[0])); displayArray("a before", a, aCount); displayArray("b before", b, bCount);
// ...use concatArray() so that a is extended with b...
displayArray("a after", a, aCount); displayArray("b after", b, bCount); free(a); }
void displayArray(const char *title, const double *elements, int count) { printf("%s:", title); for(int i=0; i<count; ++i) { printf(" %g", elements[i]); } printf("\n"); }
void test_twiceArray(void) { printf("\n~~~~ %s() ~~~~\n", __func__); int valueCount=5; double *values=(double *)malloc(valueCount*sizeof(double)); if(values==NULL) { abort(); } for(int i=0; i<valueCount; ++i) { values[i]=1.1*(i+1); } displayArray("before", values, valueCount);
// ...use twiceArray() on values...
displayArray("after", values, valueCount); free(values); }
void displayArray(const char *title, const double *elements, int count) { printf("%s:", title); for(int i=0; i<count; ++i) { printf(" %g", elements[i]); } printf("\n"); }
void test_extendArray(void) { printf("\n~~~~ %s() ~~~~\n", __func__); int valueCount=5; double *values=(double *)malloc(valueCount*sizeof(double)); if(values==NULL) { abort(); } for(int i=0; i<valueCount; ++i) { values[i]=1.1*(i+1); } displayArray("before", values, valueCount);
// ...use extendArray() on values with an extension of 10...
displayArray("after", values, valueCount); free(values); }
//----------------------------------------------------------------------------
#ifndef EXTENSION_H #define EXTENSION_H
void concatArray(double **inout_left, int *inout_leftCount, const double *right, int rightCount);
void twiceArray(double **inout_values, int *inout_valueCount);
void extendArray(double **inout_values, int *inout_valueCount, int extension);
#endif // EXTENSION_H
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include "extension.h"
#include <stdlib.h>
void concatArray(double **inout_left, int *inout_leftCount, const double *right, int rightCount) { //-- load inout-parameters -- double *left=*inout_left; int leftCount=*inout_leftCount; //-- perform work -- const int offset=leftCount; leftCount+=rightCount; left=(double *)realloc(left, leftCount*sizeof(*left)); if(left==NULL) { abort(); } for(int i=0; i<rightCount; ++i) { left[offset+i]=right[i]; } //-- store out-parameters -- *inout_left=left; *inout_leftCount=leftCount; }
void twiceArray(double **inout_values, int *inout_valueCount) { //-- load inout-parameters -- double *values=*inout_values; int valueCount=*inout_valueCount; //-- perform work -- const int offset=valueCount; valueCount*=2; values=(double *)realloc(values, valueCount*sizeof(*values)); if(values==NULL) { abort(); } for(int i=0; i<offset; ++i) { values[offset+i]=values[i]; } //-- store out-parameters -- *inout_values=values; *inout_valueCount=valueCount; }
void extendArray(double **inout_values, int *inout_valueCount, int extension) { //-- load inout-parameters -- double *values=*inout_values; int valueCount=*inout_valueCount; //-- perform work -- const int offset=valueCount; valueCount+=extension; values=(double *)realloc(values, valueCount*sizeof(*values)); if(values==NULL) { abort(); } const double lastValue=values[offset-1]; for(int i=0; i<extension; ++i) { values[offset+i]=lastValue; } //-- store out-parameters -- *inout_values=values; *inout_valueCount=valueCount; }
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include "extension.h"
#include <stdio.h> #include <stdlib.h>
void displayArray(const char *title, const double *elements, int count) { printf("%s:", title); for(int i=0; i<count; ++i) { printf(" %g", elements[i]); } printf("\n"); }
void test_concatArray(void) { printf("\n~~~~ %s() ~~~~\n", __func__); int aCount=10; double *a=(double *)malloc(aCount*sizeof(double)); if(a==NULL) { abort(); } for(int i=0; i<aCount; ++i) { a[i]=1.1*i; } const double b[]={100.0, 102.2, 104.4, 106.6, 108.8}; const int bCount=(int)(sizeof(b)/sizeof(b[0])); displayArray("a before", a, aCount); displayArray("b before", b, bCount); concatArray(&a, &aCount, b, bCount); displayArray("a after", a, aCount); displayArray("b after", b, bCount); free(a); }
void test_twiceArray(void) { printf("\n~~~~ %s() ~~~~\n", __func__); int valueCount=5; double *values=(double *)malloc(valueCount*sizeof(double)); if(values==NULL) { abort(); } for(int i=0; i<valueCount; ++i) { values[i]=1.1*(i+1); } displayArray("before", values, valueCount); twiceArray(&values, &valueCount); displayArray("after", values, valueCount); free(values); }
void test_extendArray(void) { printf("\n~~~~ %s() ~~~~\n", __func__); int valueCount=5; double *values=(double *)malloc(valueCount*sizeof(double)); if(values==NULL) { abort(); } for(int i=0; i<valueCount; ++i) { values[i]=1.1*(i+1); } displayArray("before", values, valueCount); extendArray(&values, &valueCount, 10); displayArray("after", values, valueCount); free(values); }
int main(void) { test_concatArray(); test_twiceArray(); test_extendArray(); return 0; }
//----------------------------------------------------------------------------