Chapter 6

Program 6-14

posted Mar 26, 2013, 1:21 AM by Karwan Jacksi   [ updated Mar 26, 2013, 1:21 AM ]

 // This is a modular, menu-driven program that computes
 // health club membership fees.
 #include <iostream>
 #include <iomanip>
 #include <string>
 using namespace std;

 // Function prototypes
 void displayMenu();
 int getChoice();
 void showFees(string category, double rate, int months);

 int main()
 {
 // Constants for monthly membership rates
 const double ADULT_RATE = 40.00,
 SENIOR_RATE = 30.00,
 CHILD_RATE = 20.00;
 int choice, // Holds the user's menu choice
 months; // Number of months being paid

 // Set numeric output formatting
 cout << fixed << showpoint << setprecision(2);

 do
 { displayMenu();
 choice = getChoice(); // Assign choice the value returned
 // by the getChoice function
 if (choice != 4) // If user does not want to quit, proceed
 {
 cout << "For how many months? ";
 cin >> months;

 switch (choice)
 {
 case 1: showFees("Adult", ADULT_RATE, months);
 break;
 case 2: showFees("Child", CHILD_RATE, months);
 break;
 case 3: showFees("Senior", SENIOR_RATE, months);
 }
 }
 } while (choice != 4);
 return 0;
 }

 /**********************************************
 * displayMenu *
 * This function clears the screen and then *
 * displays the menu choices. *
 **********************************************/
 void displayMenu()
 {
 system("cls"); // Clear the screen.
 cout << "\n Health Club Membership Menu\n\n";
 cout << "1. Standard Adult Membership\n";
 cout << "2. Child Membership\n";
 cout << "3. Senior Citizen Membership\n";
 cout << "4. Quit the Program\n\n";
 }

 /**************************************************
 * getChoice *
 * This function inputs, validates, and returns *
 * the user's menu choice. *
 **************************************************/
 int getChoice()
 {
 int choice;

 cin >> choice;
 while (choice < 1 || choice > 4)
 { cout << "The only valid choices are 1-4. Please re-enter. ";
 cin >> choice;
 }
 return choice;
 }

 /**************************************************************
 * showFees *
 * This function uses the membership type, monthly rate, and *
 * number of months passed to it as arguments to compute and *
 * display a member's total charges. It then holds the screen *
 * until the user presses the ENTER key. This is necessary *
 * because after returning from this function the displayMenu *
 * function will be called, and it will clear the screen. *
 **************************************************************/
 void showFees(string memberType, double rate, int months)
 {
 cout << endl
 << "Membership Type : " << memberType << " "
 << "Number of months: " << months << endl
 << "Total charges : $" << (rate * months) << endl;

 // Hold the screen until the user presses the ENTER key.
 cout << "\nPress the Enter key to return to the menu. ";
 cin.get(); // Clear the previous \n out of the input buffer
 cin.get(); // Wait for the user to press ENTER
 }



Program Output with Example Input Shown in Bold
Health Club Membership Menu
1. Standard Adult Membership
2. Child Membership
3. Senior Citizen Membership
4. Quit the Program
1[Enter]
For how many months? 3[Enter]
Membership Type : Adult Number of months: 3
Total charges : $120
Press the Enter key to return to the menu.

Program 1

posted Mar 25, 2013, 10:23 PM by Salah Ibrahim   [ updated Mar 26, 2013, 12:12 AM by Karwan Jacksi ]

//Write two c++ functions  one for factorial and another for power,
//then use these two functions to solve this equation : R=1!/n^1 +  3!/n^3 + 5!/n^5 …m!/n^m




#include<iostream>
using namespace std;

//Function power
float pow(int n, int m)            //n,m: are function parameters
{
  float p = 1;                         //p is storage and initialized by 1 because we have multiplication. 
  for(int i = 1; i <= m; i++)   //our loop from 1 -> m. 
     p *= n;                          //multiply n by p and put the result in p.
  return p;                             //return the value of p which is (n to power of m).
}  

//Function factorial               
float fact(int n)                       //n is function parameter
{
  float f = 1;                         //f is storage and initialized by 1 because we have multiplication. 
  for(int i = 1; i <= n; i++)   //our loop from 1 -> n.  
     f *= i;                           //multiply i by f and put the result in f.
  return f;                             //return the value of f which is (factorial of n).
}

void main()        
{
  float sum=0, num1, num2;           //sum is storage and initialized by 0 because we have addition.
  cin >> num1 >> num2;                    //reading num1 then num2 variables.
  for(int i = 1; i <= num1; i+=2)        //our loop from 1 -> num1, its increased by 2 because we only want odd numbers in loop.  
     sum +=  //dividing factorial on power and store result in sum. 
     (fact(i) /                                     //calling function fact(i), i: is function argument. 
     pow(num2, i));                          //calling function pow(num2, i), num2,i: are function arguments.       
  cout << sum << endl;                    //printing sum (the result).
  system("pause");
}

1-2 of 2