# ITEC 320: Programming Assignment 3

#### Submit Command:  submit itec320-01 dotrees.adb

1. No updates at this time.

You can see an example of this program at assignment checker (which uses gnoga to integrate the GUI, web server, and problem solution). On-campus or VPN session required.

Write a program that helps a fruit breeder analyze a set of data describing fruit trees and sample pieces of fruit found on those trees. Use records, enumerated types, and other language features in solving the problem.

Input:

• There will be two input streams to the program: a file specified in the command line and standard input. (See Ada.Command_Line)
1. The file specified in the command line will contain information on trees and samples. Read this file until end of file.
• The input file will contain data on between one and one thousand trees, inclusive, and on between 0 and 20 samples per tree, also inclusive.
• Information for a tree will be given in a sequence of items as follows:
1. The keyword "TREE".
2. A tree identification number that is a positive integer of 7 or fewer digits (with no sign).
3. After a tree identification string comes a possibly empty sequence of data on fruit samples. Each piece of fruit will be introduced by the keyword "FRUIT", followed by three words to describe the following characteristics, in this order:
1. Size: small, midsize, large
2. Firmness: soft, firm, hard
3. Taste: bland, sweet, sour
• Use values of 1, 2, and 3, respectively, for the values of each characteristic.
• A tree with a given ID number can appear more than once in the data.

2. Standard input will contain a sequence of commands that will direct the program's output. These commands determine program output and operation, as follows:
• "Trees": Output only Tree id number and number of fruit samples
• "Averages": Output as in Trees, plus the average and standard deviation of each category for each tree.
• "Fruits": Output as in Trees, followed by each fruit and then followed by average and standard deviation, as in Averages
• "Quit": Halt the program

• Other points on input:
• You can assume that the input is correct (but see the Extra Credit below).
• Your program should have no prompts.
• Free format input: there can be any amount of white space before and after any input item.
• Case sensitivity: Keywords and commands are NOT case sensitive. So, for example, tree, Tree, and TREE are all valid keywords and the commands quit and Quit have the same effect.
Output:
1. Output should look like the sample run below, lined up in columns, with one decimal place.
2. Output 0.0 for averages and standard deviation for trees with no fruit samples.
3. Output commands, data, and ID numbers in upper case.
Sample run:

Assume that the file  test1.dat is as follows:


tree 1234567
fruit small hard sour
fruit midsize firm bland
fruit large hard sour
tree

2234567
tree 3234567
tree 4234567
fruit
small
soft
sour
tree 1234567
fruit midsize hard bland
tree 1234567
fruit large firm sweet
tree 3333333
fruit midsize hard bland
tree 4444
fruit large firm sweet

and that the following is entered via standard input (ie either keyboard or via redirection):
trees
fruits
averages
quit

A sample run of your program would look [approximately] like this:

>dotrees  test1.dat
trees
TREE: 1234567   5
TREE: 2234567   0
TREE: 3234567   0
TREE: 4234567   1
TREE: 3333333   1
TREE:    4444   1
fruits
TREE: 1234567   5:
HARD      SOUR      SMALL
FIRM      BLAND     MIDSIZE
HARD      SOUR      LARGE
HARD      BLAND     MIDSIZE
FIRM      SWEET     LARGE
2.6       2.0       2.2
0.5       0.9       0.7
TREE: 2234567   0:
0.0       0.0       0.0
0.0       0.0       0.0
TREE: 3234567   0:
0.0       0.0       0.0
0.0       0.0       0.0
TREE: 4234567   1:
SOFT      SOUR      SMALL
1.0       3.0       1.0
0.0       0.0       0.0
TREE: 3333333   1:
HARD      BLAND     MIDSIZE
3.0       1.0       2.0
0.0       0.0       0.0
TREE:    4444   1:
FIRM      SWEET     LARGE
2.0       2.0       3.0
0.0       0.0       0.0
averages
TREE: 1234567   5:
2.6       2.0       2.2
0.5       0.9       0.7
TREE: 2234567   0:
0.0       0.0       0.0
0.0       0.0       0.0
TREE: 3234567   0:
0.0       0.0       0.0
0.0       0.0       0.0
TREE: 4234567   1:
1.0       3.0       1.0
0.0       0.0       0.0
TREE: 3333333   1:
3.0       1.0       2.0
0.0       0.0       0.0
TREE:    4444   1:
2.0       2.0       3.0
0.0       0.0       0.0
quit


Other points:
• Your main program should consist of only a few statements (ie most of the work should be done in procedures).
• Do not use any global variables!
• Efficiency: your find routine can be inefficient (ie linear search), but you responses to commands are to be efficient. In effect this means that you should not simply store a list of fruits, which is an inefficient way to solve the problem. You do not need to be concerned about space efficiency.

• Average: $$\overline{x} = \frac{1}{N}{\sum_{i=1}^{N}x_i}$$
• Standard deviation: $$\sqrt{\frac{1}{N}\sum_{i=1}^{N}(x_i-\overline{x})^2}$$

Suggestion: Here's a suggested sequence for development:

1. Define some enumeration types, and use enumeration_io to read values of those types from standard input. Also print the values.
2. Open a file whose name is specified on the command line, read enumeration values from that file, and then close it.
3. Read and print a data file that is in the correct format.

4. Design a data structure that holds a sequence of trees and their associated information, and design an algorithm to read and store a sequence of trees and associated data. (Algorithm + Data Structure = Program)
5. Read trees from the data file and store them in your data structure. Don't try yet to check if a tree's ID has already been read; instead, just read and store each tree and it's data. Then print the array.
6. Convert the characteristics (eg sweet or sour) to the correct numbers.
7. Calculate and print mean and standard deviation for each tree and store these values in your array.

8. Add enumeration types for commands, read values from standard input, and process those commands
9. Correctly handle trees that are repeated in the tree data file.

Extra Credit 1 - Efficient Searching: For 5 points extra credit, implement an efficient search algorithm (eg O(1) or O(lg n). If you want credit for doing this, please put a very noticeable comment in your header comment!

Extra Credit 2 - Robustness: For 5 points extra credit, make your program robust by doing the error checking described below. If you want credit for doing this, please put a very noticeable comment in your header comment!

• If there are no command line arguments, output an error message to that effect and then halt.
• If the input file does not exist, output an error message that contains the name of the file and then halt.
• If the data in the input file is not correct, output an error message that states the problem and then halt. Catch the following errors in the input file:
• The first keyword is "fruit".
• Invalid keyword.
• Invalid id (non-integer or out of range).
• Premature end of of the input file
• If more than one error occurs (eg eof and invalid id) then the message can mention either error.
• If an invalid command is entered via standard input, do two things:
• Skip the remainder of the input line
• Print an error message and continue execution
• If end of file is reached in the commands, then a message to that effect should printed and the program should halt.
• Your messages should briefly describe the error; not simply be the default message from a built-in exception.

Style: In coming up with your Ada solution to the following problem, please follow my style guide. In particular, please note the use of consistent indentation, named constants, and descriptive constant and variable names. Please remember that the first thing in any program file should be a comment that gives a brief overview of what the file contains (and should do). Also remember to keep your lines less than 80 characters long. Not only does this mean that printouts won't run off the side of the page, but it also makes your programs look neater on an 80 column wide xterm window (a popular size). If you are unsure of an element of programming style, please ask, as I would be more than happy to show you what I want.

Submission: Use the submit command to turn your program in for grading. You may submit as many times as you like, but only the last submission is kept. In particular, be aware that since only the last submission is kept, if the last one is late, then your assignment is late and will thus receive a late penalty. Also remember that you must be on rucs for submit to work. To submit your work for grading use the command above.