/*
problem3.c
Driver function for Problem 3.
Skeleton written by Grady Fitzpatrick for COMP20007 Assignment 1 2021 /* Constants */ /* Used to store all the statistics for a single chip. */ /* Used to store all the statistics for both chips for each problem. */ struct statistics { struct chipStatistics { /* Set all statistics to 0s */ /* Collects the minimum, average and maximum operations from running all /* Divides the number of operations by the number of instances. */ /* Prints out the minimum, average and maximum operations from given /* Calculates the number of operations required for Euclid’s algorithm given the /* Calculates the number of operations required for the sieve of Eratosthenes int main(int argc, char **argv){ collectStatistics(&summaryStatistics, MAXNUMERATOR, MAXDENOMINATOR); printf(“Old chip (Euclid):\n”); return 0; void collectStatistics(struct chipStatistics *chipStats, int maxNumerator, for(numerator = 1; numerator <= maxNumerator; numerator++){
for(denominator = 1; denominator <= maxDenominator; denominator++){
/* Run algorithms for all combinations of numerator and denominator. */
euclid(numerator, denominator, OLDCHIP,
&(chipStats->oldChipEuclid)); void calculateAverage(struct statistics *stats){ void initialiseStatistics(struct statistics *stats){ void euclid(int numerator, int denominator, int chip, struct statistics *s){ void eratosthenes(int numerator, int denominator, int chip, void printStatistics(struct statistics *stats){
*/
#include
#include
#include
#include
#include
#define OLDCHIP 0
#define NEWCHIP 1
#define MAXNUMERATOR 100
#define MAXDENOMINATOR 100
struct statistics;
struct chipStatistics;
int operations;
int instances;
int minOperations;
double avgOperations;
int maxOperations;
};
struct statistics oldChipEuclid;
struct statistics newChipEuclid;
struct statistics oldChipSieve;
struct statistics newChipSieve;
};
void initialiseStatistics(struct statistics *stats);
combinations of numerators from 1 to the given maxNumerator and 1 to the given
maxDenominator. */
void collectStatistics(struct chipStatistics *chipStats, int maxNumerator,
int maxDenominator);
void calculateAverage(struct statistics *stats);
statistics. */
void printStatistics(struct statistics *stats);
numerator and denominator when running on the given chip type (one of OLDCHIP
and NEWCHIP) by moving through the steps of the algorithm and counting each
pseudocode operation. */
void euclid(int numerator, int denominator, int chip, struct statistics *s);
given the numerator and denominator when running on the given chip type (one of
OLDCHIP and NEWCHIP) by moving through the steps of the algorithm and counting
each pseudocode operation. */
void eratosthenes(int numerator, int denominator, int chip,
struct statistics *s);
struct chipStatistics summaryStatistics;
printStatistics(&(summaryStatistics.oldChipEuclid));
printf(“\n”);
printf(“New chip (Euclid)\n”);
printStatistics(&(summaryStatistics.newChipEuclid));
printf(“\n”);
printf(“Old chip (Sieve)\n”);
printStatistics(&(summaryStatistics.oldChipSieve));
printf(“\n”);
printf(“New chip (Sieve)\n”);
printStatistics(&(summaryStatistics.newChipSieve));
printf(“\n”);
}
int maxDenominator){
int numerator, denominator;
/* Initialise all statistics */
initialiseStatistics(&(chipStats->oldChipEuclid));
initialiseStatistics(&(chipStats->newChipEuclid));
initialiseStatistics(&(chipStats->oldChipSieve));
initialiseStatistics(&(chipStats->newChipSieve));
euclid(numerator, denominator, NEWCHIP,
&(chipStats->newChipEuclid));
eratosthenes(numerator, denominator, OLDCHIP,
&(chipStats->oldChipSieve));
eratosthenes(numerator, denominator, NEWCHIP,
&(chipStats->newChipSieve));
}
}
calculateAverage(&(chipStats->oldChipEuclid));
calculateAverage(&(chipStats->newChipEuclid));
calculateAverage(&(chipStats->oldChipSieve));
calculateAverage(&(chipStats->newChipSieve));
}
stats->avgOperations = (double) stats->operations / stats->instances;
}
stats->operations = 0;
stats->instances = 0;
stats->minOperations = INT_MAX;
stats->avgOperations = 0;
stats->maxOperations = 0;
}
/* IMPLEMENT THIS */
}
struct statistics *s){
/* IMPLEMENT THIS */
}
printf(“Minimum operations: %d\n”, stats->minOperations);
printf(“Average operations: %f\n”, stats->avgOperations);
printf(“Maximum operations: %d\n”, stats->maxOperations);
}