Function - Void

//DISPLAY 5.2 void Functions
//Program to convert a Fahrenheit temperature to a Celsius temperature.
#include <iostream>
 
void initialize_screen( );
//Separates current output from
//the output of the previously run program.
 
double celsius(double fahrenheit);
//Converts a Fahrenheit temperature
//to a Celsius temperature.
 
void show_results(double f_degrees, double c_degrees);
//Displays output. Assumes that c_degrees 
//Celsius is equivalent to f_degrees Fahrenheit.
 
int main( )
{
    using namespace std;
    double f_temperature, c_temperature;
 
    initialize_screen( );
    cout << "I will convert a Fahrenheit temperature"
         << " to Celsius.\n"
         << "Enter a temperature in Fahrenheit: ";
    cin >> f_temperature;
 
    c_temperature = celsius(f_temperature);
 
    show_results(f_temperature, c_temperature);
    return 0;
}
 
//Definition uses iostream:
void initialize_screen( )
{
    using namespace std;
    cout << endl;
    return;
}
double celsius(double fahrenheit)
{
    return ((5.0/9.0)*(fahrenheit - 32));
}
//Definition uses iostream:
void show_results(double f_degrees, double c_degrees)
{
    using namespace std;
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(1);
    cout << f_degrees 
         << " degrees Fahrenheit is equivalent to\n"
         << c_degrees << " degrees Celsius.\n";
    return; // Optional
}

Function - super market

//DISPLAY 5.11 Program with a Stub
//Determines the retail price of an item according to
//the pricing policies of the Quick-Shop supermarket chain.
#include <iostream>

void introduction( );
//Postcondition: Description of program is written on the screen.

void get_input(double& cost, int& turnover);
//Precondition: User is ready to enter values correctly. 
//Postcondition: The value of cost has been set to the 
//wholesale cost of one item. The value of turnover has been 
//set to the expected number of days until the item is sold.

double price(double cost, int turnover);
//Precondition: cost is the wholesale cost of one item.
//turnover is the expected number of days until sale of the item.
//Returns the retail price of the item.

void give_output(double cost, int turnover, double price);
//Precondition: cost is the wholesale cost of one item; turnover is the
//expected time until sale of the item; price is the retail price of the item.
//Postcondition: The values of cost, turnover, and price have been
//written to the screen.

int main( )
{
    double wholesale_cost, retail_price;
    int shelf_time;

    introduction( );
    get_input(wholesale_cost, shelf_time);
    retail_price = price(wholesale_cost,shelf_time);
    give_output(wholesale_cost, shelf_time, retail_price);
    
    return 0; 
}

//Uses iostream:
void introduction( )
{
    using namespace std;
    cout << "This program determines the retail price for\n"
         << "an item at a Quick-Shop supermarket store.\n";
}
//Uses iostream:
void get_input(double& cost, int& turnover)
{
    using namespace std;
    cout << "Enter the wholesale cost of item: $";
    cin >> cost;
    cout << "Enter the expected number of days until sold: ";
    cin >> turnover;
}

//Uses iostream:
void give_output(double cost, int turnover, double price)
{
    using namespace std;
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
    cout << "Wholesale cost = $" << cost << endl
         << "Expected time until sold = " 
         << turnover << " days" << endl
         << "Retail price= $" << price << endl;
}

//This is only a stub:
double price(double cost, int turnover)
{
      return(9.99); //Not correct, but good enough for some testing.
}

Edit | Attach | Watch | Print version | History: r4 < r3 < r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions...
Topic revision: r1 - 2016-09-26 - JimSkon
 
  • Edit
  • Attach
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback