Additional Scalar Types, Arrays, and File I/O

Tom Kelliher, CS17

May 3, 1996

Additional Scalar Types

Integer Types

  1. char.
  2. short int or short.
  3. int.
  4. long int or long.
  5. signed, unsigned.

Ranges, defaults for signed, unsigned?

Float Types

  1. float
  2. double
  3. long double

Ranges, precisions? ((6, 38), (19, 4,932)).

Arithmetic Anomalies

Arithmetic Conversions

Arrays

Definition:

An array is a collection of objects (variables), all of the same type. Individual members (called ``elements'') of the collection are accessed by number. This number is known as the ``index'' or ``subscript'' of the element. The first element of an array always has the index 0. If an array has n elements, then the last element will have index n - 1.

When would you use an array?

Declaring Arrays

The key is the [ and ]:

int a[10];   // Array of 10 integers.  None are assigned any initial
             // value.

char b[6] = { 'H', 'e', 'l', 'l', 'o', '\0' }  // Array of 6 chars,
            // initialized with the literal string "Hello".  Note the
            // null character.

char c[6] = "Hello";   // Array of 6 chars, also initialized to a
            // literal string.

double d[] = { 3.4e5, -12.3, 1.4e-9 };   // Array of 3 doubles,
             // initialized to given values.

Using Arrays

Example: Read positive integer values (at most 3) from the user and print them in reverse order. Use -1 as a sentinel value.

const int MAX = 3;
int data[MAX];
int count = 0;
int i;

cout << "? ";
cin >> data[count];

while (count < MAX && data[count] != -1)
{
   ++count;
   cout << "? ";
   cin >> data[count];
}

for (i = count - 1; i >= 0; --i)
   cout << data[i] << endl;

Importance of count.

Disk I/O

New include files and new variable types:

#include <fstream.h>

ifstream in;   // Input file variable type.
ofstream out;  // Output file variable type.

Difference between file name and file variable.

Important functions:

Example Program

/**********************************************************************
 * fileio.cc
 * Tom Kelliher
 *
 * This is a small program that demonstrates a number of things:
 *    o How to use an array as a function parameter.  Note that arrays
 *      are ALWAYS passed by reference.
 *    o How to use the getline() function with cin to read an entire
 *      line from cin.
 *    o How to use the open() and fail() functions with an ifstream
 *      variable (in).
 *    o How to read numbers from a disk file bound to an ifstream
 *      variable.
 *    o How to use eof() to test for reaching the end of a file.
 *    o How to use close() to close a file.
 **********************************************************************/


#include <fstream.h>
#include <iostream.h>


const int MAX = 100;   // Maximum number of data elements.
const int LINE = 80;   // Maximum length of a single keyboard input.


// Prototype.
int getInputs(int data[], int max);


/**********************************************************************
 * main()
 **********************************************************************/

int main()
{
   int d[MAX];   // The data.
   int count;    // How much data.
   int i;        // Index variable.

   count = getInputs(d, MAX);

   cout << "Read " << count << " items:\n";

   // Just print the data out.
   for (i = 0; i < count; ++i)
      cout << "Element " << i << ": " << d[i] << endl;

   return 0;
}


/**********************************************************************
 * getInputs() --- read integers from a file into an array.
 *
 * Preconditions: data is an int array with at least max elements.
 * Postconditions: After querying for the name of an input file and
 * opening the file, integers are read from the file into the array.
 * At most max elements are read.  The file is closed and the count of
 * numbers read is returned.
 **********************************************************************/

int getInputs(int data[], int max)
{
   int count = 0;   // How many numbers read?
   char name[LINE]; // Used to store the filename.
   ifstream in;     // Our file variable.

   // Get a filename and try to open the file.
   do
   {
      cout << "File name: ";
      cin.getline(name, LINE);

      in.open(name);
   } while (in.fail());

   // Read until we've filled the array or we reach end-of-file.
   while (count < max && !in.eof())
   {
      in >> data[count];
      ++count;
   }

   // If we reached eof, the count will be off by one.  Fix it.
   if (in.eof())
      --count;

   in.close();
   return count;
}

Exercise

Write a program to write the numbers from 1 to n (ask the user for the value to use for n) to a disk file.



Thomas P. Kelliher
Thu May 2 23:22:39 EDT 1996
Tom Kelliher