Professional Documents
Culture Documents
MP4 Coprog1 De-Castro Limbo (Continuation)
MP4 Coprog1 De-Castro Limbo (Continuation)
DE LA SALLE LIPA
COLLEGE OF INFORMATION TECHNOLOGY AND ENGINEERING
COMPUTER SCIENCE DEPARTMENT
Computer Programming 1
I. OBJECTIVES
II. DISCUSSION
Nested Selection Structures:
A nested selection structure is contained within the outer selection structure
– In the true path or in the false path
Use if more than one decision must be made before appropriate action can
be taken
Logic errors in selection structures are usually the result of:
– Using a logical operator rather than a nested selection structure
– Reversing the primary and secondary decisions
– Using an unnecessary nested selection structure
Multiple-Patch Selection Structures
You can create a selection structure that can choose from several alternatives
– Multiple-path selection structure
• Also called extended selection structure
• If selection structure has many paths from which to choose, it is
often simpler to use the switch form
• Also known as the case form
•
III. METHODOLOGY
Direction: Construct an IPO, draw a flowchart and write a program for the given
scenarios.
Using Nested Selection Structure:
An automobile shop wants you to write a payroll for its employees. The program
must calculate each employee’s net salary, that is, the salary after taxes. Prompt
the user for the employee’s pay rate, hours worked, employee code (either ‘A’ for
1
[Type
DE LA SALLE LIPA
COLLEGE OF INFORMATION TECHNOLOGY AND ENGINEERING
COMPUTER SCIENCE DEPARTMENT
Computer Programming 1
full-time ir ‘B’ for part time), and state code (either ‘Y’ for New York or ‘J’ for New
Jersey). Allow the user to enter either upper- or lowercase letters. The program
should display the regular pay, the overtime pay, the gross pay, the tax, and the
net pay. Calculate the gross pay as follows. If the employee worked 40 hours or
less, the regular pay is the pay rate times the hours worked, and the overtime
pay is zero. If the employee worked over 40 hours, the regular pay is the pay rate
times 40, and the overtime pay is 1.5 times the pay rate the hours worked over
40.
Using Multiple Path Selection Structure:
The Easy Living resort hotel wants you to write a program to calculate the base
charge for its guests. The program should prompt the user for the room type (‘G’
for garden view, ‘P’ for pool view, or ‘L’ for lake view) and the number of days the
guest is to stay. The program should also prompt for whether the guest is to have
a refrigerator in the room and whether there is to be an extra bed in the room.
The daily room rates are as follows. A garden view room is $125.00 each day; a
pool view room is $145.00 each day; a lake view is $180.00 each day. A
refrigerator costs $2.50 extra each day. An extra bed costs $15.00 extra each
day for either a garden view room or a pool view room, but costs $20.00 extra
each day for a lake view room. Calculate the total bill by multiplying the room rate
(adjusted for a refrigerator and extra bed, if necessary) by the number of days
the guest is to stay. The program should display the room type, the number of
days of the stay, the basic daily room rate, the daily charge for the
refrigerator and bed (only if the guest requests these), and the total change for
the stay.
IV. OUTPUT
Process
1. Enter hours
worked, pay rate, #include <iostream>
employee code
and state code #include <iomanip>
2. If the hours
worked is using namespace std;
equivalent to 40
hours or less,
Compute regular
int main()
pay by
multiplying pay {
rate to hours
worked //Declare variables
2
[Type
DE LA SALLE LIPA
COLLEGE OF INFORMATION TECHNOLOGY AND ENGINEERING
COMPUTER SCIENCE DEPARTMENT
Computer Programming 1
6. If the employee
code is ‘A’ and
while (answer=='Y')
the state code is
not ‘Y’, the tax is
4.5% of the gross
pay. If a part time {
employee, the cout<<"Enter the number of hours worked in a
tax is 0 week: ";
7. Display the cin>> hoursWorked;
regular pay,
overtime pay, cout<<"Enter pay rate:$";
gross pay, tax
cin>> payRate;
and net pay
cout << "Enter employee code - A (full Time
8. If the user wants
Employee) B (Part Time Employee): ";
to input another
customer. cin >> employeeCode;
Start the employeeCode = toupper (employeeCode);
process
// Validate employee code
from the
top. if(employeeCode != 'A' && employeeCode !=
'B')
{
cout << "Invalid employee code!";
Output
return(0);
3
[Type
DE LA SALLE LIPA
COLLEGE OF INFORMATION TECHNOLOGY AND ENGINEERING
COMPUTER SCIENCE DEPARTMENT
Computer Programming 1
regular pay, }
overtime pay,
gross pay, cout << "Enter state code - Y (New York) J(New
Jersey): ";
tax,
cin >> stateCode;
net pay, number
stateCode = toupper (stateCode);
of customers.
// Validate state code
.
if(stateCode != 'Y' && stateCode != 'J')
{
cout << "Invalid state code!";
return(0);
}
//computations
if (hoursWorked <=40)
{
grossPay = hoursWorked*payRate;
cout<< "Regular pay:
$"<<regularPay<<fixed<<setprecision(2)<<endl;
cout<< "Overtime pay is:
$"<<endl;
if (stateCode == 'Y')
if (employeeCode == 'A')
{
taxRate = 0.07;
tax = grossPay * taxRate;
netPay = grossPay - tax;
}
else
{
taxRate = 0;
tax = grossPay * taxRate;
netPay = grossPay - tax;
}
else
4
[Type
DE LA SALLE LIPA
COLLEGE OF INFORMATION TECHNOLOGY AND ENGINEERING
COMPUTER SCIENCE DEPARTMENT
Computer Programming 1
if (stateCode == 'J')
if (employeeCode == 'B')
{
taxRate = 0;
tax = grossPay * taxRate;
netPay = grossPay - tax;
}
else
{
taxRate = 0.045;
tax = grossPay * taxRate;
netPay = grossPay - tax;
}
else
if (stateCode == 'Y')
if (employeeCode == 'A')
{
taxRate = 0.07;
5
[Type
DE LA SALLE LIPA
COLLEGE OF INFORMATION TECHNOLOGY AND ENGINEERING
COMPUTER SCIENCE DEPARTMENT
Computer Programming 1
if (stateCode == 'J')
if (employeeCode == 'B')
{
taxRate = 0;
tax = grossPay * taxRate;
netPay = grossPay - tax;
}
else
{
taxRate = 0.045;
tax = grossPay * taxRate;
netPay = grossPay - tax;
}
else
//Display
cout << "Overtime Time: " << overtime << "
hours" << endl;
cout<< "Overtime pay:
$"<<overtimePay<<fixed<<setprecision(2)<<endl;
cout<< "Gross pay:
$"<<grossPay<<fixed<<setprecision(2)<<endl;
cout<< "Tax pay:
$"<<tax<<fixed<<setprecision(2)<<endl;
cout<< "Net pay:
$"<<netPay<<fixed<<setprecision(2)<<endl;
}
6
[Type
DE LA SALLE LIPA
COLLEGE OF INFORMATION TECHNOLOGY AND ENGINEERING
COMPUTER SCIENCE DEPARTMENT
Computer Programming 1
employee +=1;
cout<<"Do you want to process another
employee? (Y/N):";
cin>>answer;
answer=toupper(answer);
//endwhile
{
cout << "Number of Employee Processed:" <<
employee << endl;
}
}
return 0;
}
//End of main function
Flowchart:
7
[Type
DE LA SALLE LIPA
COLLEGE OF INFORMATION TECHNOLOGY AND ENGINEERING
COMPUTER SCIENCE DEPARTMENT
Computer Programming 1
8
[Type
DE LA SALLE LIPA
COLLEGE OF INFORMATION TECHNOLOGY AND ENGINEERING
COMPUTER SCIENCE DEPARTMENT
Computer Programming 1
9
[Type
DE LA SALLE LIPA
COLLEGE OF INFORMATION TECHNOLOGY AND ENGINEERING
COMPUTER SCIENCE DEPARTMENT
Computer Programming 1
break;
case 'N': case 'n':
bedCharge=0.00;
break;
default:
cout<<"Invalid input";
}
charge=refCharge+bedCharge;
bill=(rate+charge)*days;
customers += 1;
cout << "Number of Customers Processed:" <<
customers << endl;
return 0;
}
10
[Type
DE LA SALLE LIPA
COLLEGE OF INFORMATION TECHNOLOGY AND ENGINEERING
COMPUTER SCIENCE DEPARTMENT
Computer Programming 1
Flowchart
11
[Type
DE LA SALLE LIPA
COLLEGE OF INFORMATION TECHNOLOGY AND ENGINEERING
COMPUTER SCIENCE DEPARTMENT
Computer Programming 1
12