\documentstyle[html]{article}
\setlength{\textwidth}{6.6truein}
\setlength{\oddsidemargin}{-.2truein}
\setlength{\evensidemargin}{-.2truein}
\setlength{\textheight}{9truein}
\setlength{\topmargin}{-.4truein}
\setlength{\headsep}{.2truein}
\setlength{\footskip}{.3truein}
\begin{document}

\begin{center}
  {\Large \bf Modular Programming} \\
  \strut \\
  {Group:\ \hrulefill
   \ \ \ \ Captain:\ \hrulefill\hrulefill\hrulefill
   \ \ \ \ Recorder:\ \hrulefill\hrulefill\hrulefill
   \ \ \ \ Reflector:\ \hrulefill\hrulefill\hrulefill}
\end{center}

\begin{description}

\item[Motivation] \strut\hfill \\

  While the program is growing, it also becomes more difficult to
  read, understand, and debug.  By breaking the main program apart
  into pieces, functions, you can improve clarity and debugging.

  It also allows you to {\it reuse} code, in the same program or in
  several programs. Taking a working subroutine and applying it to as
  many places as possible is an effective way to deal with larger
  programming tasks.

  Understanding functions is essential to make you a better
  programmer, hence a more desirable engineer.

\item[Objectives] \strut

  \begin{enumerate}
  \item Learn about functions.
  \item Learn about character variables.
  \item Learn about projects.
  \item Improve program neatness.
  \item Strengthen previous knowledge.
  \end{enumerate}

\item[Performance Criteria]  \strut

  \begin{enumerate}

  \item Success in modifying your program so that it only asks {\em once}
    for the the costs of brass and steel, however many parts the user
    computes.  Do that by initializing these variables to zero. Then
    do not ask for the steel cost unless it is still zero, and the
    value is needed for the current part.  The same for the cost of
    brass.

   \item Success in taking part of the program out of
    \verb^function.cpp^ and into a function \verb^...^ \verb^menu^
    \verb^(...)^ in a new file \verb^menu.cpp^. The part that you
    should take out is writing the menu to the screen and getting the
    menu letter from the user

    Function \verb^menu^ should \verb^return^ the letter selected
    by the user to the \verb^main^ function. \verb^Main^ will still do
    the processing appropriate to that choice, (input a material cost,
    compute a part, quit, or print an error message.) (25\%)

    {\em Hint:} You will need to declare storage locations for the
    letter, say \verb^menu_choice^ both inside \verb^main^ and inside
    \verb^menu^.

    {\em Hint:} Function \verb^menu^ must return a character to
    the \verb^main^ function.  What does that mean for the
    declaration/prototype of function \verb^menu^?

    {\em Hint:} Function \verb^menu^ does not {\em receive} any
    information from the \verb^main^ function. What does this mean for
    the declaration/prototype of function \verb^menu^?

    {\em Hint:} Unless you can answer the first three hints, do not
    even try to do any programming. See lesson 9 on functions if you
    cannot answer them.

    {\em Hint:} \verb^menu^ will be called in the \verb^main^ function
    using an assignment statement.

  \item Success in splitting up your \verb^main^ function further as
    follows: Take the part of the main program that prompts the user
    for a part's steel and brass weights and the cost of these
    materials, and that then computes and prints the part's total
    material cost.  Put all of this into a function \verb^part_process^.
    Add it to the project file list. (25\%)

    {\em Hint:} Function \verb^part_process^ does not need to return
    anything to the main program, since it should already print out
    the part's material cost itself.

    {\em Hint:} Function \verb^part_process^ will of course need the
    costs of brass and steel.  Figure out the right way to get
    these from \verb^main^ into \verb^part_process^ (Lessons 10 and 14).

    {\em Hint:} Do at least two parts and check that you do not have
    to reenter the costs of steel and brass the second time.

  \item Success in E-mailing the three C++ files to the instructor
    as a single mailing.

  \item Success in producing a clearer, better commented program that
    is more easy to read and understand by a stranger than the
    programs of the instructor and any other group in the class.
    Success in producing a neater, better looking program than the
    instructor or any other group in the class. Success in combining
    files into projects and E-mailing final source codes as ASCII
    text to the instructor. (20\%)

  \item The knowledge demonstrated by your definitions of the
    vocabulary.  (10\%)

  \item The depth of understanding demonstrated by your answers to the
    critical thinking questions. (20\%)

  \end{enumerate}

\item[Resources]  \strut

  \begin{enumerate}
  \item Your class notes.
  \item The course web page.
  \item The textbook.
  \item The online help facilities of Windows and the C++ compiler.
  \end{enumerate}

\item[Plan]  \strut

  \begin{enumerate}
  \item Skim through the lessons ``Getting Started With Functions'',
    (``Changing Parameter Values''), and ``Using C++ References'' of
    the book.  Check the activity notes on the web page for what you
    need to know.
  \item Do the programming tasks that are performance criteria.
  \item Define the vocabulary.
  \item Answer the critical thinking questions.
  \end{enumerate}

\item[Vocabulary]  \strut

  Select the best answer:

  \begin{enumerate}

  \item Function.
    \begin{enumerate}
    \item The normal and specific contribution of a bodily part to the
      economy of a living organism.
    \item The purpose of a group of source statements.
    \item Some source statements grouped together.
    \end{enumerate}

  \item Parameter.
    \begin{enumerate}
    \item A number shared between the called function and the
      calling function
    \item A number or memory location shared between the called
      function and the calling function
    \item A memory location shared between the called function and
      the calling function
    \end{enumerate}

  \item Return value.
    \begin{enumerate}
    \item A number or memory location that is always given to the
      called function when this function is called.
    \item A number or memory location that is always given back to the
      calling function by the called function when it has finished its
      job.
    \item The amount of cash Wallmart gives you back when you return
      unused paint to the store.
    \end{enumerate}

  \item Return type.
    \begin{enumerate}
    \item The act of pressing a plus-size key on a computer keyboard.
    \item The variable returned by a called function to the caller
      when the called function is done.
    \item The sort of variable that comes out of a called function.
    \end{enumerate}

  \item Scope.
    \begin{enumerate}
    \item Means: in which functions a variable can be used.
    \item The mouthwash of choice of C++ programmers.
    \item The locations or functions in which a variable has been
      declared using a type-declaration of local or global validity
      and accessibility.
    \end{enumerate}

  \item Pass-by-value.
    \begin{enumerate}
    \item Giving the value of a variable to a called function.
    \item Giving the storage location of a variable to a called function.
    \item Giving students a passing grade based on their lab and test
      performance.
    \end{enumerate}

  \item Pass-by-reference.
    \begin{enumerate}
    \item Giving the value of a variable to a called function.
    \item Giving the storage location of a variable to a called function.
    \item Giving students a passing grade based on their letters of
      recommendation.
    \end{enumerate}

  \item Function prototype.
    \begin{enumerate}
    \item A first draft of a function's source code, before being
      debugged.
    \item An absolutely useless line or few lines in a file that
      indicate roughly how some function looks.  Only necessary since
      the compiler is too stupid to check other open windows and files
      in the directory for the function.
    \item A valuable part of program that defines a function header,
      arguments, and source code statements.  It allows the function
      to be compiled correctly.
    \end{enumerate}

  \end{enumerate}

\item[Critical Thinking Questions]  \strut

  Select the best answer:

  \begin{enumerate}

  \item Without using global variables, what is the proper way to
    get information {\em into} a function from the caller?
    \begin{enumerate}
    \item Using the return type.
    \item Using the return value.
    \item Using a parameter.
    \end{enumerate}

  \item Without using global variables, what are the proper two ways
    to get information {\em out of} a function to the caller?
    \begin{enumerate}
    \item Using the return type or return value.
    \item Using a parameter type or return type.
    \item Using a parameter value or return value.
    \end{enumerate}

  \item What is produced by the following program?
\begin{verbatim}
    #include <iostream.h>
    void three(void) { cout << "3 "; }
    void two(void) { cout << "2 "; three(); }
    void one(void) { cout << "1 "; two(); }
    void main(void) { cout << "Counting "; one(); }
\end{verbatim}
    \begin{enumerate}
    \item The program will produce a compilation error.
    \item \verb^3 2 1 Counting ^
    \item \verb^Counting 1 2 3 ^
    \end{enumerate}

  \item What is produced by the following program?
\begin{verbatim}
    #include <iostream.h>
    void Jack(void);
    void John(void) { cout << "John "; Jack(); }
    void Jane(void) { cout << "Jane "; }
    void Jill(void) { cout << "Jill "; John(); }
    void Jack(void) { cout << "Jack "; Jane(); }
    void main(void) { cout << "Hi "; Jill(); cout << "!";}
\end{verbatim}
    \begin{enumerate}
    \item \verb^John Jack Jane Jill John Jack Jane Hi Jill!^
    \item \verb^John Jane Jill Jack Hi !^
    \item \verb^Hi Jack Jill Jane John !^
    \item \verb^Hi Jill John Jack Jane !^
    \end{enumerate}

  \item Why is the Jack definition done twice in the above program,
    and John, Jane, and Jill are not?
    \begin{enumerate}
    \item The instructor is an idiot.
    \item This is necessary since \verb^Jack^ is the last function
      before the \verb^main^ function.
    \item Are you an idiot? \verb^Jack^ is not defined twice!
    \end{enumerate}

  \item What happens if you call the following function:
\begin{verbatim}
    void hi(void) {
      cout << "Hello\n" ;
      hi();
      }
\end{verbatim}
    \begin{enumerate}
    \item It comes running.
    \item It will produce a compilation error since recursion of
      functions is not allowed.
    \item It will keep writing \verb^Hello^ lines to the screen.
    \end{enumerate}

  \item What is produced by:
\begin{verbatim}
    #include <iostream.h>
    void set(void) {
      int a;
      a=1;
      }
    void main(void) {
      int a;
      a=0;
      set();
      cout << a;
      }
\end{verbatim}
    \begin{enumerate}
    \item It will produce a compilation error since we did not put
      \verb^void^ between the parentheses when \verb^set^ was called
      inside \verb^main^.
    \item 0
    \item 1
    \end{enumerate}

  \item What is produced by:
\begin{verbatim}
    #include <iostream.h>
    int a;
    void set(void) {
      a=1;
      }
    void main(void) {
      a=0;
      set();
      cout << a;
      }
\end{verbatim}
    \begin{enumerate}
    \item It will produce a compilation error since declarations must
      be immediately behind the opening curly brackets of functions.
    \item 0
    \item 1
    \end{enumerate}

  \item
    Is the following code legal in C++? What does it produce?
\begin{verbatim}
    #include <iostream.h>
    void out(int houses, int horses) {
     cout << houses << " " << horses;
      }
    void main(void) {
      int boats=10, sheep=2;
      out(boats, sheep);
      }
\end{verbatim}
    \begin{enumerate}
    \item No, this is a syntax error.
    \item No, you must put \verb^void^ before \verb^out^ in \verb^main^.
    \item No, you must put \verb^int ^ before \verb^boats^ and
      \verb^sheep^ in the call to \verb^out^ in \verb^main^.
    \item Yes, but it is lousy coding. It produces \verb^houses horses^
    \item Yes, but it is lousy coding. It produces \verb^boats sheep^
    \item Yes, but it is lousy coding. It produces \verb^10 2^
    \end{enumerate}

  \item What is produced by the following program?
\begin{verbatim}
    #include <iostream.h>
    void show(float a) {
      cout << a << endl;
      }
    void main(void) {
      float a=1, b=2;
      show(b);
      }
\end{verbatim}
    \begin{enumerate}
    \item This will produce a compilation error. The argument must be
      \verb^a^, not \verb^b^
    \item 1
    \item 2
    \end{enumerate}

  \item What is produced by:
\begin{verbatim}
    #include <iostream.h>
    void out(int a, int b) {
      cout <<  a << " " << b << endl;
      }
    void main(void) {
      int a=1, b=2;
      out(b,a);
      }
\end{verbatim}
    \begin{enumerate}
    \item This will produce a compilation error.  The arguments are
      reversed.
    \item \verb^1 2^
    \item \verb^2 1^
    \end{enumerate}

  \item What is produced by (may not work with MS C++):
\begin{verbatim}
    #include <iostream.h>
    int out(int a, int b) {
      cout << a << " " << b << " ";
      return a+b;
      }
    void main(void) {
      cout << "(" << out(1,2) << ")" << endl;
      }
\end{verbatim}
    \begin{enumerate}
    \item This will produce a compilation error. You cannot use a
      function in a \verb^cout^ statement.
    \item \verb^(1 2)^
    \item \verb^(3)^
    \item \verb^(1 2 3)^
    \item \verb^(1 2 ) 3^
    \item \verb^(a b ) 3^
    \end{enumerate}

  \item What is produced by:
\begin{verbatim}
    #include <iostream.h>
    int fun(int i) {
      if (i==1) return 1;
      else return i*fun(i-1);
      }
    void main(void) {
      cout << fun(1) << " " << fun(2) << " " << fun(3) << " "
           << fun(4) << " " << fun(5) << endl;
      }
\end{verbatim}
    \begin{enumerate}
    \item \verb^0 0 0 0 0^
    \item \verb^1 2 3 4 5^
    \item \verb^1 2 6 8 10^
    \item \verb^1 2 6 24 120^
    \end{enumerate}

  \item What does the following code produce:
\begin{verbatim}
    #include <iostream.h>
    int ratio(int a, int b) {
      return a/b;
      }
    void main(void) {
      int a=1, b=2;
      cout << ratio(a,b) << " " << ratio(b,a);
      }
\end{verbatim}
    \begin{enumerate}
    \item \verb^ratio(a,b) ratio(b,a)^
    \item \verb^a/b a/b^
    \item \verb^0 0^
    \item \verb^0.5 0.5^
    \item \verb^0.5 2^
    \item \verb^0 2^
    \item \verb^2 2^
    \end{enumerate}


  \item What happens for:
\begin{verbatim}
    #include <iostream.h>
    void out(int i) {
      cout << i << endl;
      }
    void main(void) {
      out(1.5);
      }
\end{verbatim}
    \begin{enumerate}
    \item This will produce a link error. The argument must be an
      integer.
    \item It will print 1.5.
    \item The computer will stream the I/O to an \verb^h^.
    \end{enumerate}

  \item What is produced by:
\begin{verbatim}
    #include <iostream.h>
    void swap(int a, int b) {
      int tmp;
      tmp=a;
      a=b;
      b=tmp;
      }
    void main(void) {
      int a=1, b=2;
      cout << a << " " << b << " --- ";
      swap(a,b);
      cout << a << " " << b << endl;
      }
\end{verbatim}
    \begin{enumerate}
    \item \verb^1 2 --- 1 2^
    \item \verb^1 2 --- 2 1^
    \item \verb^1 2 --- b tmp^
    \end{enumerate}

  \item What is produced by:
\begin{verbatim}
    #include <iostream.h>
    void swap(int &a, int &b) {
      int tmp;
      tmp=a;
      a=b;
      b=tmp;
      }
    void main(void) {
      int a=1, b=2;
      cout << a << " " << b << " --- ";
      swap(a,b);
      cout << a << " " << b << endl;
      }
\end{verbatim}
    \begin{enumerate}
    \item This will produce a compilation error. There is an \verb^&^
      before the \verb^a^ and \verb^b^.
    \item This will produce a compilation error. \verb^a^ and \verb^b^
      must be reference variables in \verb^main^.
    \item This will produce a compilation error. \verb^a^ and \verb^b^
      must be aliases in \verb^main^.
    \item \verb^1 2 --- 1 2^
    \item \verb^1 2 --- 2 1^
    \item \verb^1 2 --- b tmp^
    \end{enumerate}

  \end{enumerate}

\item[Exercises]  \strut
  \begin{enumerate}
  \item Create \verb^void error_msg(char *msg)^ function so that,
    for example, \verb^error_msg("This is a test")^ or
    \verb^error_msg("You made a mistake!")^ will print out these
    messages along with a beep and a new line.  Use your function
    to print out the ``You entered an invalid choice'' message
    in the menu.
  \item Put inputting the costs of brass and steel into functions
    named \verb^get_brass_cost^ and \verb^get_steel_cost^. These
    should take no parameters.
  \item Create a function \verb^input_positive_float^ which inputs a
    float from the user, but does not accept a negative of zero value.
    Use this function to obtain the costs of brass and steel
    from the user.
  \item Create a similar function \verb^input_nonnegative_float^ to
    input the weights of steel and brass of the part.
  \item Print the date at the start of your program.
  \item Write an overloaded \verb^error_msg^ function that can either
    print just a character string, or a character string followed by a
    floating point number. Use it for the error messages when the
    user inputs an invalid number.
  \end{enumerate}

\item[Evaluation]  \strut

  \begin{enumerate}
  \item Give two strengths of your group.
  \item Indicate two areas for improvement.
  \item What two new insights did your group obtain?
  \item Give two strengths of the class so far.
  \item Indicate two areas for improvement of the class.
  \item What two new insights did you obtain about the class?
  \end{enumerate}

\end{description}
\end{document}
