Introduction to Computer Programming: C++

Assignment #0: Decipher

Due

Thursday, 2nd October at noon

Overview

This is an assignment that will help you get to know C++, and help you get comfortable using arrays. You'll have to figure out how to use Visual Studio, write several functions to decipher some encrypted text, and compile and run your code in order to read the decrypted text.

You are going to write a program to decipher an encrypted file. You will take input from standard in, which contains encrypted text, and store that text (in an array). Below, I supply input files - you can specify that a particular file is to be used as standard input with Visual Studio: under the Project tab, go to Properties, then under Configuration Properties, click on Debugging. On the right, set Command Arguments to specify your input file so that it is standard input, such as "< ../Neruda.in". After you have the text file in an array, you will decipher the text or figure out the encryption pattern, then decrypt the text and print it to standard output using the exact same formatting as the original input (with punctuation, spaces and newlines).

The text (see files Neruda.in and Coleridge.in) is encrypted by having each letter of the alphabet rotated by a fixed amount. In order to decipher the text, you have to first analyze the most frequently occurring character in the text. When analyzing, look only at alphabetic characters; ignore non-alphabetic characters, and ignore whether letters are uppper or lower-case when finding the frequency. When you find the most frequently occurring character, because the most common letter in the English language is "e", you know that the character identified as the most frequent signifies "e". All encrypted alphabetic characters are a particular shift distance away from their decrypted character, and so you can use the character frequency calculation to find that shift distance. For example, if the most frequent text character is "h", then you know the distance is -3 (3 backwards) because "h" is 3 away from "e", but notice to decrypt you have to rotate backwards through the alphabet. Note that the alphabet forms a circle, so rotating backwards 3 is the same as forwards by 23. Numbers, punctuation, and whitespace have not been rotated, only letters. When decrypting, you should preserve the case of letters - i.e. "H" becomes "E" and "h" becomes "e". When you decrypt the text, you have gotten it right if it looks like proper English (unlike the encrypted text).

The characters you will use come from the ASCII chart, which represents chars as numbers. "A" through "Z" are 65 through 90, and "a" through "z" are 97 through 122. However, you should not use these literals (these numbers) in your program - C++ will allow you to use a char as an int which gets the ASCII value. For example you can check if a char (for example a variable "ch") is upper case by saying: if ((ch >= 'A') && (ch <= 'Z')). You can also use standard library functions like isupper(c) and islower(c) (the proper library is included in the starting C++ file). You also might need to normalize a letter to the beginning of the alphabet before doing the rotation, for example, subtracting 'a' from a lower case char before rotating it the proper distance, and then adding 'a' back.

Instructions

  1. Write some code inside decipherA0.cpp (some code is already provided that reads in the encrypted text and creates a char array to hold it) that deciphers encrypted text, without using dynamic allocation. Make sure your name is in the top of the file!
    1. You will define a function max_frequency that takes a constant char array, figures out the most frequent char, and returns it.
    2. You will define a function rotate that takes a char array and an integer rotation distance, and rotates each alphabetic char in the array by the specified amount (using the rules above).
    3. You will also have to write code in main to call the functions above, and print your deciphered text to standard out. The input files you can use to experiment on are Neruda.in and Coleridge.in.

    Compile and run your code in Visual Studio. Use Build Solution and Start With Debugging (to debug) or Start Without Debugging.

  2. When you're happy with your code, submit your decipherA0_[yourLastName].cpp file to the DropBox in Minerva. You can turn in your file as many times as you want - I will only take the last one submitted.

Submission Checklist

Extra Credit (+4%)

Answer the following questions in written form in comments after your name at the top of decipherA0_[yourLastName].cpp.