SI 204 Spring 2017 / HWs


Homework 27: Structs

Name: _____________________________________________ Alpha: ___________________

Describe help received: ________________________________________________________

  • Due before class on Wednesday, March 29
  • This homework contains code to be submitted electronically. Put your code in a folder called hw27 and submit using the 204sub command.
  • This is an electronic homework and you do not need to print out anything to turn in during class.

Assignment

  1. Required reading: Sections 1 (Intro), 2 (Creating new types), and 3 (Using structs) from the Unit 8 notes. You can skip over sections 2.3 and 2.4 if you don't have time to read it all.
  2. Write a program called race.c that reads in 2 names (single strings) and race times, and prints out the name and time of the winner.

    Of course you could write such a program without using structs at all, but the purpose of this assignment is to give you practice with structs. So you must create and use:

    • A struct racetime that contains two fields, one for the name of the racer and one for their time.
    • A function print that takes a single racetime struct and prints out "NAME won with a time of TIME minutes."

    Example runs:

    roche@ubuntu$ ./race
    Enter the names and times of two racers:
    Jamal 32.7
    Vince 6.8
    Vince won with a time of 6.8 minutes.
    roche@ubuntu$ ./race
    Enter the names and times of two racers:
    Julie 5.7
    Margaret 5.9
    Julie won with a time of 5.7 minutes.