Function Worksheet III

1.  Show what will be printed by the following program.

    #include <iostream.h>
    int g1, g2;
    void varval(int, int&);
    int showscope(int);
    int main()
    {
       g1 = 1;
       g2 = 2;
       varval(g1,g2);
       cout << g1 << endl;
       cout << g2 << endl;
       g2 = showscope(g1);
       cout << g1 << endl;
       cout << g2 << endl;
       return 0;
    }
    void varval(int pm1, int&pm2)
    {
       int pr1, pr2;
       pr1 = 1;
       pr2 = 2;
       pm1 = pm1 + pr1 + pr2;
       pm2 = pm2 + pr1 + pr2;
    }
    int showscope(int pm1)
    {
       int g1, fn;
       g1 = 0;
       fn = 2;
       pm1 = pm1 + fn;
       cout << pm1 << endl;
       cout << g1 << endl;
       return g1;
    }


2.  Write a function that reads a file "numbers.dat". This file contains
    a list of integers. The function should return the largest number and
    the smallest number in the file.


3.  Write a prototype for your function.


4.  Write the statements to call your function and print the largest and
    smallest numbers.


5.  Write another version of the function from problem 2. In this version,
    the function should check whether the input file is opened successfully
    before reading from the file. If the file is not opened successfully,
    the function should return false. If the file is opened successfully,
    the function should return the largest and smallest numbers as before,
    and return true. The parameter list for the function should be the
    same as the original version. To return true or false, use a return
    value for the function.


6.  Write a prototype for this new version of the function.


7.  Write the statements to call the new version of the function. If the
    input file could not be opened, print an error message. Otherwise,
    print the largest and smallest numbers.

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

© Copyright Emmi Schatz 2002