Question
A company wants to transmit data through the phone but they fear their lines are monitored. All data is transmitted as four-digit non-negative integers. They
A company wants to transmit data through the phone but they fear their lines are monitored. All data is transmitted as four-digit non-negative integers. They have asked you to write a program in C++ that encrypts their data so that transmission is safer. Note that numbers with fewer than four digits are assumed to have the required zeros on the left in order to be four-digit. For example, the number 52 corresponds to 0052. You won't have to add the zeros to the numbers.
Your program should have these three functions:
int readNumber(); //function to read a four-digit number int encrypt(int data);
//function to encrypt data int decrypt(int data); //function to decrypt data
The function int readNumber() should prompt the user to enter a nonnegative integer. It will then read the number from the input and perform the following correctness checks:
If the number is negative or has more than four digits, your program should output an appropriate error message and terminate.
If the number has fewer than four digits you can assume that the first ones are zeros.
The function should return the nonnegative four-digit number, if it passes the correctness checks.
o HINT: You can return a negative number for invalid input numbers and use this in your main program to terminate it. The function int encrypt (int data) will encode the four-digit integer given as a parameter in the following way:
Each digit of the number is replaced by (the sum of the digit with the number 7) modulo 10. NOTE: The modulo operator % gives the remainder when the first operand is divided by the second. For example, "11 % 4" is equal to 3 (that is, the remainder of 11 divided by 4 is 3).
Change the position of the first digit to the fourth and of the second digit to the third.
The result is the encoded number that should be returned by the function.
The function int decrypt (int data) will perform exactly the reverse process, decrypting a coded number.
Each digit of the number is replaced by the subtraction of the digit with 7 modulo 10. NOTE: Observe that subtraction of 7 modulo 10 from the digit is the same as (the sum of the digit with the number 3) modulo 10.
Change the positions of the 1st digit with the 4th and the 2nd digit with the 3rd.
Return the decoded number.
Use these functions in main () as follows:
First, you will ask the user to select if he/she wants to encode or decode an integer with an appropriate menu of options.
o If the user enters an invalid option, the program should terminate and output the message: "Wrong choice. Try again..."
Then, your program should call the function readNumber() to prompt the user to give a number.
If the input number is valid, and based on the menu option selected, your program should call the appropriate function to encrypt or decrypt the number entered by the user.
Finally, you should output the results to the screen.
Sample runs of your program should look like:
When the user selects to Encode or decode a number:
Please select one of the following:
1. Encode a number.
2. Decode a number.
Enter choice: 1
Enter the four digit number: 7645
The encrypted number is: 2134
When the user enter an invalid menu option:
Please select one of the following:
1. Encode a number.
2. Decode a number.
Enter choice: 3 Wrong choice. Try again...
When the user enter an invalid number:
Select one of the following: 1.
Encode a number. 2.
Decode a number.
Enter choice: 1
Enter the four digit number: 1030123 You must enter a four digit nonnegative number!
Hints:
Use the results of integer division and modulo (%) by powers of 10 to get each digit of the 4- digit number. For example, for a two-digit number you can get the digits using: 35/10 = 3 and 35%10 = 5. For this homework, your function should treat the input numbers as 4-digit numbers.
Multiply each encoded or decoded digit by powers of 10 and add them up to get the encoded or decoded number that the function should return. For example, 3 * 10 + 5 can form the number 35. For this program, you should return a 4-digit number.
o NOTE: Your functions should treat the input numbers as 4-digit numbers for the calculations. However, it is not required to show the zeros to the output if the resulting encoded or decoded number has less digits. For example, if the result for the encoded number is 2, it will be counted as correct if your program outputs 2 (and not 0002).
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started