( Latest Revision:
Wed Nov 23 15:46 PST 2016
)
SIXTH CS 1500 SOLO PROGRAM: Sum & Reverse Program
OBJECTIVE:
The purpose of this programming assignment is to get practice using files,
arrays, and loops.
PREREQUISITES:
You absolutely must be familiar with the content of chapters six (streams)
and seven (arrays) of our text before you start on this assignment!
ASSIGNMENT SYNOPSIS:
Write a program that reads seventeen numbers from an input
file into an array of doubles,
computes the sum of the values in the array,
and writes first the sum and then the list, in reverse order,
to both the screen and an output file.
DETAILS:
To get a clearer idea of what the program is supposed to do,
have a look at
the sample runs.
Notice that in the first sample run, when the program outputs the elements
of the list, each is printed in a six-character-wide field, right justified.
If you utilize output statements like this
cout.width(6) ;
cout << numberList[j] ;
you will be able to achieve the same kind of formatting
with your program's output. The width function utilized above
is discussed on page 328 of your text. Make sure to familiarize
yourself with that material.
INPUT:
You must write the program with certain facts, assumptions, and
preconditions in mind:
- The program must prompt the user for the name of the input file
and the name of the output file. The program must read the data
from the file named by the user, and write the output to
both the screen and the output file.
- The input file must contain seventeen real numbers. To
keep things simple, make all the numbers positive
and less than one-thousand.
- The assignment directory contains a
sample list
that could be the contents of an input file for the program.
- Your program must correctly process the contents of the input file,
whatever particular numbers they are.
- After you turn in your program for grading, I will test it using some
input files that I have never shown to you.
If your program works incorrectly on any of my versions of
the input file, you will lose significant credit.
OUTPUT:
- As mentioned earlier in this document, the program
must write its output both to
the screen and to an output file named by the user of the program.
- The program must format the output just as shown in the
first sample run.
In particular, the program must write all the numbers,
including the sum, in fixed-point notation,
always showing the decimal point,
and always displaying exactly two digits after the decimal point.
In the function that does the writing to the screen and the
output file, you will need to place 'magic' formatting commands
that condition cout and the output file stream so as to
get the right formatting. Be sure you understand the discussion
on pages 325-327 about how to do that.
- When writing to the screen and the output file, the program must
write each of the numbers in the list on a line by itself.
Also it must use the width() formatting function and/or
the setw() manipulator to
right justify each number in a field of width six characters.
THE DESIGN OF THE PROGRAM:
The program must have at least two functions (in addition to
the main() function).
At least two of the additional functions must have
one or more parameters.
No Global Variables: You are not allowed to use any global variables.
On the other hand, if you learn about global constants and
want to try some, this might be a good program for a few of
them - such as the number of items in the file (17), the field width
you are using for formatting numbers (6), and the length of the
arrays of characters you are using to represent filenames (for
example, that might be 256).
I expect you to employ good principles of top-down design
when you develop the program.
I want you to have the experience of writing a program that reads from a file,
writes to a file, and uses an array of doubles appropriately. Therefore
- You must write the program so that it reads the list of
numbers from the input file into an array of doubles.
You have to write
code that reads one number at a time into one array slot at a time,
using a loop to do that reading. (In chapter seven of the text,
there is lots of explanation of how to do this kind of programming.)
- You must not use large numbers of individual variables
to avoid using an array.
- You must use one or more loops (I recommend for-loops) to read in
and write out the numbers.
- You must not try to avoid coding loops by utilizing large
groups of individual input statements or output statements.
You should use this kind of declaration in your program:
char someFileName[256] ;
to give your program a variable needed for accepting a filename from the user.
Do not use a declaration exactly
like the one above, because you must think about what the actual
names for such variables ought to be, and you must choose
good mnemonic names.
Of course, you will actually need two such declarations, one for the
name of the input file, and one for the name of the output file.
The kind of array of characters discussed above
is nice and long - 256 slots - so it has more than
enough capacity for most filenames. 256 slots is enough for 255 characters
in a string, plus the null character ('\0') that is used to mark the end
of the string.
As you may remember from class discussions and/or readings,
the extraction operator [ >> ]
will 'automatically' load a string into such an array of char for you,
including the null character at the end. That is something special
the extraction operator can do with arrays of char but
not with other arrays.
You can use a command like
ifstream fin(fileName) ;
OR
these commands
ifstream fin ;
fin.open(fileName);
to declare an ifstream named fin, and attach it to
whatever the file is whose name is contained in the
variable fileName.
You can declare an ofstream variable and attach it to a file in a
similar manner.
Note: Come to class to get more discussion of how to design the code.
FORM AND FORMATTING REQUIREMENTS:
At the beginning of your program file put a header comment like
this:
/* PROGRAM: Adder/Reverser Program */
/*
Name: Gordon Goodguy
User Name: goodge
Course: CS 1500, Computer Programming I
Instructor: John Sarraille
Date: January 01, 1970
*/
Of course, in place of "Gordon Goodguy," you must put your own full name. In
place of "goodge" put your user (login) name. In place of the Unix epoch,
"January 01, 1970," put the date that you
finished the program.
Note that the comment delimiters /* and */ are important. They need to be
placed correctly or you will get compiler errors.
TESTING:
When you make your test script, it will be OK to do just two tests.
Do one where you give the program the name of a "good" input file.
Be sure to perform the test the same way as in the sample run:
- Start the script,
- Cat the input file,
- Run the program, and
- Cat the output file.
Also perform a test where the input file does not exist.
(Of course, in the case of that test you do not
cat the input or the output file as part of the test.)
When the input file does not exist, the result of the test
should be that the program writes a message saying there
was a failure to open the file, and then
the program stops immediately.
To make it easier for me to check the work in your script, order your
tests so that
- you first perform the test on the "good" input file, and
- second you perform the test for nonexistent input file.
When you do your testing, verify that all the output of the program is
correct. It's not a test if you don't check the results.
WHAT TO TURN IN:
You will be sending me two e-mail messages. Please follow these rules:
- Always send me e-mail as plain text in the main message body.
Never send me attachments.
- Always use the exact subject line I specify for each
message. (I often get hundreds of e-mail messages in a week. The
subject line allows me to find and sort messages.) You will lose a
significant number of points on the assignment if you use the wrong
subject line.
- Be very careful when you send the e-mail. You may use the
instructions in your
Hello World! lab exercise
for guidance. Of course, you will need to make the obvious changes to
those directions -- you have to use the correct subject line and
filename.
- Always send yourself a copy of each e-mail message you send to me,
check immediately to see if you receive the message intact, and
check within a few minutes to see if you have received e-mail notifying
you about an undeliverable message. You are
responsible for sending e-mail correctly.
Here is the list of things you have to turn in:
- At the start of class on the
due date
place the following item on the "counter" in front of me:
- a hardcopy (printed listing) of your program (the C++
source code). Make sure all the code is properly
formatted and that it all shows on the paper.
- Before midnight on the the due
date send me the following by e-mail:
- A copy of the source code (C++ code) with subject line:
CS1500Solo6Source
- a (filtered) script, with subject line
CS1500Solo6Script
showing the results of doing all the tests described above in the
section entitled "TESTING"
Note that there are no spaces in the subject lines given. It is important
that you do not insert any spaces. My e-mail address is:
john@ishi.csustan.edu
WHEN IS THIS ASSIGNMENT DUE?
Look for the due date in
the class schedule.
(It's at the top level of the class directory.)