Pseudocode, test cases, and counting operations – 40 course points
In this assignment you will develop algorithms for problems described below, write test cases that exercise all the possible algorithm execution paths, and calculate the number of operations that are executed in certain algorithmic paths.
For each of the following problems provide:
- error conditions or pre-conditions
- an algorithm using the pseudocode reference used in class
- a set of test cases
- the minimum number of operations executed in your algorithm
- the maximum number of operations executed in your algorithm
- Largest. Write an algorithm without loops that reads 5 distinct integers and displays the largest value. Assume the input values are distinct integers.
- Check Digit. Find a UPC bar code somewhere around your house. It will look something like the following:
The first image is a UPC-A code (it has 12 digits). The second is an EAN-13 code (it has 13 digits). They both work similarly and we will use the first (UPC-A) code for the first example. You can use either, but make sure to use your symbol, not our to illustrate something about bar codes.
- Sum every other digit of the code, starting from the right. In the example, that is 2 + 6 + 1 + 1 + 2 + 4 = 16. Discard the tens digit and keep the ones digit, 6.
- Start with the second to last digit and do the same thing. Sum the digits, discard the tens digit and keep the ones digit. In this example this is 2 + 2 + 3 + 3 + 8 + 0=18. Discarding the 10 leaves 8. Multiply this number by 3 and again discard the tens digit. 8×3 = 24, leaving 4.
- Add the numbers from steps 1 and 2. Again drop the tens digit. 6 + 4 = 10, leaving 0 (zero).
Lets try the same thing for the EAN-13.
0 + 3 + 5 + 0 + 4 + 8 + 9 = 29, leaving 9.
1 + 8 + 4 + 7 + 0 + 7 = 27, leaving 7. 7×3 = 21, leaving 1.
9 + 1=10, leaving 0.
What a coincidence! For both codes, the number we are left with is 0. What number did you get for yours? Was it also 0? If not, check you arithmetic. Both codes work the same way. The last number of the code is a check digit. A computer can quickly check to see if all of the digits of the code are read correctly by the scanner by performing this simple calculation. If the result is 0, it accepts the scan and enters the number. If the result is not 0, the computer will reject the number and give the clerk a warning, usually auditory, to signal that the item needs to be rescanned or the code entered manually.
Write an algorithm that reads a code and displays ACCEPT if after performing the computation described above the result is 0 (zero) or REJECT if the result is not 0 (zero).
- Hint 1: to extract the rightmost digit of a number use: COMPUTE digit AS number MODULUS 10
- Hint 2: to remove the rightmost digit of a number use: COMPUTE number AS number / 10
- Hint 3: for full credit use a loop to perform the computation
- Assume the input value is a positive integer.
- Collaboration policy. Read our collaboration policy here.
- Use your preferred text editor to type your answers.
- Submitting your assignment. You will use Gradescope to submit your assignment. If you have not yet used Gradescope, go to your scarletmail inbox and look for an email from Gradescope with instructions on how to access your account.
- On Gradescope. Find Assignment 1 – Pseudocode. Copy your solution to problem 1 and 2 into their respective boxes.
If anything is unclear, don’t hesitate to drop by office hours or post a question on Piazza. Find instructors office hours by clicking the Staff link from the course website. You are welcome to ANY professor office hours.
In addition to office hours we have the CAVE (Collaborative Academic Versatile Environment), a community space staffed with lab assistants which are undergraduate students further along the CS major to answer questions.