Control Constructs

Case Study


PROBLEM:

Write a program to calculate the commission based wages of a computer salesman. His basic wage is £50 per week and he is expected to sell at least 10 computers. If he sells more than 10 computers, he receives an extra £5.50 per computer he sells after the 10th computer.


SOLUTION:

The basic algorithm could be drawn as:-

The structure of the program would then be:-

DECLARE CONSTANTS
DECLARE VARIABLES

PROMPT USER TO ENTER NUMBER OF COMPUTERS SOLD 
READ IN NUMBER



CALCULATE PAY
        IF SELLS MORE THAN 10 
                CALCULATE COMMISSION

OUTPUT PAYMENT

adding the actual code to the above:-


#include <iostream.h>


main()
{
	 //DECALRE CONSTANTS
        const float BASIC_PAY = 50;
        const int LEVEL = 10;
        const float COMMISSION = 5.50;

	 //DECLARE VARIABLES
        float pay, commission_pay;
        int num_computers;


        //PROMPT USER TO ENTER NUMBER OF COMPUTERS SOLD 
        cout << "Please enter number of computers sold: \en";
        //READ IN NUMBER
        cin >> num_computers;


        //CALCULATE PAY 

        if (num_computers > level)
        {
                commission_pay = (num_computers - LEVEL)* COMMISSION;
                pay = BASIC_PAY + commission_pay;
        }
        else
                pay = BASIC_PAY ;

        //OUTPUT PAYMENT
        cout << "Your wage for this week is " << pay << " pounds\en";


}

NOTE that this is only one possible solution to the problem.

Try to think of some alternative methods of solving this problem.


Back to Main Page for Lecture 3