The assignment for this week will utilize file streams.
This assignment is due on February 6th at 23:59.
The first step in every assignment will be to open a terminal window, which will be the environment you use to write, compile, and run your programs.
For a reminder on how to do this step, take a look at Step 2 of PA 01.
Start by changing into your CS 16 directory:
$ cd cs16
And then create and move into a PA 05 directory:
$ mkdir pa05 $ cd pa05
Remember that at any time, you can check what directory you are current in with the command pwd.
This week, you will need to create a three files called stddev.cpp, operators.cpp, and wordcount.cpp:
$ touch stddev.cpp operators.cpp wordcount.cpp
You will now need to complete each source file. Each corresponds to one of the problems listed below, which make up this lab.
For a reminder on how to open the text editor and how edit files, take a look at Step 5 of PA 01.
This assignment consists of three problems, each of which is described below. The first and last problems are worth 30 points each, and the middle one is worth 40 points. Each should be solved in its own file and all three must be submitted for full assignment credit.
This should be solved in the stddev.cpp file.
Write a program that takes its input from a file of numbers of type double. The program outputs to the screen the standard deviation of the numbers in the file. The file contains nothing but numbers of type double separated by blanks and/or line breaks. The standard deviation of a list of numbers x1, x2, x3, and so forth is defined as the square root of:
((x1 – a)2 + (x2 – a)2 + (x3 – a)2 + ...) / (n - 1)
Where the number a is the average of the numbers x1, x2, x3, and so forth and the number n is the count of how many numbers there are.
Your program should take file name as input from the user.A session should look like the following example (including whitespace and formatting), with a possibly number in the output:
Enter filename: nums.txt The standard deviation is 16.956
The string printed by the program should include a newline at the end, but no other trailing whitespace (whitespace at the end of the line).
The standard deviation must be displayed to exactly three digits after the decimal point.
This should be solved in the operators.cpp file.
Write a program that will correct a C++ program that has errors in which operator, << or >>, it uses with cin and cout. The program replaces each (incorrect) occurrence of
cin <<with the corrected version
cin >>and each (incorrect) occurrence of
cout >>with the corrected version
cout <<
Allow for the possibility that there may be any number of whitespace characters (one or more) between cin and << and between cout and >>. The replacement corrected version has only one blank between the cin or cout and the following operator.
Your program should get the source filename as an input from the user. The corrected version should be output to a file with name “corrected.txt” and it should also be displayed on the terminal. That means output of your program (after getting the filename) should be exactly same as the contents of the file “corrected.txt”. Your program should define a function that is called with the input- and output-file streams as arguments.
A session should look like the following example (including whitespace and formatting):
Enter filename: original.txt #include <iostream> using namespace std; int main(){ cout << "Hello!"; return 0; }File “corrected.txt” consists of the above output except first two lines.
This should be solved in the wordcount.cpp file.
Write a program that prompts the user to input the name of a text file and then outputs the number of words in the file. You can consider a “word” to be any text that is surrounded by whitespace (for example, a space, carriage return, newline, etc.) or borders the beginning or end of the file.
The program should print a string of text to the terminal before getting each piece of input from the user. A session should look like one of the following examples (including whitespace and formatting), with a possibly number and filename in the output:
Enter filename: words.txt There are 1000 words in the file "words.txt".
Enter filename: oneword.txt There is 1 word in the file "oneword.txt".
The string printed by the program should include a newline at the end, but no other trailing whitespace (whitespace at the end of the line).
To compile our code, we will use the same g++ command as last week. The following three commands will compile the three source files (in the same order as listed above):
$ g++ -std=c++11 -o stddev stddev.cpp
$ g++ -std=c++11 -o operators operators.cpp
$ g++ -std=c++11 -o wordcount wordcount.cpp
If the compilation is successful, you won't see any output from the compiler. You can then use the following commands to run your programs:
$ ./stddev
$ ./operators
$ ./wordcount
If you encounter an error, use the compiler hints and examine the line in question. If the compiler messsage is not sufficient to identify the error, you can search online to see when the error occurs in general.
Remember to re-compile the relevant files after you make any changes to the C++ code.
Once you are satisfied that your program is correct, it is time to submit it. Login at https://submit.cs.ucsb.edu/session, then navigate to “CS16_s15” and click on “Programming Assignment 5”. Then click “Make Submission”, and make your submission the same way as last week. Remember to submit all three .cpp files.
Please remember that you must submit the program to obtain any credit for the assignment; just completing the program is not enough.
Once you submit, you should see a page detailing your submission. The system will automatically grade your program and will show you the results on this page after a 1 minute delay.
You can alternatively submit your code from the command line (terminal) on any CS machine, including the Phelps lab machines or the CSIL server. You can use this method when logged in remotely. To submit the the three source files to this assignment by running the command:
$ ~submit/submit -p 428 stddev.cpp operators.cpp wordcount.cpp
You can copy the URL shown in the output of the above and paste into a web browser to reach the submission result page.
After the 1 minute delay, the submit system will show your score and give you feedback on your submission. Refresh the webpage after a minute to see this information.
You may submit multiple times. You should submit only after local compilation does not produce any errors and runs as expected. The score of the last submission uploaded before the deadline will be used as your assignment grade.
Once your submission receives a score of 100/100, you are done with this assignment.
If you are in the Phelps lab or in CSIL, make sure to log out of the machine before you leave. Also, make sure to close all open programs before you log out. Some programs will not work next time if they are not closed. Remember to save all your open files before you close your text editor.
If you are logged in remotely, you can log out using the exit command:
$ exit