/* * This is a possible solution to the cipher_functions3.cpp program you were asked to write for * this week. It updates the cipher_functions2.cpp progrm by adding the ability to pass a pointer * to an array as an argument in each of the encoding functions, and testing this by declaring the * array storing encoded messages as a local variable in main instead of as a global variable. * */ //Remember tht there are a bunch of libraries that need to be included if we are generating random //numbers. #include #include #include #include using namespace std; //A list of all the functions. void add_cipher(int *arr, int key, int mod, int n); void mult_cipher(int *arr, int key, int mod, int n); void affine_cipher(int *arr, int key1, int key2, int mod, int n); void add_cipher_rand(int *arr, int mod, int n, int range); void mult_cipher_rand(int *arr, int mod, int n, int range); void affine_cipher_rand(int *arr, int mod, int n, int range); void set_array(int *arr, int n); int rand_0toN1(int n); int main() { //As was menetioned above, since we now know how to pass a pointer as a function argument, we //can declare all the relevant array variables locally, which is what is done below. An arr_size //variable is declared to make it easier to modify the program (as with the #define directive //last week. int arr_size = 10; int A[arr_size]; //Declare a variable for the range of random numbers, and set the seed for random number //generation. int range; srand(time(NULL)); //Set all the array values to be the numbers 0 through arr_size-1. set_array(A, arr_size); //Prompt to get the range of random number values. cout << "Enter the range of the random cipher keys and press ENTER (note that the actual "; cout <<"keys will be from 0 to the number entered minus 1): "; cin >> range; //I only test the random cipher functions, but feel free to test a different set of functions //to make sure that everything is working properly. cout << "The random encoding generated by an additive cipher is: \n"; //Implement the add_cipher_rand function, and then print out the results. Perhaps you can try //and guess from the output what the key was. add_cipher_rand(A, arr_size, arr_size, range); for(int i = 0; i < arr_size; i++) { cout << A[i] << ", "; } //Reset the values in A using the set_array function. set_array(A, arr_size); cout << "The random encoding generated by an multiplicative cipher is: \n"; //Implement the mult_cipher_rand funtion, print everything out, and then reset the array. mult_cipher_rand(A, arr_size, arr_size, range); for(int i = 0; i < arr_size; i++) { cout << A[i] << ", "; } set_array(A, arr_size); cout << "The random encoding generated by an affine cipher is: \n"; //Implement the affine_cipher_rand function, and print out the results. Don't worry about //resetting A since we are at the end of the program affine_cipher_rand(A, arr_size, arr_size, range); for(int i = 0; i < arr_size; i++) { cout << A[i] << ", "; } return 0; } //Addative cipher function. It is just like the add_cipher function from cipher_functions2.cpp, //except that it also takes a pointer to the arry to be modified as a paramter. This allows us to //remove the global array variable from the program, and instead modify a local array variable. void add_cipher(int *arr, int key, int mod, int n){ for(int i = 0; i < n; i++) { *arr = (*arr + key) % mod; arr++; } } //Multiplicative cipher function. As with the new add_cipher function, the only difference between //this mult_cipher function and the one from cipher_functions2.cpp is that it takes a pointer to an //array local to the main function as an argument. void mult_cipher(int *arr, int key, int mod, int n){ for(int i = 0; i < n; i++) { *arr = (*arr * key) % mod; arr++; } } //Affine cipher function. As with the new versions of the add_ciper and mult_cipher function, the //only difference between its cipher_functions2.cpp counterpart is that it takes as a paramter a //pointer to an array, and modifies this array instead of globally declared array. void affine_cipher(int *arr, int multkey, int addkey, int mod, int n){ for(int i = 0; i < n; i++) { *arr = ((*arr * multkey) + addkey) % mod; arr++; } } //Random version of this weeks add_cipher.cpp function. void add_cipher_rand(int *arr, int mod, int n, int range) { int r = rand_0toN1(range); add_cipher(arr, r, mod, n); } //Random version of this weeks mult_cipher.cpp function. void mult_cipher_rand(int *arr, int mod, int n, int range) { int r = rand_0toN1(range); mult_cipher(arr, r, mod, n); } //Random version of thsi weeks affine_cipher.cpp function. void affine_cipher_rand(int *arr, int mod, int n, int range) { int r1 = rand_0toN1(range); int r2 = rand_0toN1(range); affine_cipher(arr, r1, r2, mod, n); } //New version of set_array.cpp function that takes a pointer to an array as well as a parameter, //and modifies this array. void set_array(int *arr, int n){ for(int i = 0; i < n; i++) { *arr = i; arr++; } } //The rand_0toN1 function that should be familiar by now. It takes an integer as a parameter, and //returns a number between 0 and that number minus 1. Same as from last week. int rand_0toN1(int n) { return rand() % n; }