Homework 14: Due Thursday, November 9th

Read Chapter 6, section 6.3 and 6.5. Complete the following problems:

Complete the following problems:

If there are no errors in the program, show what will be printed by
each of the following programs. If there are any errors in the program
explain what is wrong.


1.  #include <iostream.h>         2.  #include <iostream.h>
    void one(int);                    void one(int&);
    int main()                        int main()
    {                                 {
       int num;                          int num;
       num = 10;                         num = 10;
       one(num);                         one(num);
       cout << num;                      cout << num;
    }                                 }
    void one(int n)                   void one(int& n)
    {                                 {
       n = 50;                           n = 50;
    }                                 }


3.  #include <iostream.h>         4.  #include <iostream.h>
    void one(int,int&);               void one(int&,int);
    int main()                        int main()
    {                                 {
       int num1,num2;                    int num1,num2;
       num1 = 5;                         num1 = 15;
       num2 = 10;                        num2 = 8;
       one(num1,num2);                   one(num1,num2);
       cout << num1 << "  " << num2;     cout << num1 << "   " << num2;
    }                                 }
    void one(int n, int& m)           void one(int& n, int m)
    {                                 {
       int a;                            int a;
       a = n;                            a = n;
       m = m + a;                        m = m + a;
       n = m;                            n = m;
    }                                 }


5.  #include <iostream.h>
    int four(int&, int);
    int d;
    int main()
    {
       int a, b, c, e;
       a = 1;
       b = 2;
       c = 3;
       d = 4;
       e = 5;
       cout << a << "  " << b << "  " << c;
       cout << "  " << d << "  " << e << endl;
       e = four(a, b);
       cout << a << "  " << b << "  " << c;
       cout << "  " << d << "  " << e << endl;
    }
    int four(int& a, int b)
    {
       int c;
       a = 7;
       b = 8;
       c = 9;
       cout << a << "  " << b << "  " << c;
       cout << "  " << d << endl;
       return c;
    }

6.  The statement
            power(k,j,m);
    is a call to the void function shown below. Rewrite the function
    as a value returning function, and write a function call that
    assigns the return value from your function to the variable m.

    void power(float base, int exp, float& answer)
    {
       int i = 1;
       answer = 1.0;
       while (i <= exp)
       {
          answer *= base;
          i++;
       }
    }


7.  Write a function called insuranceCost that receives the age of the
    driver and the number of license points of the driver as parameters,
    and returns the base cost and surcharge for this driver. The
    function will calculate the insurance cost as follows:

            age            base cost
            17-20            1200
            21-29            900
            30 or over       750

   The surcharge for points is as follows:
            points            surcharge
               0                    0
              1-5                 300
              6-9                 700
              10 or more         1250


8.  Write  a prototype for your insuranceCost function.


9.  Write the statements to read the age and number of points, call
    the function, and print the base cost and surcharge.

Remember to type your answers and to make two copies of each homework assignment. One copy is submitted to me and the other you keep to use in the group discussions.


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

© Copyright Emmi Schatz 2004