HW 3 - Cornell Computer Science

CS 1109 Summer 2011
Homework 3
Due: July 18, 11:59PM
Instructions
The purpose of this assignment is to help you gain experience in writing
scripts that make use of vectors and strings. Remember: You are allowed to
discuss the homework with other people in the class, but the work you turn
in must be your own. At no point should you be in possession of someone
else’s code. There are two deliverables for this homework — a script named
tomorrow.m and another named pigLatin.m.
Code Standards
As before, please include the following header at the top of all your submission
files, with the various fields filled in.
%
%
%
%
Name: <Your name goes here>
NetID: <Your NetID goes here>
<Names of people you discussed this assignment with>
<Total time spent on problem>
Your program submissions will be required to adhere to the code standards as
described in class. A PDF version of the standards is available on the course
website. Violations of the code standards will result in style penalties, so be
sure to read the requirements carefully! In the case of egregious violations,
you will be asked to fix your code and resubmit a new version altogether,
with a 25% penalty.
Tips
Here are some tips for successfully completing this assignment:
1. Start early! Do not wait until the last minute. Budget sufficient time
for fixing errors. Seek out help from the course staff if you are having
difficulty with any of the course material.
2. Read this document carefully! People have been losing points due to
careless mistakes and/or a failure to follow directions. This is completely avoidable.
3. Review the material that was covered in Lab 5, as well as the lectures/labs on loops and if-statements. This will be useful preparation
for this homework assignment.
1
CS 1109 Summer 2011
Homework 3
Due: July 18, 11:59PM
4. Use the top-down approach as discussed in class when building your
programs. Begin with a logical decomposition of the overall task in
pseudo-code; successively refine this to arrive at your final program.
5. Test, test, test! Once you have a working program, test it to make sure
it obeys its specifications. Try a variety of inputs and verify that your
program produces the correct outputs.
What is Tomorrow’s Date?
In any application that is reliant on a clock, such as calendar software or
email clients, it is necessary to perform date computations. In this problem,
you will write a script named tomorrow.m that prompts the user for today’s
date in MM/DD format and outputs tomorrow’s date in the same format.
You may assume that the user always enters a legal date. You can also assume that the input will always use two digits to represent the month and
the day, i.e., the user will enter 01/04 rather than 1/4 to represent January
4th . Below are some examples of how your script should behave. Note that
the text in red represents user supplied input.
>> tomorrow
Enter a date: 07/11
Tomorrow is 07/12
>> tomorrow
Enter a date: 05/31
Tomorrow is 06/01
>> tomorrow
Enter a date: 12/31
Tomorrow is 01/01
As always, devise a solution “roadmap” using English / pseudocode on paper
first, before writing any code. Here are some questions to think about as you
plan out your solution:
• How are you going to store the user input? As a number? A string?
• You need a way to look up the number of days in a month. What is
an efficient way to represent this information in your script?
2
CS 1109 Summer 2011
Homework 3
Due: July 18, 11:59PM
• You may find the Matlab functions str2num and num2str useful. Read
the documentation for these functions and play around with them to
understand how they work.
• Your output needs to be in the format MM/DD as well. So if tomorrow’s date is 1/7, your program needs to print 01/07. How can you
force a numeric field to be 2 characters-wide when you print it to the
screen? How can you “pad” the empty spaces with leading zeros?
Igpay Atinlay Anslatortray
Pig Latin is a language game that is popular in much of the English-speaking
world. It works by taking English words and applying transformations to
them that render them incomprehensible to the untrained ear. You can read
more about Pig Latin on Wikipedia1 . In this problem, you will write a script
that translates a user-input phrase from English into Pig Latin.
The rules for changing a single word from English to Pig Latin are as follows:
• If the word begins with a consonant, then we move the first “consonant
cluster” i.e., all the consonants up to (but not including) the first vowel,
to the end of the word. The letters ‘ay’ are then appended to the end
of the word. For example:
– duck → uckday
– school → oolschay
• If the word begins with a vowel, then we simply append the letters
‘way’ to the end of the word. For example:
– aardvark → aardvarkway
– each → eachway
Write a script pigLatin.m that prompts the user to enter a phrase, translates
each word into Pig Latin and prints out the results to the screen. You may
assume that the user input will consist solely of lower-case letters from the
English alphabet, i.e., there will be no upper-case letters, numbers, symbols
or punctuation marks in the input. You can also assume that the input will
1
http://en.wikipedia.org/wiki/Pig Latin
3
CS 1109 Summer 2011
Homework 3
Due: July 18, 11:59PM
not contain words that have no vowels such as ‘rhythm’ or ‘gypsy’. Below
are some examples of how your script should behave. The text highlighted
in red represents user-supplied input.
>> pigLatin
Enter phrase: hello world
ellohay orldway
>> pigLatin
Enter phrase: black and white are all i see
ackblay andway itewhay areway allway iway eesay
Here are some tips for completing this problem, as well as questions to think
about, while designing your solution:
• How do you extract individual words from a given string? Make use of
the strtok function. In particular, suppose we are given a string str
composed of multiple words. Then, we can use the following statement:
[firstWord rest] = strtok(str);
This stores the first word from str in firstWord and everything but
the first word from str in rest. Note that firstWord and rest are
just variable names — you can call them anything you wish. Having
extracted the first word from str, how can you extract the second
word? The third word? How can you tell when there are no more
words left?
• A file named findFirstVowel.m is available on the course website to help
you complete this problem. Download this to your working directory.
Type help findFirstVowel to understand what it does and how to
use it.
Submission
Please submit your files tomorrow.m and pigLatin.m via CMS. If you wish to
resubmit a file, simply upload a new version — this will replace any previous
submissions.
4
CS 1109 Summer 2011
Homework 3
Due: July 18, 11:59PM
Checklist
• Correctness
Scripts have been tested and found to obey specifications
• Documentation
Each script contains a header with your name, NetID, names of collaborators,
and time spent on the problem
Each script contains a header detailing its purpose
The submissions contain comments documenting the various sub-tasks that
are solved by the programs
• Presentation
Output from intermediate computations have been suppressed where necessary
Scripts have been prefaced with clc and clear commands
• Readability
Variable names are meaningful and follow a consistent naming convention
The code is neatly indented (Ctrl-A, Ctrl-I)
Long lines (more than 80 columns) are wrapped around to the next line
Unnecessary code fragments have been eliminated
5