CS 010 - Introduction to Computer Science I
Lab 7. Functions



Points(10 overall)

Collaboration policy:

You will be working in pairs during labs. Pairs will be randomly selected and will be announced by the TA. Each week you will have a new random partner. You will discuss the programs with your partner, but you will still be typing in your own code to show to the TA. You can help each other debug, give plenty of suggestions and hints, **explain** why things work or don't work, etc.


Lab Objectives

To gain experience in


Exercise 1. Pre-packaged Functions

Write a program that allows the user to choose how many random circles to draw in a graphics window. Your program will then randomly choose the size of each radius (an integer ranging between 1 and 5 inclusive), and will randomly choose both the x-coordinate and y-coordinate for each center point. You will use the default coordinate system, thus both the x and y coordinate values will range between -10 to 10 inclusive.

Recall from Friday's lecture:


Exercise 2: Void Functions

Write a void function, called output, that takes two doubles (representing two values) and one integer (representing a precision) and prints them out nicely formatted, lined up at the decimal using the specified precision. Use your function and the following main to verify that your function works. You will need to add the statements that have the function call to main.


int main()
{
   double x = 3.15872;
   double y = 171.3;

   // ADD YOUR FUNCTION CALL HERE TO OUTPUT X AND Y
   // WITH A PRECISION OF 2

   cout << "Enter two numbers: ";
   cin >> x >> y;
  
   int precision;
   cout << "What precision would you like to use? ";
   cin >> precision;

   // ADD YOUR FUNCTION CALL HERE TO OUTPUT X AND Y
   // WITH THE USER ENTERED PRECISION

   return 0;
}

Exercise 3: Functions with Return Types

Write a function, called total, that takes a price, as a double, and a quantity, as an integer, and calculates and returns the total price, as a double. Your function should not have need to use any cins or couts.

Write a function, called change, that takes a price, as a double, and a payment, as a double, and calculates and returns the amount of change that should be received, as a double. Your function should not have any input or output.

Use the following main to verify your functions.

int main()
{
   const double TWINKIES = 0.60;
   const double DRPEPPER = 1.25;
   const double GUM = 0.40;

   cout << "1) Twinkies $0.60\n";
   cout << "2) Dr. Pepper $1.25\n";
   cout << "3) Gum $0.40\n";

   int choice;
   cout << "What would you like to buy?";
   cin >> choice;

   double price;

   if (choice == 1)
   {
      price = TWINKIES;
   }
   else if (choice == 2)
   {
      price = DRPEPPER;
   }
   else if (choice == 3)
   {
      price = GUM;
   }

   int quantity;
   cout << "How many would you like to buy?";
   cin >> quantity;

   double total_price = total(price, quantity);

   cout << fixed << setprecision(2);
   cout << "Your total price is $" << total_price << endl;

   double payment;
   cout << "Enter amount giving cashier: ";
   cin >> payment;

   cout << "Your change is $" << change(total_price, payment) << endl;

   return 0;
}