CS107 - Lab 9
C++ Functions and Cryptography

Due Thursday 04/15

Overview:

This assignment continues with the idea of abstraction in problem solving using functions. The key challenge here is, "How do software designers organize large programs, and how do the parts of these programs communicate with each other when they are running?" The readings and exercises for this assignment are taken from section 7.7 of your text.


Functions

You should carefully review the discussion of C++ functions in section 7.7 before answering the questions in this lab.

Open example9.cpp from the Invitation -> EXAMPLES folder on your desktop. Notice here that the program has two parts, a main program and a function definition named swap.

When the main program begins to run, it first reads two numbers from the user, and then it executes swap(first, second); This is a function call statement. That is, the main program is calling upon the function swap to perform a service for it - namely to exchange the values of the two variables.

Now look at the function definition , which begins with the line void swap(int a, int b) { and ends with the single right brace }. This definition has statements that are executed only when the function is called, and not before. The two parameters a and b are placeholders for the arguments that will be provided by the function call (in this case, first and second). When the function's statements are executed, the results are assigned to the arguments first and second (rather than the parameters a and b).

1. Compile and run example9.cpp. Does it do what you expect it to do? Why not? Explain briefly.

Additional examples of simple C++ programs with functions are called example6.cpp - example8.cpp in the Invitation -> EXAMPLES folder on your desktop. You may want to run some of these to get a better feeling for writing and running programs with functions. Also, you may want to complete practice problems 1-3 on page 347 of your text.

Sorting

Write a C++ program that reads a list of numbers ended by 0 from the user, stores them into an array and sorts them using selection sort. To do this you will use a top-down approach:
  1. Assume you are given the following functions (that is, somebody has implemented them for you):
    //function: reads values from the user ended by a 0, stores them into array a[],
    //and sets n to be the number of values read up to and excluding 0
    void getArray(int a[], int&n); 
    
    //input: an array a[] storing n numbers
    //function: prints the n values stored in array a[]
    void printArray(int a[], int n);
    
    //input: an array a[]  storing n numbers
    //function: finds the largest number in a[] 
    //return value:  the index of the position of the largest element in a[] 
    int findLargest(int a[], int n);
    
    //input: an array a[] storing n numbers; 
    //function: swaps elements i and j in the array 
    void swap(int a[], int i, int j);
    

    Using these functions, write a new function selSort that takes in an array and its size and rearranges the elements of the array so that they are sorted in increasing order.

    //input: assume a[] is an array storing n numbers
    //function: sorts a[] in increasing order using selection sort
    //output: array a[] is in sorted order
    void selSort(int a[], int n);
    
    
  2. Now implement all the functions listed above (some of them you already have from the last lab).

Cryptology using Caesar's Cypher

Cryptology is the science of "secret codes". Messages are encoded before they are sent out for thr purpose of keeping their content secret if they are intercepted by the wrong parties, and they are decoded when they are received to retrieve the original information.

The most famous instances of cryptology occur in military history, beginning with Julius caesar of the Roman Empire, who developed the Caesar Cypher, and certainly including the German Enigma code cracked by the Allies during World War II (by Alan Turing, a British mathematician).

Transmitting information securely has taken a modern turn with electronic commerce on the Internet and concerns over protection of consumer credit card information and other personal data.

A Caesar cypher, also called a shift cypher, involves shifting each character in the message to another character some fixed distance farther along in the alphabet. Specifically, let s be some integer between 1 and 25 that represents the amount of the shift. Each letter in the message is encoded as the letter that is s units farther along in the alphabet, with the last s letters of the alphabet shifted in a cycle to the first s letters.

For example, if s=3, then A is encoded as D, B is encoded as E, X is encoded as A, and Z is encoded as C. Decoding a message requires knowing s. For example, knowing that s=3, the code word DUPB is decoded as ARMY.

Open crypt_decode.cpp and crypt_encode.cpp programs from the EXAMPLES directory and take a look at them. The body of the main functions, encode() and decode(), is missing.

You will work in teams, eiter as encoders, or decoders. The encoding teams will fill in the missing part in crypt_encode.cpp and the decoding teams will fill in the missing part in crypt_decode.cpp. The task is to encode all lower case characters using Caesar cypher with s=3 and leave all other characters unchanged. The goal is for the encoder teams to encode an arbitrary piece of text and challenge the decoder teams to decode it.

Extra credit: Assume the lower case characters have been shifted with an unknown amount s. Write a decode function that tries all possible values of s and picks out the good one.

Work in the following teams:

Encoders:
Team E1: Astrid, Will, Sam
Team E2: Adam, Tom
Team E3: Kevin, Charles, Catoria

Decoders
Team D1: Chris Field, Dan
Team D2: Bryan, Sonia, Chris Sullivan
Team D3: Connor, Hai Anh


Submit your programs to the csci107 -> Drop Box when you are done.



To submit a file electronically, you should first rename it so that you are identified as the author (e.g., give it a name like lab-x-ltoma). Then drag the file to the csci107 -> Drop Box folder. Be careful not to drag an entire folder into the Drop Box; only a single file at a time can be submitted.

Once you are finished in the lab, be sure to drag the CS107 icon to the Trash - this step disconnects you from the server and prevents someone else (who may use this iMac later in the day) from accidentally accessing files in your personal folder.