Stats Calculator
Here is an example of how we can use the dynamic array to implement the statistics calculator.
#include <stdio.h>#include "dynamic-array.hpp"
/** * Read an integer from the user * * @param prompt the message to show the user * @returns the integer entered */int read_integer(const char *prompt){ int result = 0;
printf("%s", prompt); while (scanf(" %d", &result) != 1) // Read value, and try to convert to int { // Convert failed, as input was not a number scanf("%*[^\n]"); // Read past the end of the current line printf("Please enter a whole number.\n"); printf("%s", prompt); }
return result;}
/** * Read a double value from the user * * @param prompt the message to show the user * @returns the double entered */double read_double(const char *prompt){ double result = 0;
printf("%s", prompt); while (scanf(" %lf", &result) != 1) // Read value, and try to convert to int { // Convert failed, as input was not a number scanf("%*[^\n]"); // Read past the end of the current line printf("Please enter a whole number.\n"); printf("%s", prompt); }
return result;}
/** * Populate the array with values entered by the user * * @param data the array of values */void populate_array(dynamic_array<double> *data){ int size = read_integer("How many values do you want to enter? ");
for (int i = 0; i < size; i++) { add(data, read_double("Enter value: ")); }}
/** * Output the values in the array * * @param data the array of values */void print(const dynamic_array<double> *data){ for (int i = 0; i < data->size; i++) { printf("%d: %lf\n", i, get(data, i, 0.0)); }}
/** * Calculate the sum of the values in the array * * @param data the array of values * @return the sum of the values */double sum(const dynamic_array<double> *data){ int i; double result = 0;
for (i = 0; i < data->size; i++) { result += get(data, i, 0.0); }
return result;}
/** * Calculate the mean of the values in the array * * @param data the array of values * @returns the mean of the values */double mean(const dynamic_array<double> *data){ if (data->size > 0) return sum(data) / data->size; else return 0;}
/** * Calculate the largest value in the array * * @param data the array of values * @returns the largest value, or 0 if there are no values */double max(const dynamic_array<double> *data){ // Ensure there is data if (data->size == 0) return 0;
// Assume the first value is the largest double result = get(data, 0, 0.0);
// Check the rest of the values for (int i = 1; i < data->size; i++) { if (get(data, i, 0.0) > result) { result = get(data, i, 0.0); } }
return result;}
/** * Add a value to the array * * @param data the array of values */void add_data(dynamic_array<double> *data){ double value = read_double("Enter a value to add: ");
if (!add(data, value)) { printf("Sorry, out of memory\n"); }}
/** * Remove a value from the array * * @param data the array of values */void remove_value(dynamic_array<double> *data){ print(data);
int index = read_integer("Enter the index of the value to remove: ");
if (index >= 0 && index < data->size) { for (int i = index; i < data->size - 1; i++) { set(data, i, get(data, i + 1, 0.0)); } resize(data, data->size - 1); } else { printf("Sorry, that is not a valid index.\n"); }}
// Implements a statistics calculator. The program reads in values entered by the user// and then calculates the sum, mean, variance, and maxint main(){ dynamic_array<double> *data = new_dynamic_array<double>(10);
populate_array(data);
printf("\nCalculating statistics...\n\n");
printf("Sum: %4.2f\n", sum(data)); printf("Mean: %4.2f\n", mean(data)); printf("Max: %4.2f\n", max(data));
add_data(data);
print(data);
remove_value(data);
print(data);
delete_dynamic_array(data);
return 0;}