problems 7 and 8 in R