Lab 09 LAB 09 More on Data Types, Decisions, and Loops PURPOSE 1. To learn more about data types and reading characters 2. To learn about combined assignments and formatted output 3. To learn about...

have one more file but cannot upload more


Lab 09 LAB 09 More on Data Types, Decisions, and Loops PURPOSE 1. To learn more about data types and reading characters 2. To learn about combined assignments and formatted output 3. To learn about the switch statement and the conditional operator 4. To learn about the do-while loop and the for loop (sentinels, flags, break & continue) (MOVE TO L11) PROCEDURE Create a project with the name of your choice in Visual Studio C++ and follow the instructions below. 9.1 Encoding and decoding text Bring in the program cipher.cpp from the Lab 9 folder. The code follows: _____________________________________________________________________________ // 9.1 cipher.cpp // Encode and decode text ==================== #include using namespace std; // FILL IN A STATEMENT for the prototype of the function // as defined below int main() { // == Select Encode or Decode ========================================== bool encode; // FILL IN A STATEMENT that defines an unsigned integer called // FILL IN THE CODE for a do loop while is greater than 1. // It contains the following menu and statements: cout < "select:="" \n";="" cout="">< "="" 0:="" decode\n";="" cout="">< "="" 1:="" encode\n";="" cout="">< "="" 2:="" help\n";="" fill="" in="" a="" statement="" to="" read=""> // FILL IN THE CODE for a switch statement that tests the following // cases for // 0 sets to false and break // 1 sets to true and break // default write to console: "1. Select decode encode\n2. Enter a key // (a whole number)\n3. Enter a phrase\n\n" // == Read Secret Key =================================================== int secretKey; cout < "enter="" encryption="" key:="" ";="" cin="">> secretKey; // FILL IN A STATEMENT that defines two string variables called // and // FILL IN THE CODE that writes to the console the prompt "Enter phrase:" and // reads . // Hint: use to clear the newline before reading. // == Translate phrase into decoded or encoded phrase ================== codeDecodeFromTo(selection, phrase1, phrase2, secretKey); cout < "translation:\n"="">< phrase2="">< endl;="" return="" 0;="" }="" encoder/decoder="=========================================" fill="" in="" the="" code="" that="" defines="" a="" void="" function="" called="">. // In contains the following parameters: a boolean called, // a constant string passed by reference called , // a string passed by reference called , // and a unsigned integer called { const int FAC = ' '; // min ascii printable char (32) = ' ' const int LAC = '~'; // max ascii printable char (126) = '~' // FILL IN A STATEMENT that defines an unsigned long integer called // and initialize it to the size of ( call its size() method ). // FILL IN A STATEMENT that calls the method of // with argument srand(key); int charnum = LAC - FAC + 1; // number of printable characters // FILL IN THE CODE for a for loop with index

for 0, 1, ..., size-1 // It includes the following three statements // 1. FILL IN A STATEMENT that defines an integer called // initialized to rand()%charnum // 2. FILL IN A STATEMENT that defines and integer called // initialized to r if is true, and to charnum-r, if it is false. // Hint: Use the conditional operator // 3. FILL IN THE CODE that sets tostr[i] to // ((frstr[i]-FAC)+disp)%charnum + FAC } _____________________________________________________________________________ This program encodes ascii characters (text) by replacing each symbol with a different one, chosen according to a displacement (rotation) in the order of all printable ascii characters. For example, if only the first six letters of the alphabet are considered, the encoding abcdef -> efabcd cyclicly rotates the characters to the right by 2, replacing for example the letter ‘c’ with ‘a’, and ’b’ with ‘f’. To make it harder to decipher, these rotations are performed independently for each character in a message according to a random sequence of numbers. The seed to the random number generator that generates this sequence is the key for encoding and decoding the message. To decipher the message, the rotations are performed in the opposite direction (left). Exercise 1: Fill in the missing statements in bold and run the program. Output example: Select: 0: Decode 1: Encode 2: Help 2 1. Select decode encode 2. Enter a key (a whole number) 3. Enter a phrase Select: 0: Decode 1: Encode 2: Help 1 Enter encryption key: 3452 Enter phrase: Programming in C++ Translation: mr[gjV!E+Va$Nt0W_| Select: 0: Decode 1: Encode 2: Help 0 Enter encryption key: 3452 Enter phrase: mr[gjV!E+Va$Nt0W_| Translation: Programming in C++ 9.2 Modular exponentiation Bring in the program modularexp.cpp from the Lab 9 folder. The code follows: _____________________________________________________________________________ // 9.2 modularexp.cpp #include #include // FILL IN A STATEMENT with the header for formatting output using namespace std; // FILL IN A STATEMENT for the prototype of the function // described below int main() { // FILL IN A STATEMENT that defines an unsigned integer called. // Initialize it to 2^(bits of unsigned) - 1 // Hint: use the pow() for ^ and use the sizeof() function // and multiply by 8 to get bits // FILL IN A STATEMENT that displays the message "Max size = " followed // by formatted to display the width of 10 characters cout < "solve="" c="b^k" %="" m\n";="" unsigned="" long="" c,="" b,="" k,="" m;="" cout="">< "b="; cin >> b; cout << " k="; cin >> k; cout << " m="; cin >> m; c = powmod(b, k, m); cout << " \nc="<< b=""><><><") %="" "="">< m="">< "="; cout << " ("="">< (unsigned="" long)pow(b,="" k)=""><") %="" "="">< m="">< " = "; cout << ((unsigned long)pow(b, k))%m << endl; return 0; } // modular exponentiation =================== // description: solve c = b^k % m // fill in the code that defines a function called that returns // an unsigned value with three parameter: an unsigned long integer // called

and two unsigned integers called and . // the function contains the following: // fill in a statement that returns b%m if is 1 // fill in a statement that defines an unsigned long integers called

// fill in the code that performs the following statements: // 1. if k is odd // a = powmod(b, k-1, m); // return (a*(b%m))%m; // 2. otherwise // a = powmod(b, k/2, m); // return (a*a)%m; _____________________________________________________________________________ the most important formula for encryption is called the modular exponentiation c = bk mod m where mod is the modulus operator (i.e., % in c++). given b, k, m, is relatively straight forward to find c. however, finding k given the others can only be solved by trial and error. if m is large, with several hundred digits, finding k is nearly impossible even for the fastest computers. this property is exploited in for transmitting secret encryption keys in the open as we will see in the next problem. however, computing bk can be nearly impossible simply using pow(b, k)%m. if k is a large number, bk is astronomical. fortunately, a mathematical identity allow the simplification and calculation of the modular exponentiation. this identity states that if r is the product of two integers p, q, i.e., r = p q, then r mod m = [(p mod m) · (q mod m)] mod m . the identity leads to the map bk mod m = [(bk�a mod m) · (ba mod m)] mod m , for 1  a  k . this formula is used recursively to obtain c. the program above implements this formula and compares it to computing c directly with pow(b, k)%m. exercise 1: fill in the missing statements in bold and run the program. exercise 2: try large values for k and m and notice how bk quickly grows beyond computer precision. output examples: max size =


"=""> that returns // an unsigned value with three parameter: an unsigned long integer // called

and two unsigned integers called and . // the function contains the following: // fill in a statement that returns b%m if is 1 // fill in a statement that defines an unsigned long integers called

// fill in the code that performs the following statements: // 1. if k is odd // a = powmod(b, k-1, m); // return (a*(b%m))%m; // 2. otherwise // a = powmod(b, k/2, m); // return (a*a)%m; _____________________________________________________________________________ the most important formula for encryption is called the modular exponentiation c = bk mod m where mod is the modulus operator (i.e., % in c++). given b, k, m, is relatively straight forward to find c. however, finding k given the others can only be solved by trial and error. if m is large, with several hundred digits, finding k is nearly impossible even for the fastest computers. this property is exploited in for transmitting secret encryption keys in the open as we will see in the next problem. however, computing bk can be nearly impossible simply using pow(b, k)%m. if k is a large number, bk is astronomical. fortunately, a mathematical identity allow the simplification and calculation of the modular exponentiation. this identity states that if r is the product of two integers p, q, i.e., r = p q, then r mod m = [(p mod m) · (q mod m)] mod m . the identity leads to the map bk mod m = [(bk�a mod m) · (ba mod m)] mod m , for 1  a  k . this formula is used recursively to obtain c. the program above implements this formula and compares it to computing c directly with pow(b, k)%m. exercise 1: fill in the missing statements in bold and run the program. exercise 2: try large values for k and m and notice how bk quickly grows beyond computer precision. output examples: max size =


>
May 18, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here