# 12-3 Exercise 2

### Function Definitions ###

# Function build_range(x,y,size)
# x=first value, y=highest last value, size=increment value
# Returns a vector starting at x with increment size, 
# with the final value no higher than y

build_range <- function(x,y,size)
{
  outvector<-seq(x,y,by=size)
  return(outvector)
}

# Function print_squares(v, limit)
# v=vector of numerical values, limit=maximum value to be printed
# Prints the squares of the vector elements
# No return object

print_squares <- function(v, limit)
{
  for (i in v) if (i^2 <= limit) print(i^2)
}

# Function print_limit(x,y,size,limit)
# x=first value, y=highest last value, 
# size=increment value, limit=max value
# Prints out the squares of every size-th number between x and y
# providing that square is less than limit

print_limit <- function(x,y,size,limit)
{
  numberlist<-build_range(x,y,size) # use build_range to generate
                                    # the input vector for print_squares
  print_squares(numberlist,limit) # call print_squares
}

# The following version is more elegant :)

#print_limit <- function(x,y,size,limit)
#{
#  print_squares(build_range(x,y,size),limit) # function calls function
#}

### Main Script ###

# Test build_range
build_range(4,13,4)

# Test print_squares
print_squares(3:9,40)

# Test print_limit
print_limit(2,9,3,30)

# What happens if we use floats instead of integers?
print_limit(2.1,15.6,2.5,35.2)
