lab06 : Using File I/O Data Streams & String Manipulation

num ready? description assigned due
lab06 true Using File I/O Data Streams & String Manipulation Wed 05/10 08:00AM Tue 05/16 12:00PM

CS16: Programming Assignment 06

Introduction – Important: Read this!

The assignment for this week will utilize concepts of file I/O data streams and string manipulation. Some of the concepts needed to finish this lab, such as the various string member functions and others items, will be discussed further in the Thursday lecture in class. Again, the TAs and I will be looking for (and grading) programming stylizations, such as proper use of comments, tab indentation, good variable names, and overall block and function designs. So, it is not enough for your lab to pass submit.cs! Please read the instructions herein carefully.

Step 1: Getting Ready

Open a terminal window and log into the correct machine. Change into your CS 16 directory, create a lab06 directory and change into it. Remember that at any time, you can check what directory you are currently in with the command pwd.

Step 2: Create and Edit Your C++ Files

This week, you will need to create 2 files called stddev.cpp and operators.cpp: Each corresponds to one of the problems listed below, which make up this lab.

This assignment consists of 2 problems, each of which is worth 50 points and is described below. Each should be solved in its own file and each must be submitted for full assignment credit.

Note: All these submissions will be checked by the automatic system on submit.cs AND by the instructor and TAs for further evaluation. Details below.


STDDEV.CPP

This program takes its inputs from a file that contains numbers. The program reads them in as 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. The answers should be given with 3 decimal points. Additionally, your program should define at least one function. If your program does NOT have at least one function, you will not get credit for this part of the assignment, even if your program passes submit.cs grading.

A session should look exactly like the following example (including whitespace and formatting - note that there is no whitespace at the end of each of these lines and each printed line has a newline at the end), with all manners of different numbers for inputs and the output:

Enter filename:
nums.txt
The standard deviation is 1.581

The accompanying input file in this example, could look like this (note the separation by one or more spaces):

6 7 8    9			10

or like this (note the separation by either spaces or newline characters):

6 7
8
9
10

OPERATORS.CPP

Write a program 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. You should not correct other whitespace characters in the input file (such as those at the start of a line).

Your program should get the source filename as an input from the user. The corrected version should be output to a file called “corrected.txt” (it cannot be called anything else) and the output should also be displayed on the terminal. That is, the output of your program should be exactly same as the contents of the file “corrected.txt”.

Your program should define at least one function that is called and that manipulates the read line from the input file. If your program does NOT have at least this function, you will not get credit for this part of the assignment, even if your program passes submit.cs grading.

You will need to use multiple member functions to manipulate strings in this program. In addition, you will have to use the getline() function in the library in order to read an entire line by ifstream.

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;
}

The file “corrected.txt” consists of the above output (except first two lines).


Step 3: Create a makefile and Compile the Codes with the make Command

In order to learn another way to manage our source codes and their compilations, we will first create a makefile and put in the usual g++ commands in it. Afterwards, whenever we want to compile our programs, the Linux command is a lot shorter. The use of makefiles will reveal itself to be very useful the more complex our programs and CS projects become.

Using your text editor, create a new file called makefile and enter the following into it:

all: stddev operators 

stddev:
	g++ -std=c++11 stddev.cpp -o stddev

operators:
	g++ -std=c++11 operators.cpp -o operators

Then from the Linux prompt, you can do one of two things: either issue separate compile commands for each project, like so:

$ make stddev

Or, you can issue one command that will compile all the projects mentioned in the makefile, like so:

$ make

If the compilation is successful, you will not see any output from the compiler. You can then run your programs, for example:

$ ./stddev

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.

Step 4: Submit

Once you are satisfied that your programs are correct, it is time to submit them. Login at https://submit.cs.ucsb.edu, then navigate to “CS16_s17” and click on “lab06

”. Then click “Make Submission”, and make your submission the same way as last week. Remember to submit both of the .cpp files.

Please remember that you must submit the programs to obtain any credit for the assignment; just completing the programs 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 732 stddev.cpp operators.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.

Step 5: Check Submission Results

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 this lab 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.

Step 6: Done!

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