Chap 7 –Arrays and arrays as parameters

advertisement
Arrays and arrays as parameters. (chap 7)
No 2-D Arrays
Use my ARRAY notes.
If have time, can redo them
When you send an array to a function, you are sending a reference to the array. Thus the
function changes the array in main.
END OF DATA (sec 7-9)
A new way to determine end of data, called the end-of-file method (also called "reading
to input failure"—you must know both terms), has the computer test for the actual end of
the set of data. When the program tries to read past the last value in the file, the end-offile condition is set to true.
One advantage of the end-of-file method is that the computer, rather than the
programmer, handles the details of detecting the end of the set of data. No phony values
are needed, and the person entering the data does not have to know in advance how many
values there are. The method can be used when reading from an input stream or a file
stream, and with any data type.
When you read from a stream or a file stream, the stream variable gets a value indicating
the success or failure of the attempt to read. The attempt to read can fail either because
you have come to the end of the input or because the data value read in cannot be
appropriately converted to the type needed by the variable into which it is to be stored.
The value of the stream variable can be used in a while loop so that the program will fall
out of the loop at input failure. We will see or have seen examples.
Using the End-of-file Method with cin; Signaling End-of-File
When reading from cin, the user must enter a special value, called the end-of-file
character, to signal that there are no more data values. When the program reads this
special value, the program inteprets the value to mean that there are no more values to
read in.
The end-of-file character is entered by pressing <Ctrl>-z in Windows or DOS or
<Ctrl>-d in Unix. The user must enter the end-of-file character when the program
requests a data value. When the program reads this character, it stops reading data.
Note: The user does not need to do anything special to mark the end of an actual input
file, since the computer can determine by itself that it has reached the end. [Used to say
the character is appended when you save the file. Extra blank line at end can be a
problem.]
There is a file stream member function eof() that returns true when it reaches the end of a
stream and false if it is not at the end. Unfortunately, implementation of this function
varies by compiler.
So, no more structured read loop.
Instead of:
cin >> count[i];
while (!cin.eof()){
i++;
cin >> count[i];
}
We simply use:
while(cin>>count[i])
i++;
SEE ave_fun.cpp
SEE largest.cpp better yet, have them write it. LAB
Following stuff was added recently. SKIP it. Already checked if file is open.
Since the program can't continue if a file is not opened correctly, the programmer should
check whether the attempt to open a file has been succesful or not. The file stream
member function is_open() does this checking.
This function returns true if the file was successfully opened. If there was an
error in opening the file (the file name is incorrect, the file does not exist, or the output
file cannot be created), the function returns false. If is_open() returns false, the program
can print a message and terminate. Since is_open() is a file stream member function, its
name is preceded by the name of the stream we are trying to open, as shown in this
example which calls is_open() for file stream fin:
fin.is_open() . . .
Forgot what I read vs. fin.failure(). Which is problematic?
SKIP? Sending a File as a Parameter to a Function
A file stream or a standard I/O stream may be sent as a parameter to a function. It
must be sent as type istream (for input) or ostream (for output). Note: This is not
ifstream or ofstream. The parameter must be passed as a reference, even if the file is not
going to be changed in the function. By changing only the call, you can send a standard
I/O stream, instead of a file, to the function. If you are sending a file, it must be opened
before being sent to the function; types istream and ostream do not have open() methods
associated with them. Example 7-23 shows how to send both an input file and an output
file to a function.
EXAMPLE 7-23:
The function readdata() receives four parameters; the first two are stream
parameters, the last two are the array and the number of filled positions (the last two get
values in the function). The calling program can send either the standard input stream
(cin) or an input file as the first parameter; it can send either the standard output stream
(cout) or an output file as the second parameter.
Here is the prototype for readdata(); note that the I/O parameters are reference
parameters:
void readdata(istream &, ostream &, int [], int &);
Here is a call from the main program that sends two files as the first two
parameters. Note that the calling program must open and close any files sent as
parameters:
ifstream infile("in7.dat");
ofstream outfile("out7.out");
readdata(infile,outfile,mark,n);
infile.close();
outfile.close();
The name of the input (or output) file can be replaced by cin (or cout); in that
case, the function will read from standard input (cin) instead of from the input file, or
write to standard output (cout) instead of to the output file.
.
readdata(cin,cout,mark,n);
Here is the readdata() function:
// readdata reads values into the first n elements of array
num
// and outputs those values
// the first two parameters can be I/O streams or files
void readdata(istream &in, ostream &out, int num[], int &n)
{
n = 0;
in >> num[n];
while (in) {
out << num[n] << endl;
n++;
in >> num[n];
}
return;
}
Depending on the call, in will mean either infile or cin (or a different file, if one is sent)
and out will mean either outfile or cout (or a different file).
Download