Homework 12B: Due Tuesday, November 7th

Read Chapter 6, section 6.1 and 6.2. Skip the sections on default arguments, function templates, and inline functions.

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);                   int two(int,int);
    int main()                       int main()
    {                                {
       int num;                         int a,b;
       num = 10;                        a = 3;
       one(num);                        b = 4;
       cout << num;                     cout << two(a,b);
    }                                }
    void one(int n)                  int two(int x, int y)
    {                                {
       n = 50;                          int a;
    }                                   a = x + y;
                                        return a;
                                     }


3.  #include <iostream.h>        4.  #include <iostream.h>
    int three(int,int);              void four(float);
    int main()                       int main()
    {                                {
       int f;                           float num = 3.5;
       f = 1;                           int i = 1;
       for (int i=1; i < 5; i++)        while (i < 5)
       {                                {
          f = three(i,f);                  cout << four(num) << endl;
          cout << f << endl;               i++;
       }                                }
    }                                }
    int three(int a, int b)          void four(float n)
    {                                {
       int z;                            n++;
       z = a + a * b;                    cout << n << endl;
       return z;                     }
    }


5.  Write a function called average which has two float parameters.
The function should calculate and return the average of the two
parameters.

6.  Write a prototype for the average function.

7.  Write the statements to call the function average and print the
value that was returned by the function.

8.  Write a function called equal which has two int parameters. The
function should return true if the two parameters are equal, and false
if the two parameters are not equal.

9. Write a prototype for the equal function.

10. Write the statements to read two integers, call the function equal,
and then print "true" if the numbers are equal or "false" if the
numbers are not equal.

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