DOC PREVIEW
Stanford CS 106B - Assignment 1—Simple C++

This preview shows page 1-2 out of 5 pages.

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

Unformatted text preview:

Eric Roberts Handout #7CS106B April 3, 2009Assignment #1—Simple C++Parts of this handout were written by Julie ZelenskiDue: Friday, April 10Part 1. Get your C++ compiler workingYour first task is to set up your C++ compiler. If you’re using the machines in Stanford’spublic clusters, you don’t need to do anything special to install the software. If you’reusing you own machine, you should consult one of the following handouts, depending onthe type of machine you have:• Handout #7M. Downloading XCode on the Macintosh• Handout #7P. Downloading Visual Studio for WindowsOnce you have the compiler ready, go to the assignments section of the web site anddownload the starter file for the type of machine you are using. For either platform, theAssignment1 folder contains six separate project folders: one for this warmup problemand one for each of the five problems in Part 2 of the assignment. Open the project file inthe folder named 0-Warmup. Your mission in Part 1 of the assignment is simply to getthis program running. The source file we give you is a complete C++ program—socomplete, in fact, that it comes complete with two bugs. The errors are not difficult totrack down (in fact, we’ll tell you that one is incorrect arguments to a function call andthe other is a needed #include statement is missing). This task is designed to give you alittle experience with the way errors are reported by the compiler and what it takes to fixthem.Once you fix the errors, compile and run the program. When the program executes, itwill ask for your name. Enter your name and it will print out a “hash code” (a number)generated for that name. We’ll talk later in the class about hash codes and what they areused for, but for now just run the program, enter your name, and record the hash code.You’ll email us this number. A sample run of the program is shown below: Please enter your name: Eric The hash code for your name is 339.Once you’ve got your hash code, we want you to e-mail it to your section leader andintroduce yourself. You don’t yet know your section assignment, but will receive it viaemail after signups close, so hold on to your e-mail until then. I’d also appreciate if youwould cc me on the e-mail ([email protected]) so I can meet you as well.Here’s the information to include in your e-mail:1. Your name and the hash code that was generated for it by the program2. Your year and major3. When you took 106A (or equivalent course) and how you feel it went for you4. What you are most looking forward to about 106B5. What you are least looking forward to about 106B6. Any information that might be helpful to us about how to best help you learn andmaster the course material– 2 –Part 2. Simple C++ problemsMost of the assignments in this course are single programs of a substantial size. To getyou started, however, the first assignment is a series of five short problems that aredesigned to get you used to using C++ and to introduce the idea of functional recursion.None of these problems will require more than a page of code to complete; most can besolved in just a few lines.Problem 1 (Chapter 1, exercise 7, page 41)Greek mathematicians took a special interest in numbers that are equal to the sum of theirproper divisors (a proper divisor of N is any divisor less than N itself). They called suchnumbers perfect numbers. For example, 6 is a perfect number because it is the sum of1, 2, and 3, which are the integers less than 6 that divide evenly into 6. Similarly, 28 is aperfect number because it is the sum of 1, 2, 4, 7, and 14.Write a predicate function IsPerfect that takes an integer n and returns true if n isperfect, and false otherwise. Test your implementation by writing a main program thatuses the IsPerfect function to check for perfect numbers in the range 1 to 9999 bytesting each number in turn. When a perfect number is found, your program shoulddisplay it on the screen. The first two lines of output should be 6 and 28. Your programshould find two other perfect numbers in the range as well.Problem 2 (Chapter 2, exercise 6, page 79)A histogram is a graphical way of displaying data by dividing the data into separateranges and then indicating how many data values fall into each range. For example,given the set of exam scores100, 95, 47, 88, 86, 92, 75, 89, 81, 70, 55, 80a traditional histogram would have the following form:******** ****0–9 10–19 20–29 30–39 40–49 50–59 60–69 70–79 80–89 90–99 100The asterisks in the histogram indicate one score in the 40s, one score in the 50s, fivescores in the 80s, and so forth.When you generate histograms using a computer, however, it is usually much easier todisplay them sideways on the page, as in this sample run: 0: 10: 20: 30: 40: * 50: * 60: 70: ** 80: ***** 90: ** 100: *– 3 –Write a program that reads in an array of integers—ideally from a text file as described inChapter 3—and then displays a histogram of those numbers, divided into the ranges 0–9,10–19, 20–29, and so forth, up to the range containing only the value 100. Your programshould generate output that looks as much like the sample run as possible.Problem 3 (Chapter 3, exercise 2, page 116)Heads. . . .Heads. . . .Heads. . . .A weaker man might be moved to re-examine his faith, if in nothingelse at least in the law of probability.—Tom Stoppard, Rosencrantz and Guildenstern Are Dead, 1967Write a program that simulates flipping a coin repeatedly and continues until threeconsecutive heads are tossed. At that point, your program should display the totalnumber of coin flips that were made. The following is one possible sample run of theprogram: tails heads heads tails tails heads tails heads heads heads It took 10 flips to get 3 consecutive heads.Problem 4 (Chapter 5, exercise 8, page 201)The mathematical combinations function C(n, k) is usually defined in terms of factorials,as follows:C(n, k) =n!k! x (n–k)!The values of C(n, k) can also be arranged geometrically to form a triangle in which nincreases as you move down the triangle and k increases as you move from left to right.The resulting structure, which is called Pascal’s Triangle after the French mathematicianBlaise Pascal, is arranged like this:C(0, 0)C(1, 0) C(1, 1)C(2, 0) C(2, 1) C(2, 2)C(3, 0) C(3, 1) C(3, 2) C(3, 3)C(4, 0) C(4, 1) C(4, 2) C(4, 3) C(4, 4)Pascal’s Triangle has the interesting property that every entry is the


View Full Document

Stanford CS 106B - Assignment 1—Simple C++

Download Assignment 1—Simple C++
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 Assignment 1—Simple C++ 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 Assignment 1—Simple C++ 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?