Homework 28: Struct search
Name: _____________________________________________ Alpha: ___________________
Describe help received: ________________________________________________________
- Due before class on Friday, March 31
- This homework contains code to be submitted electronically.
Put your code in a folder called
hw28and submit using the204subcommand. - This is a written homework - be sure to turn in a hard-copy
of your completed assignment before the deadline. Use the
codeprintcommand to print out your code and turn that in as well.
Assignment
-
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 allSkimmed itRead someRead all -
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 allSkimmed itRead someRead all - Given the following declarations:
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.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;expression type (or ERROR) gg.firstg.xp.xscoreg.first[i]w + g.scoreg.score++*g.last -
Consider the following
mainmethod:
Write a definition forint 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; }struct roomas well as prototypes for the three functionsisOffice,isCloset, andprintRoom, based on how they are used in the code above. -
Write a program
topscores.cthat 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$./topscoresfilename:scores1.txtBetty Johnson 2400Andy Smith 800Betty Smith 2300roche@ubuntu$./topscoresfilename:scores2.txtAndy Brown 2200Betty Brown 1900Casey Brown 1500Devon Brown 2400Andy Johnson 700Betty Johnson 2200Casey Johnson 800Devon Johnson 1600Andy Smith 1800Betty Smith 2100Casey Smith 900Devon Smith 2300Andy Williams 1800Betty Williams 1600Casey Williams 800Devon Williams 2200