University of Toronto
CSC104s, Spring 1997
Assignment 2 (10%)

Turing Programming

Due: Week of Mar. 17th, start of tutorial

This assignment introduces you to computer programming using the Turing programming language. In programming the problems described below, you will learn something about data structures, algorithm design, and programming methodology.

The assignment is divided into six parts, each part building on the part before it, and getting progressively more difficult. You will receive credit for each part you complete.

Part 1

In your first program, you will start with two lines of code. The program will initially contain a bug. When you fix the bug, the program, not counting lines of documentation, will have three lines of code.

In the first line declare a string variable (type string). The identifier for the variable (i.e., the variable name) is your own first name, all in lower case letters. Dashes and spaces are not allowed in identifier names: if you have a dash or space in your first name, change it to an underscore character (i.e., Mary-Lou would become mary_lou).

In the second line of code, display the value of the variable, using the put instruction.

Now run your program. An error message pops up in the OOT error viewer. Fix the bug by adding a line between the two you already have. The line should assign to the variable the value "5 February 1997". Run this changed program, debugging if necessary. When the program runs correctly, the Winoot run window should show the assigned value (5 February 1997).

Finally, add comment lines at the top with your full name and student number, course number (CS104), instructor name, TA name, and assignment part (Part 1). Save the program using the name "a2part1.t" (The Save As command is in the File menu). Get a printout to hand in.

Part 2

This program starts out with, then changes, the first program. Before you begin this second program, load the first program into Winoot, if it is not already in the editor. Change the comment line that tells what assignment part this is to "Part 2", and save the program with the new name "a2part2.t".

The value assigned to the variable in part 1 should now be received from the keyboard. Change the program so that, instead of assigning the value to the string variable directly, it prompts the user to enter a date in international format (i.e., day month year, with no punctuation) with quotation marks around it. Use the get command to input the date. Add an output phase before the put statement at the end that displays on the screen "You typed" in front of the value the user typed in. Test to see if it works so far. Here is what the output screen might look like:

     Enter a date in international format 
        with quotation marks around it: "5 February 1997"
     You typed 5 February 1997

Now put a loop around the input and output so that the program continues to ask for a date and to show what was typed, until the user enters the word "quit". You should also add a put statement before the loop telling the user how to get out of the loop, and a line at the end thanking the user for running the program. The program should not echo the word "quit" on the screen the way it echos the date typed in. Here is a sample run:

     To stop the program, type the word quit

     Enter a date in international format 
        with quotation marks around it: "5 February 1997"
     You typed 5 February 1997
     Enter a date in international format 
        with quotation marks around it: quit
     
     Thank you for running the program

Save the program and get a printout to hand in.

Part 3

This program starts out with the second program. Before you begin, load the second part, if it is not already in the editor, change the comment line that tells what assignment part this is to "Part 3", and save the program with the name "a2part3.t".

Add a menu to the program. The menu should be printed each time the program goes through its loop. Clear the output screen before the menu is printed, using the setscreen ("text") command. The menu should give the person the choice of date conversions. The conversions will be the formats represented by "February 5, 1997," "2/5/1997," and "19970205." The user should be prompted for the choice before the date is entered, using an integer variable.

The menu would be the logical place to include the stopping logic. Remove the old stopping code from the program and add a menu choice (choice zero) that causes the program stop running.

Here is what the output should look like. At this point no date conversions happen.

               Menu 

     Convert the date to:

        (1)  Month Day, Year (February 5, 1997)

        (2)  Month/Day/Year (2/5/1997)

        (3)  Eight-digit zero-padded YearMonthDay (19970205)

        (0)  Quit program

     Enter choice (0 to 3):  3

     Enter a date in international format 
        with quotation marks around it: "5 February 1997"

     You typed 5 February 1997

               Menu 

     Convert the date to:

        (1)  Month Day, Year (February 5, 1997)

        (2)  Month/Day/Year (2/5/1997)

        (3)  Eight-digit zero-padded YearMonthDay (19970205)

        (0)  Quit program

     Enter choice (0 to 3):  0

     Thank you for running the program.

If the user does not make a valid choice (not 0, 1, 2 or 3) the code to be added in the next part will not know what to do. So add a loop at the point where the user enters the choice that will continue to prompt the user until he/she provides a valid answer. Here is what the screen will look like if the user inputs an invalid number:

     Enter choice (0 to 3):  4

     Enter choice (0 to 3):  -3

     Enter choice (0 to 3):  3

     Enter a date in international format: 
        with quotation marks around it: "5 February 1997"

     etc.

Finally, add a delay statement that holds the output (the "You typed..." line) on the screen long enough for the user to view the converted date (the actual conversion is in the next part). The statement "delay (4000)" will hold the output on the screen for 4 seconds before clearing the screen and displaying the menu.

Save the program and make a printout to hand in.

Part 4

This program starts out with the third program. Before you begin, load the third part, if it is not already in the editor, change the comment line that tells what assignment part this is to "Part 4", and save the program with the name "a2part4.t".

Add to the program the logic that checks which conversion the user has chosen, and does the correct conversion.

The easiest way to do the conversion is to parse the input string into day, month and year, then use the parsed parts to build up the desired date format. The month also has to be turned from a string into a number for conversions 2 and 3. More code is needed to handle the problem of the one- and two-digit days and months, so that extra spaces are not inserted (So the output is not "1997 210", for example, instead of "19970210" and not "02/01/1997" when it should be "2/1/1997").

You should handle month in the long format ("February") and the short format ("Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"). If the user types the short format, keep it ("5 Feb 1997" becomes "Feb 5, 1997"). Also handle the case where the user does not capitalize the long or short format ("5 february 1997" becomes "february 5, 1997" and "10 mar 1992" becomes "mar 10, 1992"). You do not need to take into account abbreviations with periods (e.g. "Mar." for "Mar").

Add comments to your code to indicate the various logical parts. Save the program and get a printout to hand in.

Part 5

In this part of the assignment we will not significantly change the program code. We will make only a few necessary changes to the program so that we can run it on a test file.

In the previous steps, you tested your programs using input typed directly from the keyboard. Using the Winoot editor create a new file which contains all the input for your program. Your file might look as follows:

	3
	"5 February 1995"
	2
	"10 March 1880"
	3
	"31 July 1954"
	4
	-3
	3
	"2 June 1901"
	0

Save your file as "a1test1.in".

Load your program into Winoot and select from the Run menu the entry Run with Args. In the box labelled input redirection type the name of your test file ("a1test1.in") and then click on Run. This time when the program runs it will not stop to wait for keyboard input but will instead use the input data file you specified.

Notice now that when you run the program, each input value, "5 February 1995" for example, no longer appears on the output screen. To correct this problem edit your program adding a put statement after every get statement. For example, if you had the statement get choice then immediately following it, add put choice into your code. Now your output from Run with Args should look like it did in step 4 except that you won't see the quotation marks around your input even though you still included them in the file. If you go back to running your program with input from the keyboard, the output will look a little odd having two copies of each input value; You can just ignore this.

Go back and edit your test file, or create a new one, so that your data thoroughly tests the different capabilities of your program. Be sure to test the user errors that your program is supposed to handle and the special cases.

When you are happy with your testing, you must make two more changes to your program before creating the printout. You need to comment out any lines containing delay statements and the line that clears the screen. Save your program calling it "a2part5.t" and exit Winoot. From windows pick the icon labelled Print Turing Assignment. You will be required to specify a program name; Yours is "a2part5.t". You will also be able to specify a number of input data file names. You should give the name of the test file, probably "a1test1.in", or multiple test files that you created. Print Turing Assignment will run your program on the input files and produce a complete handout which you will submit to your TA for marking.

Part 6

This is the part where you can express yourself. Add to your program two features. The first feature is an extra choice on the menu that promises to give the "Chinese Year" conversion. This option will strip the month out of the input date and tell the user which animal year it is. Here are the correspondences:

1912 Rat
1913 Ox
1914 Tiger
1915 Hare
1916 Dragon
1917 Serpent
1918 Horse
1919 Ram
1920 Monkey
1921 Rooster
1922 Dog
1923 Boar

Dates not listed here repeat with a twelve year cycle. This is the year of the ox, because 1913 + 12*7 = 1997.

In addition to telling the user which year it is, the program should also perform some graphical activity corresponding to the year. The more interesting (and animated) the graphics, the better the score.

You can still receive a B or even an A on the assignment without completing part 6 so if you have finished part 5 you might choose to do some, all or none of this part depending on your situation.

If you chose to calculate the Chinese year but not do any graphics, you can make your changes to "a2part5.t" and to your corresponding test file. Call the new program "a2part6.t" and name the test file appropriately. Use Print Turing Assignment to create a printout.

If you chose to include graphics in your assignment, then your TA will need to mark your assignment on-line. This means that you should start with your program from Part 4 ("a2part4.t") so that the delays and screen clearing statements and not removed. You should submit the program electronically on the CDF machines so that the grader can view the program. Remember, though, not to use Print Turing Assignment. Also submit a paper copy of the program listing, as you did for steps 1 to 4. Add a comment at the top of the program printout that says something like "The source code for this program has been submitted to account s104----".

What to Hand In

Begin your submission with a header page which includes your name, student number, TA and instructor. Hand in the printouts of your programs written for parts 1 through 4. For part 5, hand in the output from Print Turing Assignment. On the output produced by Print Turing Assignment for part 5, add handwritten comments to indicate the purpose of each test case. For example you might say something like "to test option 3 with an abbreviated month name." Do not type these comments into the test file; Just write them on in pen after the printout is produced.

Hand in the appropriate pieces from the sections of part 6 which you were able to complete. Remember if you do any graphics for part 6 to electronically submit your program and indicate in a comment on our paper submission where the source code was submitted.