# ITEC 320: Program 2

#### Submit command: submit itec320-01 isbntest.adb

1. No updates at this time.

Coming Soon: 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.

This assignment works with ISBN book identification numbers and gives you an opportunity to use arrays, strings, characters, procedures, and functions. Each ISBN contains a check digit for detecting and correcting certain errors. A valid ISBN will have a check sum of 0, and for this assignment you will determine whether a ISBN's check sum is correct, and in certain cases you will do very simple error correction.

Originally the ISBN was 10 digits long, and later it was expanded to 13 digits to allow for more books and to be compatible with the 13 digit UPC code. For this assignment you will work with the ISBN of length 10.

Sample run: Assume that you have an input file, called  isbn.txt, that contains the following:

2111111111
77-77-77-7777
2222222222
3333333332
0-306-40615-2
0-306-4?615-2
0-306-4061?-2

The output from executing your program (ie executing isbntest < isbn.txt) will look like the following:
rucs@/home/nokie> isbntest < isbn.txt
Valid Check Sums:
7777777777 0
2222222222 0
0306406152 0
0306406152 0
0306406152 0

Invalid Check Sums:
2111111111 1
3333333332 1


Input: Input will a sequence of ISBN , with each number appearing on a single line, with no white space on the line. There will be fewer than 1000 numbers, but there is no limit to the length of the input line. The characters in the ISBN can be any of the following:

• Decimal digits, or 'X' representing the value 10. In real ISBN, the X appears only in the tenth (ie rightmost) position, but we will allow it in any position.
• Dashes - there can be any number of dashes (ie '-') in any position.
• Question Mark - A number can contain a single question mark (ie '?') that represents a missing digit. When you print the number, ou will replace a question mark with the correct digit.
You should read from standard input, until end of file. The input may contain errors as described below.

Output: Output for correct input lines will consist of one line of output for each number. On that line you will output the number, possibly modified, and it's check sum.

You are to remove all dashes from the number, and any question mark is to be replaced by the number that produces a valid check sum.

Output should begin with all of the valid numbers and their check sums (ie 0), followed by all of the invalid numbers and their check sums (ie not 0). Numbers should be indented and one space should separate each numbers from its sum. Follow the format shown in the sample run.

Errors: You are to detect the following errors in the input:
1. Input line too short or too long (after dashes are removed)
2. Invalid character in the line (ie other than decimal digit, 'X', '-', '?')
• This includes white space anywhere on the input line
3. More than one question mark in the line
When an error occurs, your program should print an error message along with the invalid line and then continue the program. The invalid string should be printed with chevrons (ie '<' and '>') as delimiters so that you can see the presence of initial and trailing blanks. See the assignment checker for examples.

When an occurs, you should print the line number along with the invalid line. If a line contains more than one error then you can choose which message to print.

Check Digits: In checking a ten-digit ISBN you must verify the following formula: $$\sum_{i=1}^{10} i*s(i) \mod 11 = 0$$ Where s(i) is the ith digit of the ISBN. This assumes that the digits are numbered from left to right, starting at 1. Thus, the check sum is (1*s(1) + 2 * s(2) + ... + 10 * s(10)) mod 11. For the curious, notice that the same formula works if the digits are numbered in the other direction, and there is also a formula for calculating the same sum without using multiplication at all!

There are several techniques for finding the value to use in place of a '?'. One approach is as follows:

• Temporarily replace position P (ie the '?') with '0'
• Calculate the check sum of this number, and call it C
• Find the missing number M that satisfies (P * M + C = 0) mod 11 . This is equivalent to finding M such that P * M = -C, where -C is calculated mod 11 (ie 11 - C). Since there are only 11 values to choose from, you can do this by a table lookup or simply use a loop!
• Replace position P with the character for M (ie s(P)= digit2char(M)). If M is 10, then use the character 'X'.
Note that ISBN-13 uses a different check sum calculation, one that unfortunately does not detect as many errors. [What errors do you think this method detects?]

Other points:

• Your main routine should be no more than a few (ie 3) statements. Make sure that you do not use any global variables!
• You must use fixed length strings and one or more (non-string) arrays in this assignment.
• You must not use type Unbounded_String.
• You must use at least one procedure with at least one out or in out parameter.
• You will have to convert between characters and digits. That's covered in the FAQ, and we can discuss it in class.
• Make good use of types, procedures, and functions in the assignment.

Interactive Input: You will of course want to have input files that you use for testing your program. However, if you want to do a quick interactive run, then it's useful to know how to denote EOF interactively. (Since of course there is no EOF if there is no file!) On Unix end_of_file will return true if control-D is pressed; on Windows it will return true if control-Z and then enter are pressed.

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 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.

Submit: 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 at the top.