Function Lab 2

 

Type in Word. Work with a partner and submit ONE COPY per pair. Include the following at the top of your submission:

 

Partner 1:       Name ______________________________________________

role (driver or navigator) _____________________________

 

Partner 2:       Name ______________________________________________

role (driver or navigator)______________________________

 

Please note if one partner did not work on all problems:

 

---------------------------------------------------------------------------------------------------

 

//**************************************************

// NewWelcome program

// This program prints a "Welcome Home" message

//**************************************************

#include <iostream>

using namespace std;

void PrintLines( int );                     // Function prototype

int main()

{

    PrintLines(2);

    cout << " Welcome Home!" << endl;

    return 0;

}

//****************************************************************

// PrintLines prints lines of asterisks, where

// numLines specifies how many lines to print

//***************************************************************

void PrintLines( int numLines )

{

    int line;      // Loop control variable

 

    for (line = 0; line < numLines; line++)

    {

        cout << "*************************************" << endl;

    }

}

 

  1. Type in the above program as lines.cpp. Add a comment to include your name and date. Compile and run. Change the program so that it will print 2 lines of asterisks before the welcome message and 4 lines of asterisks after the welcome message. Compile and run. Turn in a copy of the program and output
  2. Is PrintLines an example of a void or value returning function?
  3. Is numLines being passed in or passed out?
  4. On the program label the formal argument(s) and the actual argument(s).
  5. Draw a hierarchy chart for the program. 

 

 

#include <iostream>

using namespace std;

int Square(int x);

void CalcCube(int x, int& cube);

 

int main()

{

      int square,number, cube;

     

      cout << "Enter number, 0 to quit" << endl;

      cin >> number;

      while(number != 0)

      {

            square = Square(number);//this is a function

            CalcCube(number, cube);  //this is a procedure

            cout << number << " squared is " << square

                           << " cubed is " << cube << endl;

            cout << "Enter number, 0 to quit" << endl;

            cin >> number;

      }

      return 0;

}

//**************************************

// this value returning function returns the square of x

//**************************************

int Square(int x)

{

    int result;

   

    result = x * x;

    return (result);

   

}//**************************************

// this void function calculates the cube of x

//**************************************

void CalcCube(int x, int& cube)    

{

   

    cube = x * x * x;

    

}

  1. Type in the above program. Add your name and date as a comment. Run the program. Submit the program and a few sample outputs. Draw a hierarchy chart.
  2. Fill in the following table:

 

 

value-returning function

void function

Name of function

 

 

Type in c++(int, float, void)

 

 

Contains return statement(y/n)

 

 

Contains arguments(y/n)

 

 

Other differences

 

 

 

 

 

 

 

 

 

#include <iostream>

using namespace std;

void Swap(int x,int y); //prototype

int main()

{

            int num1, num2;

           

            cout << "Please enter two numbers to swap, the same numbers will quit." << endl;

            cin >> num1 >> num2;

            while (num1 != num2)

            {

                        Swap(num1,num2);

                        cout << "After swap, the numbers are "

                               << num1 << "  " << num2 << endl;

                        cout << "Please enter two numbers to swap, ";

                        cout << "the same numbers will quit." << endl;

                        cin >> num1 >> num2;

            }

            cout << "Thanks!" << endl;

            return 0;

}

//***************************

//This subroutine swaps two numbers

//*****************************

void Swap(int x, int y)

{

            int temp;

            temp = x;

            x = y;

            y = temp;

}

1.      Type in the above program as swap.cpp. Add a comment to include your name and date. Compile and run with different values.

2.      To find out why this is not working, insert the following output statements into the swap function as appropriate.

a.       cout << “ beginning of swap” << x << “ “ << y << endl;

b.      cout << “ end of swap” << x << “ “ << y << endl;

Are the variables being swapped inside the swap function?

3.      Explain why this program is not working correctly.

4.      Fix the program so the swap will work. Compile and run and be sure the swap works.

5.      Submit a copy of the final listing and output. Also include a copy of the hierarchy chart for the program.