#include <stdio.h>
#include <stdlib.h>

// creates an array of doubles that has the specified length,
// and asks the user to enter a value for each element
// in the array.
double * read_double_array(int length)
{
  // create an array of doubles. The number of elements
  // in the array is specified by the value of length
  // (which was specified by the user)
  double * numbers = (double *) malloc(sizeof(double) * length);
  
  // ask the user to enter a value for each element in the array.
  int i;
  for (i = 0; i < length; i++)
  {
    printf("please enter a value for element %d:\n", i);
    scanf("%lf", &numbers[i]);
  }

  return numbers;
}


// prints the elements of the array n. 
// length is the number of elements in the array n.
void print_double_array(double * a, int length)
{
  int i;
  for (i = 0; i < length; i++)
  {
    printf("a[%d] = %lf\n", i, a[i]);
  }
}


int main()
{
  int length;
  printf("how many numbers do you want to enter?\n");
  scanf("%d", &length);

  double * numbers = read_double_array(length);

  print_double_array(numbers, length);


  // for every array that we create with malloc, we must
  // destroy it with free, when we don't need the array anymore.
  free(numbers);
}

