Winter 2019

PIC 10B: Intermediate Programming

Discussion Section 3A

Wonjun Lee



Week2

Tuesday

About Streams

Stream example code. Download both files. - stream_ex1.cpp, numbers.txt

Stream quiz - stream_quiz1.cpp


About arguments

Arguments example code - arguments_ex1.cpp


The following problems are from the textbook chapter 9


Exercise R9.1.

Write C++ code to open a file with the name Hello.txt, store the message "Hello, World!" in the file, and close the file. Then open the same file again and read the message into a string variable. Close the file again.


Exercise P9.1.

Write a program that asks the user for a file name and displays the number of characters, words, and lines in that file. Then have the program ask for the name of the next file. When the use enters a file that doesn't exist (such as the empty string), the program should exit.


Exercise P9.2.

Random monoalphabet cipher. The Caesar cipher, which shifts all letters by a fixed amount, is far too easy to crack. Here is a better idea. As the key, don't use numbers but words. Suppose the key word is FEATHER. Then first remove duplicate letters, yielding FEATHR, and append the other letters of the alphabet in reverse order:

formula random

Now encrypt the letters as follows:

formula random

Write a program that encrypts or decrypts a file using this cipher. For example,

./crypt -d -kFEATHER encrypt.txt output.txt

decrypts a file using the keyword FEATHER. It is an error not to supply a keyword.