# SI 204 Spring 2017 / HWs

This is the archived website of SI 204 from the Spring 2017 semester. Feel free to browse around; you may also find more recent offerings at my teaching page.

# Homework 28: Struct search

Name: _____________________________________________ Alpha: ___________________

• Due before class on Friday, March 31
• This homework contains code to be submitted electronically. Put your code in a folder called hw28 and submit using the 204sub command.
• This is a written homework - be sure to turn in a hard-copy of your completed assignment before the deadline. Use the codeprint command to print out your code and turn that in as well.

# Assignment

1. Circle one to indicate how you did for the reading assignment from Homework 26 before class on Monday:

How carefully did you complete the reading? (Circle one)

Not at all
Skimmed it
2. Circle one to indicate how you did for the reading assignment from Homework 27 before class on Wednesday:

How carefully did you complete the reading? (Circle one)

Not at all
Skimmed it
3. Given the following declarations:
struct game {
char first[128];
char last[128];
int score;
};

struct point {
double x;
double y;
};

int i;
double w;
struct point p;
struct game g;
fill in the following table with the type (only) of each expression. Write ERROR for if the expression would be a compiler or run-time error.
 expression type (or ERROR) g g.first g.x p.x score g.first[i] w + g.score g.score++ *g.last
4. Consider the following main method:
int main() {
struct room r;
scanf(" %s %i %c", r.bldg, &r.num, &r.type);

if (isOffice(r)) {
r.sqft = 120.0;
} else if (isCloset(r.num, r.type)) {
r.sqft = 62.5;
} else {
r.sqft = 300;
}

printRoom(r, stdout);

return 0;
}
Write a definition for struct room as well as prototypes for the three functions isOffice, isCloset, and printRoom, based on how they are used in the code above.
5. Write a program topscores.c that reads in player names and game scores for some game, and prints out the names in alphabetical order along with the highest score that player achieved in the game.

The input to your program will come from a file like scores1.txt or scores2.txt, with the number of scores in the file, followed by a list of that many names and scores. You can assume that a name will always be in two parts (first and last name).

You must create a struct to hold a single player's name and score, and use that to read in and organize your data. Beyond that, you are free to solve the problem in whatever way you think is best, and there are multiple good ways to do it. One good way is to first read in all of the scores, then sort by name (alphabetically) and breaking ties by score (highest first). Once the data is sorted in this way, printing out the top score for each player should be a much easier task.

Note: if you get stuck on creating the array of structs, you might want to read ahead to section 4.1 in the Unit 8 notes.

Example runs:

roche@ubuntu$./topscores filename: scores1.txt Betty Johnson 2400 Andy Smith 800 Betty Smith 2300 roche@ubuntu$ ./topscores
filename: scores2.txt
Andy Brown 2200
Betty Brown 1900
Casey Brown 1500
Devon Brown 2400
Andy Johnson 700
Betty Johnson 2200
Casey Johnson 800
Devon Johnson 1600
Andy Smith 1800
Betty Smith 2100
Casey Smith 900
Devon Smith 2300
Andy Williams 1800
Betty Williams 1600
Casey Williams 800
Devon Williams 2200