Lab 2
Objectives:

Practice debugging a program.

Use basic branching and looping statements.

The assignment contains two parts.

Mini Sort Study Debugging. Study the following program. The program attempts to sort three integer numbers in the increasing order. However, it is incomplete. For example, it does not work for the input 2 3 1 . Debug the program. Try using gdb. See information about gdb.Demonstrate your testing by creating a script file of your tests. Name your script yourusername2.txt. You are not allowed to use the concepts, such as looping or functions.

Submit the corrected program and your script to the subversion repository.


Encrypt-decrypt. A company wants to transmit data, but they are afraid to send it and having it tapped. All of their data is transmitted as four digit natural numbers. Your program should repeatedly ask the user if they wish to encrypt or decrypt until the user signals they no longer wish to transmit data. Name your program encrypt_decrypt.cpp.

Initially, the user will be asked if they wish to encrypt (input 'E' or 'e') or decrypt (input 'D' or 'd'). The user will input a four digit natural number. To encrypt replace each digit with the sum of the digit plus seven modulus 10. Then swap the first digit with the third and the second digit with the fourth. Output the result with an appropriate label. (Obviously, this scheme could be easily broken, but the idea is to have you practice writing code, not design a good security algorithm!)

Decryption takes an enrypted four digit natural number and recalculates the original data and outputs it with an appropriate label.

Ask the user if they wish to transmit more data and have them enter 'Y' (or 'y') for yes or 'N' (or 'n') for no. If 'Y' is input, repeat the encrypt-decrypt question. Otherwise, terminate the program.

Hints: Note that given 3579, modulus and quotient provide the necessary algorithm for breaking a 4 digit integer into 4 separate variables.

3579 % 10 is 9;

3579/10 is 357.

Continue to pluck off the next digit 7.

To decrypt, add 3 and mod by 10, because 7+3 = 10.

You may not use arrays or user-defined functions. Try to look for common tasks that can be coded once rather repeadly. Make sure your program adheres to proper programming style. Submit your project to the subversion repository.

It is always a good idea to build your program by handling a few tasks first. For example, one approach might be to write code for doing input and output and test. Then perhaps add the code to put the digits in 4 different variables and test again. Add the calculations for each digit and test. Code the swaps and test. Then complete the control structures to make the choice between encrypting and decrypting and test. Finally, wrap the code in a loop that will allow a user to do encrypting or decrypting repeatly and, of course, test again!

Although the above approach looks like it will take a lot of time, throwing everything into one coding will often result in a "Big Bang" test result - i.e. everything explodes and you will spend more time trying to find bugs in this case.

Do not forget to verify your submission on the web.