Loading lang_c_ex_0702...
double *data=(double *)malloc(count*sizeof(double)); if(data==NULL) { abort(); } srand((unsigned int)(size_t)&count); // rely on ASLR for randomisation for(int i=0; i<count; ++i) { data[i]=0.01*(rand()%100); }
void test_DataSet(void) { printf("\n~~~~ %s() ~~~~\n", __func__); // ... }
char *nucleotides=(char *)malloc(length*sizeof(char)); if(nucleotides==NULL) { abort(); } const char *AGCT="AGCT"; srand((unsigned int)(size_t)&length); // rely on ASLR for randomisation for(int i=0; i<length; ++i) { nucleotides[i]=AGCT[rand()%4]; }
void test_Gene(void) { printf("\n~~~~ %s() ~~~~\n", __func__); // ... }
int *identifiers=(int *)malloc(size*sizeof(int)); if(identifiers==NULL) { abort(); } srand((unsigned int)(size_t)&size); // rely on ASLR for randomisation for(int i=0; i<size; ++i) { identifiers[i]=rand()%1000; }
void test_UserBase(void) { printf("\n~~~~ %s() ~~~~\n", __func__); // ... }
//----------------------------------------------------------------------------
#ifndef COMPLEXTYPES_H #define COMPLEXTYPES_H
typedef struct { int count; double *data; } DataSet;
DataSet DataSet_create(int count);
void DataSet_destroy(DataSet *ds);
double DataSet_mean(const DataSet *ds);
void DataSet_scale(DataSet *ds, double factor);
typedef struct { int length; char *nucleotides; } Gene;
Gene Gene_create(int length);
void Gene_destroy(Gene *g);
void Gene_dnaToRna(Gene *g);
int Gene_count(const Gene *g, char nucleotide);
typedef struct { int size; int *identifiers; } UserBase;
UserBase UserBase_create(int size);
void UserBase_destroy(UserBase *ub);
void UserBase_revoque(UserBase *ub);
int UserBase_privileged(const UserBase *ub);
#endif // COMPLEXTYPES_H
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include "complexTypes.h"
#include <stdlib.h>
DataSet DataSet_create(int count) { double *data=(double *)malloc(count*sizeof(double)); if(data==NULL) { abort(); } srand((unsigned int)(size_t)&count); // rely on ASLR for randomisation for(int i=0; i<count; ++i) { data[i]=0.01*(rand()%100); } DataSet ds={count, data}; return ds; }
void DataSet_destroy(DataSet *ds) { free(ds->data); ds->count=0; ds->data=NULL; }
double DataSet_mean(const DataSet *ds) { double sum=0.0; const int count=ds->count; const double *data=ds->data; for(int i=0; i<count; ++i) { sum+=data[i]; } return sum/count; }
void DataSet_scale(DataSet *ds, double factor) { const int count=ds->count; double *data=ds->data; for(int i=0; i<count; ++i) { data[i]*=factor; } }
Gene Gene_create(int length) { char *nucleotides=(char *)malloc(length*sizeof(char)); if(nucleotides==NULL) { abort(); } const char *AGCT="AGCT"; srand((unsigned int)(size_t)&length); // rely on ASLR for randomisation for(int i=0; i<length; ++i) { nucleotides[i]=AGCT[rand()%4]; } Gene g={length, nucleotides}; return g; }
void Gene_destroy(Gene *g) { free(g->nucleotides); g->length=0; g->nucleotides=NULL; }
void Gene_dnaToRna(Gene *g) { const int length=g->length; char *nucleotides=g->nucleotides; for(int i=0; i<length; ++i) { if(nucleotides[i]=='T') { nucleotides[i]='U'; } } }
int Gene_count(const Gene *g, char nucleotide) { int count=0; const int length=g->length; const char *nucleotides=g->nucleotides; for(int i=0; i<length; ++i) { if(nucleotides[i]==nucleotide) { ++count; } } return count; }
UserBase UserBase_create(int size) { int *identifiers=(int *)malloc(size*sizeof(int)); if(identifiers==NULL) { abort(); } srand((unsigned int)(size_t)&size); // rely on ASLR for randomisation for(int i=0; i<size; ++i) { identifiers[i]=rand()%1000; } UserBase ub={size, identifiers}; return ub; }
void UserBase_destroy(UserBase *ub) { free(ub->identifiers); ub->size=0; ub->identifiers=NULL; }
void UserBase_revoque(UserBase *ub) { const int size=ub->size; int *identifiers=ub->identifiers; for(int i=0; i<size; ++i) { if(identifiers[i]<100) { identifiers[i]=-1; } } }
int UserBase_privileged(const UserBase *ub) { int count=0; const int size=ub->size; const int *identifiers=ub->identifiers; for(int i=0; i<size; ++i) { const int id=identifiers[i]; if((id>=0)&&(id<100)) { ++count; } } return count; }
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include "complexTypes.h"
#include <stdio.h>
void test_DataSet(void) { printf("\n~~~~ %s() ~~~~\n", __func__); DataSet ds=DataSet_create(10000); printf("mean before: %g\n", DataSet_mean(&ds)); DataSet_scale(&ds, 4.0); printf("mean after: %g\n", DataSet_mean(&ds)); DataSet_destroy(&ds); }
void test_Gene(void) { printf("\n~~~~ %s() ~~~~\n", __func__); Gene g=Gene_create(10000); printf("T count before: %d\n", Gene_count(&g, 'T')); printf("U count before: %d\n", Gene_count(&g, 'U')); Gene_dnaToRna(&g); printf("T count after: %d\n", Gene_count(&g, 'T')); printf("U count after: %d\n", Gene_count(&g, 'U')); Gene_destroy(&g); }
void test_UserBase(void) { printf("\n~~~~ %s() ~~~~\n", __func__); UserBase ub=UserBase_create(10000); printf("priviledged before: %d\n", UserBase_privileged(&ub)); UserBase_revoque(&ub); printf("priviledged after: %d\n", UserBase_privileged(&ub)); UserBase_destroy(&ub); }
int main(void) { test_DataSet(); test_Gene(); test_UserBase(); return 0; }
//----------------------------------------------------------------------------