CSCI 240 - Function Worksheet 2 Answers

Function 1

Write a function called CalcAverage. This function takes as its arguments two integers. The first is a total from a number of items and the second represents the number of items. The function should calculate and return the average. Before the function returns, print a sentence stating what the average is.

Function prototype:

double CalcAverage(int, int);

Function:

double CalcAverage(int total, int count)
   {
   double average;

   average = (double) total / count;

   cout << "\nThe average is " << average;

   return average;
   }

Calling Statement:
Pass ItemTotal and NumberOfItems to the function and store the return value in a variable named avg. You can assume that ItemTotal and NumberOfItems have received valid values prior to this call.

avg = CalcAverage(ItemTotal, NumberOfItems);

 

Function 2

Write a function called FindSum. It takes no arguments. The function should prompt the user for integer values and add them up until a negative value is entered. The sum should then be returned.

Function prototype:

int FindSum();

Function:

int FindSum()
   {
   int sum = 0, user_value;

   cout << "Enter an integer (negative to quit): ";
   cin >> user_value;

   while (user_value > 0)
      {
      sum += user_value;
      cout << "Enter another integer (negative to quit): ";
      cin >> user_value;
      }
   return sum;
   }

Calling Statement:

Call the function and assign the return value to a variable called UserSum.

UserSum = FindSum();

 

Function 3

Write a function called PrintPageHeader. This function takes as its argument an integer which represents the page number of the page to be printed. The function should print "240 Distributors Employee Report" and the page number centered on the top of a new page.

Function prototype:

void PrintPageHeader(int);

Function:

void PrintPageHeader(int page_num)
  {
  cout << "\f\t\t\t\t 240 Distributors Employee Report \t\t\t Page: " << pagenum;
  }

Calling Statement:

Pass the function an integer variable called page. Assume that page has been given a valid value prior to this call.

PrintPageHeader(page);

 

Function 4

Write a function called UserChoice. It takes as its argument an integer that represents an upper bound. The function is to prompt the user to enter an integer between 1 and the upper bound. If the user enters a number less than 1, return -1. If the user enters a number greater than the upper bound, return 0. If the user enters a number between 1 and the upper bound, inclusive, return the user's number.

Function prototype:

int UserChoice(int);

Function:

int UserChoice(int maximum)
   {
   int user_value;

   cout << "Enter a value between 1 and " << maximum;
   cin >> user_value;

   if (user_value < 1)
      return -1;
   else if (user_value > maximum)
      return 0;
   else
      return user_value;
   }

Calling Statement:

Pass the function the value 25. Assign the return value to a variable called choice.

choice = UserChoice(25);

 

Function 5

Write a function called IsOdd. It takes as its argument an integer that is to be tested to see if it's odd. If the number is odd, return 1. If the number is even, return 0.

Function prototype:

int IsOdd(int);

Function:

int IsOdd(int x)
   {
   if ((x % 2) == 0)
      return 0;
   else
      return 1;
   }

Calling Statement:

Pass the function the variable testnum and assign the return value to oddFlag. Assume that testnum has been given a valid value prior to this call.

oddFlag = IsOdd(testnum);

 

Function 6

Write a function called FigureTax. It takes as its argument a real number that represents the subtotal for a customer's purchase. The function should apply a 6.25% tax to the subtotal and return a grand total, which is equal to the subtotal plus the tax amount.

Function prototype:

double FigureTax(double);

Function:

double FigureTax(double subTotal)
   {
   double tax_amount;

   tax_amount = subTotal * 0.0625;

   return (subTotal + tax_amount);
   }

Calling Statement:

Pass the function the value 79.85 and assign the return value to GrandTotal.

GrandTotal = FigureTax(79.85);