CSC 153 | Grinnell College | Spring, 2007 |
Computer Science Fundamentals | ||
Quick links: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
Supplemental Problems extend the range of problems considered in the course and help sharpen problem-solving skills. Starred problems may be turned in for extra credit.
Note: Since this course emphasizes functional problem solving with Scheme and object-oriented problem solving with Java, the nature of programs should reflect the language being used. For example, Scheme programs may not use mutation (e.g., set!), except in the context of object-oriented problem solving (e.g., labs 26-31). Scheme programs that use of set!, particular in the context of global variables, will receive 0 points.
Format: In turning in any programs for the course, please follow these directions:
;;; Henry M. Walker ;;; Box: Science Office ;;; Supplemental ProblemAlso, a comment is needed for every procedure, stating in English what that procedure is supposed to do.
For this problem, dates will have three parts: month day year, where the year and day are integers and the month is a symbol (e.g., january, february, etc.). Write the following procedures:
year-end? which returns true if and only if a date ends a year (e.g., December 31, 2004)
month-end? which returns true if and only if a date ends a month (e.g., March 31, 2001 or November 30, 1776)
Notes:
January, March, May, July, August, October, and December have 31 days.
April, June, September, and November have 30 days.
February has 28 days in non-leap years, but 29 days in leap years. A leap year occurs in years when the year is divisible by 4, except that century years are not leap years unless they are divisible by 400. Thus, the years 1999 and 1900 are not leap years, while 1996 and 2000 are leap years.
Examples: These procedures should produce the following results:
(year-end? 'march 31 2004) ===> #f (year-end? 'december 30 2004) ===> #f (year-end? 'december 31 2004) ===> #t (month-end? 'january 8 2004) ===> #f (month-end? 'january 31 2004) ===> #t (month-end? 'february 28 2003) ===> #t (month-end? 'feburary 28 2004) ===> #f (month-end? 'february 28 2000) ===> #fNote: In this and all other supplemental problems, you may define additional helper functions as you wish. From a user's perspective, the required procedures must be available using the prescribed parameters. Beyond that interface, you may handle processing details in whatever way seems convenient.
Given a continuous function y = f(x), it is natural to want to know values of x, called roots, for which f(x) = 0. One approach to this problem is the Bisection Method, discussed in the lab on designing recursive procedures. The Bisection Method works for any continuous function, provided that you can find points a and b, such that f(a)<0 and f(b)>0. However, the Bisection Method can take some time to find good approximations for roots of a continuous function.
This problem discusses a different approach, called Newton's Method. Newton's method is remarkably efficient for typical functions, but it has some limitations:
However, when it works, Newton's method produces very accurate approximates to roots in a very short amount of time.
The following description is a slightly-edited version of what appears in Section 5.1 of Problems for Computer Solutions Using Basic by Henry M. Walker, Winthrop Publishers, 1980.
The following algorithm, which is credited to Newton, can often be used to approximate the roots of a differentiable function efficiently and accurately. In the algorithm we begin by making a guess x_{0} for the root of a function f(x). The algorithm then gives us a "closer" approximation x_{1 to the root. We can then use x1 as our guess, apply the algorithm again, and get a still better guess. This process continues until we are satisfied with our approximation. }
The key, therefore, is to decide how to improve a guess a. Here we observe that the tangent line to a cure is usually a good approximation to the curve "near" the point of tangency. Thus, if the point of tangency is near the root, the tangent line will cross the horizontal coordinate axis near the point where the curve crosses the axis.
We look at the equation of the tangent line to the curve y = f(x) at the point where x=a, The point of tangency is ((a, f(a)) and the slope at this point is f'(a), so by the point-slope form of a line, the equation of the tangent is given by
This tangent crosses the x-axis at a point (b, 0), and since this point is on the tangent, we must have
The geometry of this process is shown in the following diagram.
We can now describe Newton's method more precisely.
First, choose an initial approximation, call it x_{0}, for the root (this may be only a blind guess or it may be based on some knowledge, such as a rough sketch of the graph). Secondly, construct a sequence of numbers:
Because the above formula gives the x-intercept of the tangent to the curve corresponding to x= = a, the above iterative process can be graphically described by the dotted line in the following graph.
The general form of Newton's method can be written:
We expect that for reasonably shaped curves, the x_{n} values approach the actual root as n increases.
Notes:
When do you stop the process? There are various ways of deciding when you are close to the root. None of them are completely satisfactory. The procedure that we will use is to choose some measure on the error err that we are willing to tolerate, for example err = 10^{-6}. We then continue the iteration process until |f(x_{n})| ≤ err
This procedure cannot give acceptable results for every function, because it states that x = 1 is an acceptable approximation to the root x = 0 of the function f(x) = err * x.
Problem: Write a Scheme procedure find-root that takes four parameters, an initial guess, an accuracy err, the function f, and the function's derivative fprime. find-root then should follow Newton's Method to approximate the root of a function f.
Note that find-root may use one or more helper procedures, as long as find-root provides the specified interface.
Write a procedure compute-bill that takes a list of original item costs as input and computes the total charge a customer will pay after discounts. For this exercise, assume that the costs of identical items will be grouped together on the list, and all consecutive equal costs relate to identical items. To illustrate, consider this example:
(compute-bill '(1.25 8.73 5.55 5.55 5.55 12.84 5.55 5.55 20.30 20.30 20.30 20.30))
Here, we assume the first item purchases costs $1.25 and the second costs $8.73. The third, fourth, and fifth items have the same cost, so we assume they are identical items (the cost of these items after multiple-item discount will be 5.55 + 0.9*5.55 + 0.8*5.55). The sixth item costs 12.84. The next two items again cost 5.55, but these must represent a different item than the earlier ones of the same price, since these costs are not contiguous with the previous run of 5.55 prices. Thus, the cost of the seventh and eighth items is 5.55 + 0.9*5.55. The last four items again are considered identical, with costs 20.30 + 0.9*20.30 + 0.8*20.30 * 0.7*20.30.
Notes:
"While some flexibility may be possible in determining a final semester grade, the following percentages approximate the relative weights attached to various activities in this course.
Lab. Write-ups: | 35% | Programs: | 15% | Hour Tests: | 30% | Final Exam: | 20%" |
A recent offering of CSC 105 involved these activities and percentages:
Lab. Write-ups: | 30% | Research Exercises: | 10% | Class Questions: | 10% | Papers: | 50% |
Also, a past offering of CSC 213 involved these activities and percentages:
Labs and Programs: | 60% | Hour Tests: | 20% | Exams: | 30% |
Generalizing, final grade averages typically involve some collection of grade categories and percentages for those categories. In computing a student's final score, student scores in each category are multiplied by the prescribed weight. Of course, the student scores vary but the weights are the same for the entire course. This problem asks you to write a higher-order procedure that will compute averages for students in a course:
Write a higher-order procedure grade-template that takes a sequence of category percentages for a course as parameters and returns a procedure. This returned procedure similarly takes a sequence of student grades as parameter, and returns the student's final semester score.
For example, suppose a student in this course had a Lab Write-up average of 80%, a program average of 90%, an hour test average of 95%, and a final exam average of 85%. Then procedure grade-template could be used to compute the student's semester average as follows:
(define course-evaluator (grade-template 0.35 0.15 0.30 0.20)) (course-evaluator 80 90 95 85)
As a result, the computer should return the weighted sum:
Notes:
File test.data in directory ~walker/151s/labs contains information on test results for a certain class. Each line of the file contains a students first and last name and the results of three hour tests. Write a program that computes the average test score for each student, the maximum and the minimum scores for each test, and prints the results in a nicely formatted table. For example, the table might have the following form:
Name Test First Last 1 2 3 Average Egbert Bacon 88 85 92 88.33 . . . Maximum −− −− −− Minimum −− −− −−
Computer monitors divide an image into a grip of pixels. Each pixel appears as a colored dot. Pixels are identified by coordinate system, with horizontal and vertical (x and y) coordinates. For example, on a Linux system, the pixel at the upper left of the monitor is given the coordinates (0, 0), and pixel part-way down the screen on the left might have coordinates (0, 300).
When displaying rectangles (e.g., windows) on a monitor, at least two approaches may be used:
In this problem, you are to use the second approach. Often, these coordinates are grouped as the upper-left coordinates and lower-right coordinates, but for this introductory problem you should store the four values as separate integer fields.
Using Java, write a simple Rectangle class with the following properties:
This problem develops an efficient way to find the kth-smallest element in an array.
Partition: Write a method
int partition (int a[], int left, int right)which rearranges those elements within the a array with indices between left and right, so that
Also, the method returns the final value of middle.
Thus, the array segment a[left] ... a[right] is rearranged as required, to give a new arrangement of values a[left] ... a[(middle)] ... a[right], where all array elements before position middle have values <= a[(middle)] and where a[(middle)] is less than or equal to all array elements after position middle.
Include your partition method in an appropriate class and testing suite , so that you can adequately check the correctness of your code.
Discussion: Work within partition should be done with a single pass through the elements a[left] ... a[right], as follows.
move upward in the array through a[right], a[right-1], ... until finding an element a[r_spot] where a[r_spot] < a[left], if such an element exists.
move downward in the array through a[left], a[left+1], ... until finding an element a[l_spot] where a[l_spot] > a[left], if such an element exists.
swap a[l_spot] and a[r_spot].
continue steps 1, 2, and 3 until searching all elements in the array segment. (At this point, "small" values will have been moved early within the array segment, while "large" values will have been moved late.)
place a[left] in its appropriate position a[middle], where middle is the index where l_spot and r_spot have come together.
Finding Median Values: The partition method of part A may be used to find the kth smallest element in an array segment a[left], ..., a[right], as follows:
If there is only one element in a[left], ..., a[right], then one expects k = 1, and one can return that element.
Use the partition procedure to rearrange a[left], ..., a[right] into a[left] ... a[(middle)] ... a[right] as described in problem 1.
If k = ((middle)-left+1), then a[middle] is the desired element, and stop.
If k <= ((middle)-left), then apply this algorithm recursively to find the kth smallest element in an array segment a[left], ..., a[(middle)-1].
If k >= ((middle)-left), then apply this algorithm recursively to find the k-((middle)-left)-1th smallest element in an array segment a[(middle)+1], ...,a[right].
Problems for part B:
Write a procedure
int select (int a[], int n, int k)
that uses the above algorithm and procedure partition to find the kth smallest element in an array a, where a has n elements.
Write a procedure
int median (int a[], int n)
that uses the above algorithm and procedure partition to find the median element in an array a, where n is the size of the array.
Write methods deleteDuplicates and deleteDuplicatesOrdered for a singly-linked-list class. For both methods, when duplicate names appear, the first entry on the list should be retained, and all subsequent duplicate entries should be deleted.
Any of the following problems may be done for extra credit. As noted in the course syllabus, however, a student's overall problems' average may not exceed 120%.
Of course, some fractions trivially have this property. For example, when numerator and denominator are multiples of 10, such as 20/30, one can always "cancel" the zeroes. Similarly, cancellation is always possible when the numerator and denominator are equal, as in 22/22. Your program should omit these obvious cases.
Albuquerque Bernalillo New Mexico 1891 331767 247 323935 14 5A blank line follows each entry, including the last.
Write a procedure which has a filename as parameter and which answers the following questions about the cities represented in the data files.
This program counts words and sentences in file "comp.text ". Sentence: 1 Words: 29 Sentence: 2 Words: 41 Sentence: 3 Words: 16 Sentence: 4 Words: 22 Sentence: 5 Words: 44 Sentence: 6 Words: 14 Sentence: 7 Words: 32 File "comp.text" contains 198 words words in 7 sentences for an average of 28.3 words per sentence.In this program, you should count a word as any contiguous sequence of letters, and apostrophes should be ignored. Thus, "word", "sentence", "O'Henry", "government's", and "friends'" should each be considered as one word.
Also in the program, you should think of a sentence as any sequence of
words that ends with a period, exclamation point, or question mark.
Exception: A period after a single capital letter (e.g., an initial)
or embedded within digits (e.g., a real number) should not be counted as
being the end of a sentence.
White space, digits, and other punctuation should be ignored.
Information on the 1997-1998 Iowa Senate
File /home/walker/151s/labs/ia-senate contains information about the members of the 1997-1998 Iowa Senate. After a title line and a blank line, a typical line has the following form:
Angelo Jeff 44 Creston IA 50801 Kramer Mary 37 West Des Moines IA 50265 Lundby Mary 26 Marion IA 52302-0563
Thus, a typical line gives the last name, the first name, the district number, the town of residence, the state (always IA), and the town's zip code. The information in these lines is arranged in columns.
Design and write a Scheme program that reads in data from this file and creates two output files, senators-by-city and senators-by-zip-code, in the current working directory. The senators-by-city file should contain the same data as the source file, in the same format (including capitalization), but with the lines arranged alphabetically by city (column 4). The other file, senators-by-zip-code, should contain a list of all senators in the following format
Jeff Angelo Creston, IA 50801
A blank line should appear after each senator and city address. In this format, the name appears on a first line (first name, then last), and the city, a comma, the state, and zip code is on the next line — separated by single spaces in the format shown. Note that a variation of this format (with a street address, if available) might be used for a mailing label.
In private games, different types of gamblers set different personal limits according to the following table:
Gambler Category | Amount at Start of Evening | Amount Bet
per Game | Game Payoff | Probability of Winning Game | Total to Stop |
---|---|---|---|---|---|
Average Gambler | $25 | $2 | $4 | 0.3 | $50 |
Low-risk Gambler | $10 | $1 | $1 | 0.5 | $18 |
High-risk Gambler | $50 | $5 | $15 | 0.2 | $150 |
Interpreting this table, for an average gambler, the gambler starts the evening with $25; he bets $2 on each game and stops when he either runs out of money or has a total of $50. To be more specific, for a specific game, the gambler bets $2. If the gambler loses the bet, then $2 is deducted from his account. If the gambler wins the bet, then the gambler wins a payoff amount, and the gambler's new balance is increased by that payoff — the $2 is not deducted. For example, if the payoff is $5 and if the gambler starts the game with $20, then the gambler's new balance would be $18 if the gambler loses a bet and $25 if the gambler wins the bet.
The following problems will allow you to investigate the likelihood of winning by types of gamblers, by simulating games each evening over a period of 1000 nights of gambling. To accomplish this simulation, you are to proceed in three steps:
Write a PlayGame class that has one method:
Write a Gambler class with these characteristics:
double betSize; /* size of each bet */ double payoff; /* amount earned (in addition to bet) if bet won */ double prob; /* probability of winning a bet */ double start; /* amount in gambler's purse at start of evening */ double purse; /* current amount gambler holds */ double quitAmount /* amount gambler must earn before quitting for evening */
Write 3 subclasses, AverageGambler, LowRiskGambler, and HighRiskGambler, that extend class Gambler by including a constructor with no parameters that set the fields according to the above table.
Write a SimulateGambling class that creates 1000 of each type of Gambler, records how many Gamblers of each type win over a full evening, and prints the results in a table (filling in the question marks below):
Gambler Category Evenings Won Evenings Lost Average Gambler ??? ??? Low-risk Gambler ??? ??? High-risk Gambler ??? ???
This document is available on the World Wide Web as
http://www.walker.cs.grinnell.edu/courses/153.sp07/suppl-prob.shtml
created: 6 February 1997 last revised: 16 April 2007 |