Difference: CodeCh5 (1 vs. 4)

Revision 42016-09-29 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Function - Void

Line: 396 to 396
  %ENDCODE%
Changed:
<
<

Function -

>
>

Reference Example

  %CODE{"c++"}%
Added:
>
>
#include

using namespace std;

void addOne(int &y) // y is a reference variable { y = y + 1; } /* * */ int main() { int a = 0, b = 12, c = 100;

cout << "a:" << a << " b:" << b << " c:" << c << endl; addOne(a); addOne(b); addOne(c); addOne(a);

cout << "a:" << a << " b:" << b << " c:" << c << endl; return 0; }

  %ENDCODE%

Revision 32016-09-27 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Function - Void

Line: 62 to 62
  %ENDCODE%
Changed:
<
<

Function - super market

>
>

Function - call-by-reference parameters

  %CODE{"c++"}%
Added:
>
>
//DISPLAY 5.4 Call-by-Reference Parameters //Program to demonstrate call-by-reference parameters. #include
 
Added:
>
>
void get_numbers(int& input1, int& input2); //Reads two integers from the keyboard.
 
Changed:
<
<
//DISPLAY 5.11 Program with a Stub
>
>
void swap_values(int& variable1, int& variable2); //Interchanges the values of variable1 and variable2.

void show_results(int output1, int output2); //Shows the values of variable1 and variable2, in that order.

int main( ) { int first_num, second_num;

get_numbers(first_num, second_num); swap_values(first_num, second_num); show_results(first_num, second_num); return 0; }

//Uses iostream: void get_numbers(int& input1, int& input2) { using namespace std; cout << "Enter two integers: "; cin >> input1 >> input2; } void swap_values(int& variable1, int& variable2) { int temp;

temp = variable1; variable1 = variable2; variable2 = temp; }

//Uses iostream: void show_results(int output1, int output2) { using namespace std; cout << "In reverse order the numbers are: " << output1 << " " << output2 << endl; } %ENDCODE%

Function - Function Calling Another Function

<-- SyntaxHighlightingPlugin -->
//DISPLAY 5.8 Function Calling Another Function
//Program to demonstrate a function calling another function.
#include <iostream>

void get_input(int& input1, int& input2);
//Reads two integers from the keyboard.

void swap_values(int& variable1, int& variable2);
//Interchanges the values of variable1 and variable2.

void order(int& n1, int& n2);
//Orders the numbers in the variables n1 and n2
//so that after the function call n1 <= n2.

void give_results(int output1, int output2);
//Outputs the values in output1 and output2.
//Assumes that output1 <= output2


int main( )
{
    int first_num, second_num;

    get_input(first_num, second_num);
    order(first_num, second_num);
    give_results(first_num, second_num);
    return 0;
}


//Uses iostream:
void get_input(int& input1, int& input2)
{
    using namespace std;
    cout << "Enter two integers: ";
    cin >> input1 >> input2;
}

void swap_values(int& variable1, int& variable2)
{
    int temp;

    temp = variable1;
    variable1 = variable2;
    variable2 = temp;
}

void order(int& n1, int& n2)
{
    if (n1 > n2)
        swap_values(n1, n2);
}

//Uses iostream:
void give_results(int output1, int output2)
{
    using namespace std;
    cout << "In increasing order the numbers are: "
         << output1 << " " << output2 << endl;
}
<-- end SyntaxHighlightingPlugin -->

Function - Driver Program

<-- SyntaxHighlightingPlugin -->
//DISPLAY 5.10 Driver Program
//Driver program for the function get_input.
#include <iostream>

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.

int main( )
{
    using namespace std;
    double wholesale_cost;
    int shelf_time;
    char ans;
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
    do
    {
        get_input(wholesale_cost, shelf_time);

        cout << "Wholesale cost is now $" 
             << wholesale_cost << endl;
        cout << "Days until sold is now " 
             << shelf_time << endl;
       
        cout << "Test again?"
             << " (Type y for yes or n for no): ";
        cin >> ans;
        cout << endl;
    } while (ans == 'y' || ans == 'Y');

    return 0;
}
//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;
}
<-- end SyntaxHighlightingPlugin -->

Function - Supermarket Pricing

%CODE{"c++"}% //DISPLAY 5.9 Supermarket Pricing
 //Determines the retail price of an item according to //the pricing policies of the Quick-Shop supermarket chain. #include
Added:
>
>
const double LOW_MARKUP = 0.05; //5% const double HIGH_MARKUP = 0.10; //10% const int THRESHOLD = 7; //Use HIGH_MARKUP if not expected //to sell in 7 days or less.
 void introduction( ); //Postcondition: Description of program is written on the screen.
Line: 136 to 302
  << "Retail price= $" << price << endl; }
Changed:
<
<
//This is only a stub:
>
>
//Uses defined constants LOW_MARKUP, HIGH_MARKUP, and THRESHOLD:
 double price(double cost, int turnover) {
Changed:
<
<
return(9.99); //Not correct, but good enough for some testing. }
>
>
if (turnover <= THRESHOLD) return ( cost + (LOW_MARKUP * cost) ); else return ( cost + (HIGH_MARKUP * cost) );
 
Added:
>
>
}
  %ENDCODE%
Changed:
<
<

Function -

>
>

Function - super market

  %CODE{"c++"}%
Deleted:
<
<
%ENDCODE%
 
Changed:
<
<

Function - Call By Reference Example

%CODE{"c++"}% //DISPLAY 5.4 Call-by-Reference Parameters //Program to demonstrate call-by-reference parameters.
>
>
//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
Changed:
<
<
void get_numbers(int& input1, int& input2); //Reads two integers from the keyboard.
>
>
void introduction( ); //Postcondition: Description of program is written on the screen.
 
Changed:
<
<
void swap_values(int& variable1, int& variable2); //Interchanges the values of variable1 and variable2.
>
>
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.
 
Changed:
<
<
void show_results(int output1, int output2); //Shows the values of variable1 and variable2, in that order.
>
>
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( ) {
Changed:
<
<
int first_num, second_num;
>
>
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);

 
Deleted:
<
<
get_numbers(first_num, second_num); swap_values(first_num, second_num); show_results(first_num, second_num);
  return 0; }

//Uses iostream:

Changed:
<
<
void get_numbers(int& input1, int& input2)
>
>
void introduction( )
 { using namespace std;
Changed:
<
<
cout << "Enter two integers: "; cin >> input1 >> input2;
>
>
cout << "This program determines the retail price for\n" << "an item at a Quick-Shop supermarket store.\n";
 }
Changed:
<
<
void swap_values(int& variable1, int& variable2)
>
>
//Uses iostream: void get_input(double& cost, int& turnover)
 {
Changed:
<
<
int temp;

temp = variable1; variable1 = variable2; variable2 = temp;

>
>
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:

Changed:
<
<
void show_results(int output1, int output2)
>
>
void give_output(double cost, int turnover, double price)
 { using namespace std;
Changed:
<
<
cout << "In reverse order the numbers are: " << output1 << " " << output2 << endl;
>
>
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;
 }
Added:
>
>
//This is only a stub: double price(double cost, int turnover) { return(9.99); //Not correct, but good enough for some testing. }

%ENDCODE%

Function -

<-- SyntaxHighlightingPlugin -->

<-- end SyntaxHighlightingPlugin -->

Function -

%CODE{"c++"}%
 %ENDCODE%

Function -

Revision 22016-09-26 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Function - Void

Deleted:
<
<
 
Changed:
<
<
%CODE{"c++"}%//DISPLAY 5.2 void Functions
>
>
%CODE{"c++"}% //DISPLAY 5.2 void Functions
 //Program to convert a Fahrenheit temperature to a Celsius temperature. #include

Line: 144 to 144
 

%ENDCODE%

Added:
>
>

Function -

<-- SyntaxHighlightingPlugin -->

<-- end SyntaxHighlightingPlugin -->

Function - Call By Reference Example

<-- SyntaxHighlightingPlugin -->
//DISPLAY 5.4 Call-by-Reference Parameters
//Program to demonstrate call-by-reference parameters.
#include <iostream>

void get_numbers(int& input1, int& input2);
//Reads two integers from the keyboard.

void swap_values(int& variable1, int& variable2);
//Interchanges the values of variable1 and variable2.

void show_results(int output1, int output2);
//Shows the values of variable1 and variable2, in that order.

int main( )
{
    int first_num, second_num;

    get_numbers(first_num, second_num);
    swap_values(first_num, second_num);
    show_results(first_num, second_num);
    return 0;
}

//Uses iostream:
void get_numbers(int& input1, int& input2)
{
    using namespace std;
    cout << "Enter two integers: ";
    cin >> input1
        >> input2;
}
void swap_values(int& variable1, int& variable2)
{
    int temp;


    temp = variable1;
    variable1 = variable2;
    variable2 = temp;
}

//Uses iostream:
void show_results(int output1, int output2)
{
    using namespace std;
    cout << "In reverse order the numbers are: "
         << output1 << " " << output2 << endl;
}
<-- end SyntaxHighlightingPlugin -->

Function -

<-- SyntaxHighlightingPlugin -->

<-- end SyntaxHighlightingPlugin -->

Function -

<-- SyntaxHighlightingPlugin -->

<-- end SyntaxHighlightingPlugin -->
 \ No newline at end of file

Revision 12016-09-26 - JimSkon

Line: 1 to 1
Added:
>
>
META TOPICPARENT name="WebHome"

Function - Void

<-- SyntaxHighlightingPlugin -->
//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
}
<-- end SyntaxHighlightingPlugin -->

Function - super market

<-- SyntaxHighlightingPlugin -->
//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.
}

<-- end SyntaxHighlightingPlugin -->
 
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