



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Assignment; Class: Object-Oriented Programming and Data Structures; Subject: Computer Science; University: Cornell University; Term: Summer 2008;
Typology: Assignments
1 / 7
This page cannot be seen from the preview
Don't miss anything!




The goal of this assignment is to familiarize you with asymptotic complexity (big-O), run-time analysis, basic data structures, and sorting.
Follow the Submission Requirements on the course website. The last section of this assignment summarizes the files that you need to submit on CMS.
As before, solutions will be graded on both correctness and style. For correctness, at the very least your program must compile without errors or warnings. For style, we like concise, clear, easy-to-read code. Use mnemonic variable names. Use proper indentation. Comment where necessary for clarification, but do not overcomment. Be concise; a two-page solution to a problem that can be done in a few lines will lose points, even if correct. The assignment will be graded out of 100 points and is worth 10% of your course grade.
Purpose:Understanding the growth of functions, complexity Points: 25
(a) int sharp(int n){ int r = 0; for(int i=1; i<n; i++) for(int j=i+1; j<=n; j++) for(int k=j+1; k<=n; k++) r++; return r; } (b) int cool(int n){ int r = 0; for(int i=1; i<=n; i++) for(int j=1; j<=i; j++) for(int k=j; k<=i+j; k++) for(int l=1; l<=i+j-k; l++) r++; return r; } (c) int awesome(int n){ if(n<3) return 1;
1/
return 4awesome(n/3)+2n+1; } (d) int sneaky(int n){ if(n<=1) return n; return 5sneaky(n-1)-6sneaky(n-2); }
(a) Give a O(N 2 ) algorithm. (b) Give a O(N log N ) algorithm.
Write your answers in a plain text file called Runtime.txt.
Purpose: To become familiar with the uses of various data structures Points: 20 Answer the following questions. You do not have to write working Java code for this problem. You only have to describe the implementation in words and/or in pseudocode.
Submit your answers in DataStructures.txt.
Purpose: To illustrate some ideas about sorting Points: 20
Your friend Ada Byron is working on a program that will automatically create an index of a given text. She wants the index to list each word in the text in alphabetical order. For each word, she wants the page number(s) that the word appears on to be listed in ascending order. She has written most of the program, but she is having some trouble getting the sorting to work properly. In her implementation, the index is stored as an array of IndexEntry objects. She has defined the IndexEntry class as follows:
amoeboid 63 Algeria 101 anarchism 5 autism 57 algae 68 Algeria 25 anarchism 581 autism 831
Your program should load the file into an array of IndexEntry objects, create an instance of your sorting algorithm, sort by page number, and then sort by term, using one of the sorting routines you have written. Then it should pretty print the sorted index to the screen, like this:
algae 47, 68 Algeria 13, 25, 101 amoeboid 63 anarchism 5, 581 autism 57, 831, 1025
Submit your program in IndexSort.java. You can find some skeleton code and some test input files on CMS. Also provide a plain text file IndexSortNotes.txt that describes which sorting algorithms you chose to implement, and explain why each algorithm is or is not stable.
Purpose: Stacks Points: 35
In this exercise, we will explore an interesting application that lets you implement and appropriately use stacks, as well as reinforce your understanding of recursion and grammars.
As we learnt in class, recursion is a powerful technique for solving problems based on the observation that sub-problems have a structure similar to that of the bigger problem. A recursive function can therefore call itself to solve its sub-problems, and then use these solutions to solve the current problem. Recursion is a concept that manifests itself even outside the realm of CS2110! Snowflakes, clouds, coastlines and plants like ferns exhibit some level of structural self-similarity: when these are viewed at different levels of resolution, you see the same repetitive patterns. Such shapes are examples of fractals. Using our grasp of recursion, we can exploit the self-similarity in these shapes to render realistic images of fractals. For example, in Figure 1 we see that the pattern visible in the big square is scaled down and replicated in four smaller squares within the square, which is further replicated in four squares in those squares and so on. The self-similarity in this image naturally lends itself to a recursive implementation, where in the base case (when the square is too small), we can imagine just drawing a simple square. Such self-similarity is also evident in the bush-like structures we see in Table 1.
In class, a grammar was introduced as a set of recursive rules that help to generate the sentences in a language. Such recursive rules can also be used to model the structure of fractals. For this exercise, we look at a variant of grammars, known as L-systems. These too contain symbols and recursive rules. A sentence is derived by multiple applications of the rules of a grammar, until ultimately all the non-terminals have been replaced by terminals. Likewise, some fractal images can be created by repeatedly applying the rules of an L-system.
Figure 1: A fractal image with tiled self-similarity
Table 1: The bushes in the arboretum, and their corresponding L-systems
δ = 25. 7 ◦^ δ = 20◦^ δ = 22. 5 ◦ F → F[+F]F[-F]F F → F[+F]F[-F][F] F → FF-[-F+F+F]+[+F-F-F]
The difference between grammars and L-systems is the number of rules applied during each step. To produce a sentence using a grammar, one production rule is applied during each derivation step. In other words, one non-terminal symbol is replaced (or rewritten) by applying an appropriate rule (that has the symbol on the left-hand side of the rule). L-systems, on the other hand, apply all possible rules simultaneously during each step. As a result, all non-terminals are replaced using appropriate rewrite rules. To make things more precise, the following L-system was used to synthesize image A in Table 1:
δ = 25. 7 ◦
F → F[+F]F[-F]F
Before you get bewildered by the syntax, note that each of the symbols in this L-system have a simple interpretation as rules for drawing on a canvas. Consider a turtle (or cursor) positioned on a canvas at co-ordinates (x, y), with an orientation α. Note that for these images, the origin is located at the top-left corner of the image: the x-coordinate increases from left to right, and the y-coordinate increases from top to bottom. Let d be a parameter known as the step-size.
you to experiment with the Image API. For example, you could be creative and modify the turtle state such that different branches of the tree are drawn using different shades of colors, or with different line widths. In addition to the code, we require you to submit the three best images you were able to synthesize, one for each of the bush-like structures A, B and C in Table 1 (along with the values of any parameters you used to create those images).
Submit the following files:
If you have finished everything above and are craving some more programming, here are a couple extra credit items you can try your hand at. We will award extra credit based on how well they are done. However, extra credit is always worth less than the main part of the homework. Therefore, your time is best spent finishing the homework well and only working on extra credit afterwards. Make sure you keep a pristine backup copy of the solutions before you start.
Points: 5 An alternative way to solve problem 3 is to sort using a compound key and only sort the data once. In a compound key with two components (for example), entries are compared using the first component and ties are broken by using the second component. In the sorting problem above, the first component would be term comparisons and the second would be page number comparisons. Extend your solution to problem 3 by adding a command line option to enable sorting with a compound key. In particular, you should get the same sort results after sorting the data only once. You will need to modify the main() method; make sure that we can still run the non-extra-credit part of the solution. Start Early and Good Luck! Have Fun!