DOC PREVIEW
Yale CPSC 427 - Problem Set 1
School name Yale University
Pages 4

This preview shows page 1 out of 4 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Assignment GoalsProblemsWritten PartProgramming PartProgramming NotesDeliverablesYALE UNIVERSITYDEPARTMENT OF COMPUTER SCIENCECPSC 427a: Object-Oriented Programming Handout #2Professor M. J. Fischer September 8, 2011Problem Set 1Due before midnight on Friday, September 16, 2011.1 Assignment Goals1. To familiarize yourself with the tools needed for this course: the Zoo computer facility,your Zoo course account, a good text editor or IDE, the C++ compiler and linker (g++),a Linux command shell, and basic Linux commands.2. To learn how to compile, link, and run a multimodule C++ program.3. To learn how to use the submit script to submit your assignment.4. To learn to distinguish programming constructs for describing computation from thoseused to control, modularize, and constrain the code.5. To have the experience of repurposing existing code.2 ProblemsThe class demo 02-InsertionSortCpp illustrates how a C++ class and multiple source filescan be used to put structure on a program. In this problem set, you will be asked to readthe code carefully in order to distinguish which parts of the program support modularity,code reuse, and robustness, and which parts comprise the actual executable code. You willthen be asked to repurpose the code for a different but related application.2.1 Written PartRecall from class that the insertion sort demo does the following:1. It prints a banner.2. It prompts the user to enter a file name.3. It opens the specified file name and reads up to LENGTH floats from it or until end-of-fileis reached (or a read error occurs), storing the list of numbers read.4. It prints the list of numbers.5. It uses insertion sort to sort the list. (N.B. Insertion sort is an O(n2) algorithm andis only suitable for use on relatively short lists.)6. It prints the sorted list.7. It frees storage and exits.2 Problem Set 1This isn’t a particularly interesting application in its own right except to serve as a unittest for verifying the correctness of the sort function.Many people, when asked to write a program to do what is described above, would comeup with a monolithic program such as you will find in 02-InsertionSortMonolith, whereeverything is contained in main() (which you will find in the file sort.cpp). Indeed, thisprogram does exactly the same thing as 02-InsertionSortCpp (with one minor difference),but the code looks very different.For this problem, I want you to print out files sort.cpp from 02-InsertionSort-Monolith, and files main.cpp, datapack.hpp, and datapack.cpp from 02-Insertion-SortCpp. Then for each line in sort.cpp, find whether or not that line appears in one ofthe other files, and if so, highlight it there. If it appears but in a slightly altered form,highlight it in a different color (or otherwise indicate that it corresponds but has beenchanged slightly).Now, the lines that are not highlighted in the demo files are the ones that do not appearin the monolithic version. These lines are there for the purpose of putting structure onthe code. You will see that they define subunits such as classes, function declarations, andfunction definitions. Now go back to sort.cpp and mark the lines that belong to the samesubunit in the demo program. For example, you might use the notation “sd” to mark allof the lines in sort.cpp that came from the definition of DataPack::sortData(). I don’tcare what notation you use as long as it is clear and unambiguous.After you have identified the code-structuring parts of the demo program, write a briefparagraph on each, describing how its use contributes (or not) to the goals of modular,reusable, robust programming. Also, for any lines that appear in both versions of the codebut with modifications, describe why the monolithic version of the program would not workif the lines were the same as in the demo program.2.2 Programming PartThe code in 02-InsertionSortCpp reads in a file of floats and prints it out in both fileorder and in increasing order. You are to repurpose this code to get a program that readsin a file of baseball statistics and prints it out in both file order and in decreasing order ofbatting average.A sample statistics data file data.csv is provided in the Zoo directory/c/cs427/assignments/ps1. This file consists of two kinds of lines:1. Comment lines, which begin with a ’#’ character.2. Stat lines, which consist of three tab-delimited fields: name, batting average, andyear. Each describes a player’s batting performance in the given year.1In the original code, each data record consisted of a single float. In the stats file, eachrecord contains three fields. You will need to define a class Player in which to store arecord. Just as a float can be read and printed using the C++ I/O operators >> and <<,a Player can be read and printed using those same operators after you have defined themappropriately. Similarly, you will need to define the comparison operator <= to comparetwo Players for use in sorting them. Since the sort function in DataPack puts the smallestelement first, then the “smallest” player should be the one with the largest batting average.Given two players with the same batting average, the “smaller” should be the one whose1Data obtained from URL http://www.baseball-reference.com/leaders/batting avg season.shtml.Handout #2—September 8, 2011 3name occurs first in alphabetical order. Given two players with the same batting averageand same name, then they should be ordered by year.I have given you the header file player.hpp in /c/cs427/assignments/ps1 for classPlayer. Other than defining the implementation file player.cpp, you should make almostno changes to any of the other files. In particular, the only file that needs to be changedat all is datapack.hpp, where three small changes are required:1. You will need to change the typedef for BT;2. You will need to include the new header file player.hpp;3. You will need to make the length of the DataPack large enough to accommodate thedata file, which contains 100 records.As part of your testing, you should run your code under valgrind to make sure thatthere are no storage leaks.3 Programming NotesThere are two ways to store a string in C++ . First is as a char array as one does in C .The other is to use the standard string library. To use it, you must put#include <string>at the top of your code. This makes string available as a new type, which you can useto declare variables and parameters. A variable of type string acts just like one


View Full Document

Yale CPSC 427 - Problem Set 1

Download Problem Set 1
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Problem Set 1 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Problem Set 1 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?