CSE100 Principles of Programming with C++ Lab 5 :: 5 pts
PPG Points per game. PTS divided by GP. RPG Rebounds per game. REB divided by GP. FG% Field goal percentage. FGM divided by FGA. FT% Free throw percentage. FTM divided by FTA. 3P% Three point field goal percentage. 3P divided by 3A.
For this particular stats-season.txt input file, the contents of stats-game.txt will be, Name GP PPG RPG FG% FT% 3P% F.Flintstone 23 11.0 4.3 0.463 0.610 0.365 B.Rubble 23 10.4 4.1 0.401 0.849 0.286 W.Flintstone 14 10.1 5.9 0.554 0.630 0.000 H.Simpson 23 9.4 6.2 0.653 0.579 0.333 N.Flanders 23 7.2 2.2 0.449 0.882 0.407 N.Explosion 23 7.0 2.7 0.486 0.643 0.484 K.Broflovski 21 6.0 1.8 0.310 0.722 0.276 E.Cartman 18 4.6 1.2 0.424 0.833 0.459 G.Jetson 17 4.4 2.2 0.448 0.655 0.188 S.Skwigelf 18 3.3 1.1 0.442 0.720 0.333 T.Wartooth 22 3.1 2.1 0.322 0.575 0.304
Note that PPG and RPG are printed with 1 digit after the decimal point and percentages are displayed with 3 digits after the decimal points. The names are printed left-justified in a column and the numerical values are printed right-justified in each column.
4.2 Software Design Here is the structure chart for the program,
main() first calls ReadHeader() passing the file input stream object fin as an argument2; ReadHeader() is a void function which returns nothing. Next, main() calls PrintHeader(), again, passing fin as the argument; PrintHeader() is also a void function. Finally, main() calls PrintStats() passing fin and the file output stream object fout as the arguments; PrintStats() is also a void function. Here is the pseudocode,
Program Lab5 Function main () Returns 0
Define an ifstream object named fin and open “stats-season.txt” for reading Verify that fin was successfully opened; if not, terminate the program Define an ofstream object named fout and open “stats-game.txt” for writing Configure fout so real numbers will be printed in fixed notation Call ReadHeader(fin) Call PrintHeader(fout) Call PrintStats(fin, fout) Close fin Close fout Return 0
End Function main Function PrintHeader (pFout : ofstream by-ref) Returns Nothing
Print “Name” left-justified in a field of width 16 Print “GP” right-justified in a field of width 4 Print “PPG” right-justified in a field of width 6 Print “RPG” right-justified in a field of width 6 Print “FG%” right-justified in a field of width 7 Print “FT%” right-justified in a field of width 7 Print “3P%” right-justified in a field of width 7 Print newline
End Function PrintHeader
2 Stream variables are passed to functions using a C++ parameter passing technique known as pass by-reference. We will discuss pass by-reference later in the course, but if you look at the code you will see the function header for ReadHeader() is void ReadHeader(ifstream& pFin). The & indiccates that fin is being passed by-reference. Similary, fout is passed to PrintStats() by-reference as well. Stream objects such as cin, cout, fin, and fout must always be passsed by-ref.
(c) Burger :: Computer Science & Engineering :: Arizona State University Page 2
CSE100 Principles of Programming with C++ Lab 5 :: 5 pts
Function PrintStats (fin : ifstream by ref, fout: ofstream by-ref) Returns Nothing Loop 11 times — we will discuss what a loop is later in the course. Don’t worry about it for now.
Define string object named name and read player name from fin into name Define int variables gp, fgm, fga, fta, threem, threea, pts, reb Read from fin into gp, fgm, fga, ftm, fta, threem, threea, pts, reb Double fgp ← fgm ÷ fga — Hint: typecast fgm to double Double ftp ← ftm ÷ fta — Hint: typecast ftm to double Double threep ← threem ÷ threea — Hint: typecast threem to double Double ppg ← pts ÷ gp — Hint: typecast pts to double Double rpg ← reb ÷ gp — Hint: typecast reb to double Print name left-justified in a field of width 16 to fout Print gp right-justified in a field of width 4 with 1 digit after the decimal pt to fout Print ppg right-justified in a field of width 6 with 1 digit after the decimal pt to fout Print rpg right-justified in a field of width 6 with 1 digit after the decimal pt to fout Print fgp right-justified in a field of width 7 with 3 digits after the decimal pt to fout Print ftp right-justified in a field of width 7 with 3 digits after the decimal pt to fout Print threep right-justified in a field of width 7 with 3 digits after the decimal pt to fout Print newline
End Loop End Function PrintStats Function ReadHeader (fin : ifstream by-ref) Returns Nothing
Define string object named header Read a line of text from fin into header
End Function ReadHeader End Program Lab5
4.3 Additional Programming Requirements 1. Update the header comment block in the source code template with your author information, your lab date and time, your lab
TA, and the two test cases you are to write for the prelab exercise. 2. Carefully format your code and follow the indentation of the text as shown in the example programs of the textbook.
5 What to Submit for Grading and by When Upload the Lab05.cpp C++ source code file to Blackboard using the lab submission link by the deadline. If your program does not compile or run correctly, upload what you have completed for grading anyway (you will generally receive some partial credit for effort). The deadline for the complete lab project is 4:00am Sat 10 Oct. Consult the online syllabus for the late and academic integrity policies.
6 Grading Rubric 1. Lab Exercise Program (0 to 5 pts) a. If the submitted program does, or does not, compile and the student completed less than 50% of the required code correctly, assign +2 pts. c. If the submitted program does not compile and the student completed more than 50% of the required code correctly, assign +3 pts. d. If the submitted program compiles and the student completed more than 50% of the required code correctly, assign +4 pts. e. If the submitted program compiles and is implemented perfectly, or close to perfect with only one or two minor mistakes, assign +5 pts.
2. Deadline was 4:00am Sat 10 Oct 1. Assign 20% bonus calculated on the earned pts for a submission prior to 4:00am Thu 8 Oct. 2. Assign 10% bonus calculated on the earned pts for a submission between 4:00am Thu 8 Oct and 4:00am Fri 9 Oct. 3. Deduct 0.5 pt for a submission between 4:00am Sat 10 Oct and 4:00am Sun 11 Oct. 4. Deduct 1 pt for a submission after 4:00am Sun 11 Oct.
(c) Burger :: Computer Science & Engineering :: Arizona State University Page 3