Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assignment 3: Going Postal! In this assignment, you will write a program that co

ID: 3829354 • Letter: A

Question

Assignment 3: Going Postal!

In this assignment, you will write a program that could be used by the United States Postal Service (USPS): it reads in a postal bar code representing a zip code, and returns the zip code in digits.

You can also see the description of this problem in exercise P4.17 on pg. 225 of your book. (The actual description of the postal bar coding is given in P4.16 starting on pg. 224.)

Your program should prompt the user to enter a postal bar code, with the character | representing a long bar, and the character : representing a short bar. No spaces should be entered. If a valid code is entered, the program should output to the console the 5-digit zip code. If an invalid code was entered, the program should output an error message to the console.

The first and last bars input should always be long bars (the frame bars), and there will be a 6th check digit to make the sum of the numbers a multiple of ten.

The bar code for each digit is given by a 5-bar block (consisting of 2 long bars and 3 short bars in some order). The code for each digit is given in the table below. Here, a 1 represents a long bar, while a 0 represents a short bar.

Implementation details:

You should separate your code into functions. Your program should contain at least two functions (aside from main()) that do the following:

One function should take a 5-bar block as parameter and return its corresponding digit (or return a different integer if there is an error).

Another function should take in the entire string of bars as parameter and return the 5-digit zip code, or return a different integer if there is an error. Hint: call the first function!

Think about the possible errors that could occur in the entry of a bar code:

The correct number of bars must be entered. If not, it is an error.

The first and last bars must be long frame bars. If either is a short bar, it is an error.

The check digit must cause the sum to be a multiple of 10. If not, it is an error.

If any 5-bar block does not correspond to a number, it is an error.

Some trial runs are provided below. As usual, user input is given in red.

Sample trial 1:

Enter a postal bar code: |:|::|:|::|:::||||::::||::|:::||

The code entered is invalid.

Sample trial 2:

Enter a postal bar code: ||:|::||:::||:::::|:|:|::|:|:|:|

The zip code is 90024.

Sample trial 3:

Enter a postal bar code: |::|:|:::||::||::||::|:::|:::|||

The zip code is 21367.

Note: if the zip code happens to start with one or more 0's, they will not be displayed in the final output on the console. For example, the zip code 00124 will be displayed as "124". You may assume that the user has the ability to interpret any missing digits on the display as initial 0's. You are not required to include additional code to handle this inconsistency.

Explanation / Answer

$address = urlencode('1600 Pennsylvania Avenue, Washington, DC'); $json = json_decode(file_get_contents("http://where.yahooapis.com/geocode?q=$address&flags=J")); print_r($json);