# 12-2 Exercise 1

### Function Definitions ###

# Function mean_mean(x)

mean_mean <- function(x)
{
  out <- mean(x) # Calculate using mean()
  out # Return out
}

# Function mean_loop(x)

mean_loop <- function(x)
{
  running_total<-0 # Initialise for calculating sum
  element_total<-0 # Initialise for counting number of elements
  
  for (i in x) # for each element in the vector passed in
  {
    running_total<-running_total+i # Add the element to the running total
    element_total<-element_total+1 # Increment the number of elements
  }
  out<-running_total/element_total # Calculate the mean
  out # Return out
}

# Function mean_function(x)

mean_function <- function(x)
{
  out<-sum(x)/length(x) # Calculate mean using vector functions
  out # Return out
}

### Main script ###

# Create test vectors

int_test<-1:5
float_test<-c(1.2,2.5,3.5,4.5,5.5)

# Test mean_mean

mean_mean(int_test)
mean_mean(float_test)

# Test mean_loop

mean_loop(int_test)
mean_loop(float_test)

# Test mean_function

mean_function(int_test)
mean_function(float_test)

