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


int count_even_numbers(int * numbers, int length)
{
  int sum = 0;
  int i;
  for (i = 0; i < length; i++)
  {
    if (numbers[i] % 2 == 0)
    {
      sum = sum+1;
    }
  }

  return sum;
}

// creates an array of integers that has the specified length,
// and asks the user to enter a value for each element
// in the array.
int * read_int_array(int length)
{
  // create an array of ints. The number of elements
  // in the array is specified by the value of length
  // (which was specified by the user)
  int * numbers = (int *) malloc(sizeof(int) * 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("%d", &numbers[i]);
  }

  return numbers;
}


// prints the elements of the array n. See bubblesort1 
// for an alternative version of this function.
// length is the number of elements in the array n.
void print_int_array(int * a, int length)
{
  int i;
  for (i = 0; i < length; i++)
  {
    printf("a[%d] = %d\n", i, a[i]);
  }
}


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

  int * numbers = read_int_array(length);

  print_int_array(numbers, length);

  int result = count_even_numbers(numbers, length);
  printf("result = %d\n", result);

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

