Function Worksheet III Answers

1.    1
      5
      3
      0
      1
      0


2.  #include <fstream.h>
    #include <limits.h>
    void maxmin(int& max, int& min)
    {
       ifstream numin;
       int value;
       numin.open("numbers.dat");
       max = INT_MIN;
       min = INT_MAX;
       numin >> value;
       while (numin)
       {
          if (value > max)
             max = value;
          if (value < min)
             min = value;
          numin >> value;
       }
     }


3.  void maxmin(int&,int&);


4.  maxmin(large,small);
    cout << "The largest number in the file is: " << large << endl;
    cout << "The smallest number in the file is: " << small << endl;


5.  #include <fstream.h>
    #include <limits.h>
    int maxmin(int& max, int& min)
    {
       ifstream numin;
       int value;
       numin.open("numbers.dat");
       //  if the open was not successful, return false
       if (!numin)
          return 0;
       //  otherwise, find max and min and return true
       max = INT_MIN;
       min = INT_MAX;
       numin >> value;
       while (numin)
       {
          if (value > max)
             max = value;
          if (value < min)
             min = value;
          numin >> value;
       }
       return 1;
     }


6.  int maxmin(int&,int&);


7.  ok = maxmin(large,small);
    if (ok == 1)   //  or could write "if (ok)"
    {
       cout << "The largest number in the file is: " << large << endl;
       cout << "The smallest number in the file is: " << small << endl;
    }
    else
       cout << "Error opening file numbers.dat\n";

Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2002