#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)
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");
* 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)
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");
* 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++)
data.add(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, data[i]);
* 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)
for (i = 0; i < data.size; i++)
* 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)
return sum(data) / data.size;
* 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)
// Assume the first value is the largest
// Check the rest of the values
for (int i = 1; i < data.size; i++)
* 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: ");
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)
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++)
data.resize(data.size - 1);
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 max
dynamic_array<double> data(10, 0.0);
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));