CS 245 - Programming Languages

Lab 3

Little Go Programs

In this lab you will write, and or analyze a series of small programs in Go. The intent here is simply to get you some practice with Go before starting on the first programming assignment.

Fizz Buzz

Write a program that prints the numbers 1 .. 100 except: if the number is divisible by 3 print "Fizz", if the number is divisible by 5 print "Buzz", if the number is divisible by 3 and 5 print "FizzBuzz".You should have at least one function other than main.

K to M conversion

Write a program that takes input from the command line to convert miles to kilometers or kilometers to miles. (1 mile equals 1.609km). For instance, if your command line was
        go run convert.go 10 k
    
then the output would be "6.14 miles". Conversely, if your command line was
        go run convert.go 5 m
    
then the output should be "8.05 k" For this program you will need to use the ParseFloat function in the strconv package. You should look up how to do this.

Getting command line input in easy in Go, if different from Java. See this program for an example. In addition you will need to convert the strings you received from the command line into numbers. See this program for an example.

Slice analysis

Look at this program. It has three functions that do much the same thing. Answer the following questions about this program (either by visual inspection or by adding code to rnums.go that illustrate the program's operation)
  1. Why do I comment that rrslice is less efficient than rslice?
  2. What is r3slice said to suck?
  3. How would you fix r3slice?

Manipulating slices

Take the go program fro the previous section and add a new function. (You might also want to remove the rrslice and r3slice functions and their uses.) The function you write should take an integer slice and a number as parameters. The function should return a subslice of the original slice such that the subslice has the smallest sum of any contiguous subslice of the original slice. For instance, given a slice containing [1,6,2,3,4] and a number 2, you should return the subslice containing [2,3] as that is the portion of the original slice of length 2 has the smallest sum. (This is MUCH easier in Go than Java.)

Student Structs

Write a new Go program that does the following:
  1. Create a struct that holds a date (month, day and year).
  2. Create a String() method for this struct that results in a nice format for the date. (See the String method in here for an example.
  3. Create a struct that holds information about a student: specifically: name, id and birth date. You should use the Date struct in your student struct.
  4. Initialize several instances of the student struct. Use only one statement to initialize
  5. Print these instances

Mapped students

Take your student structs from the previous problem and put them into a map that is indexed by the student id. When done, simply print the map.

What to turn in

When you are done send email to gtowell@brynmawr.edu with each of the Go programs you wrote and answers to the "slice analysis" questions. As usual for labs, you are only expected to spend 80 minutes on this; so send what you have at the end of that time.