Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lab Activity 3B PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

DARVEN DANRAJ.

K -01DDT19F2041(DDT2B)

LAB ACTIVITY 3B: PROGRAM CONTROL


STRUCTURES (II)

Duration: 2 Hours

This lab activity encompasses activities 3B

By the end of this practical session, you should be able to :

• Solve problems using looping control structures.

Hardware/Software: C++ software (Microsoft Visual Studio, Turbo C++ 5.0/6.0)

SCENARIO:

Help Suria to do some exercises to improve her skills in solving problems related to looping
control structures.

Activity 3B.1
Activity Outcome: Write a program using repetition control structures in solving the given
problems.
Duration : 180 minutes

1. Based on the algorithm below, write a program using :


a. For loop
b. While loop
c. Do..while loop
1. Initialize Counter = 1; average = 0; Total
= 0;
2. Input number.
3. Add Total using formula:
Total = Total + number
4. Add Counter using formula:
Counter = Counter + 1
5. Compare whether Counter is greater than 5.
If yes, go to step 6.
If no, go to step 2.
6. Calculate Average of numbers using formula:
Average = Total / 5
7. Display Average.

1.a)
#include <iostream>
using namespace std;

int main ()
{
for (int counter=1, average=0, Total=0, number; counter > 0; counter++ )
{
cout << "Input number : ";
cin >> number;
Total = Total + number;

if (counter > 5)
{
average = Total / 5;
cout << "Total : " << Total;
cout << "\nAverage : " << average;
cout << "\n";
counter ++;
}
else
{
counter ++;
}
}

return 0;
}
1.b.
#include <iostream>
using namespace std;

int main ()
{
int counter = 1, average = 0, Total = 0;
while(counter > 0)
{
int number;
cout << "Input number : ";
cin >> number;
Total = Total + number;

if (counter > 5)
{
average = Total / 5;
cout << "Total : " << Total;
cout << "\nAverage : " << average;
cout << "\n";
counter ++;

}
else
{
counter ++;
}
}

return 0;

}
1.c.
#include <iostream>
using namespace std;

int main ()
{
int counter = 1, average = 0, Total = 0;

do
{
int number;
cout << "Input number : ";
cin >> number;
Total = Total + number;

if (counter > 5)
{
average = Total / 5;
cout << "Total : " << Total;
cout << "\nAverage : " << average;
cout << "\n";
counter ++;

}
else
{
counter ++;
}
}while(counter > 0);

return 0 ;
}

2. A class of ten students took a quiz. The mark is between the ranges 0 - 25 for this quiz
is available to you. Determine the average mark for this quiz. Based on this problem,
you are required to create a program.

#include <iostream>
using namespace std;

int main ()
{
float marks = 0, mark, average;
for(int i=10; i <= 10; i--)
{
//user input the marks
cout << "Input the mark : ";
cin >> mark;
marks = marks + mark;
if(mark <= 25 && mark >= 0)
{
if (i == 1)
{
average = marks / 10;
cout << "\nTotal Marks : " << marks;
cout << "\nAverage : " << average;
break;
}
}
else
{
cout << "\nError!";
break;
}
}

return 0;
}
3. Calculate the salary that employee get based on number of working. Rate of the
payment is RM8.00 per hour but the salary will be deducted with 10% KWSP. By using
a repetition control structure, write a program to calculate the total salary of 5
employees and the average salary of the employee.

#include <iostream>
using namespace std;

int main ()
{
double salary, hour, total_salary = 0, ave_salary;

for(int i = 1; i <= 5; i++)


{
cout << "\nInput your work time : ";
cin >> hour;
salary = (8 * hour) - (8 * hour * 0.1);
total_salary = total_salary + salary;

if(i == 5)
{
cout << "\nTotal salary : " << total_salary;
ave_salary = total_salary / 5;
cout << "\nAverage salary of employee : " << ave_salary;
}
}
return 0;
}
4. Write a program asking the user for the upper and the lower bound and then
displaying all the numbers between them. It should work somehow like this:

Lower bound: 12
Upper bound: 23
12 13 14 15 16 17 18 19 20 21 22 23

#include <iostream>
using namespace std;

int main ()
{
for(int i = 12; i >=12 && i <= 23; i++)
{
cout << i << " ";
}

return 0;
}
Activity 3B.2
Activity Outcome: Write a program using repetition control structures in solving the given
problems.
Duration : 180 minutes

Task 1:

PROBLEM
Case Senario:

Now Suria already to develop a system that will automate the process of calculating payroll for
employees worked at Infinity Design Solution Sdn. Bhd. Each employee will be recognized by
employee ID. The system will receive gross income, allowance, overtime, income tax and loan
deduction. The system will automatically calculate the total income, total deduction and net salary
for the employee. System will calculate the total income based total deduction. Now Head of HR
Department gives a new task to Suria. Help Suria to create t a payroll system will automatic
execute to calculate salary for 200 staff of Infinity Design Solution.

#include <iostream>
using namespace std;

int main ()
{

for(int i = 0; i <= 200; i++)


{
string employee_ID;
float gross_Inc, allow, ot, IncTax, Loan_Dedu;
float Total_Inc, Total_Dedu, net_salary;

cout << "[ " << i + 1 << " ]\n";


cout << "Input employee's ID : ";
cin >> employee_ID;
cout << "Input gross income : ";
cin >> gross_Inc;
cout << "Input allowance : ";
cin >> allow;
cout << "Input overtime : ";
cin >> ot;
cout << "Input income tax : ";
cin >> IncTax;
cout << "Input loan deduction : ";
cin >> Loan_Dedu;

Total_Dedu = IncTax + Loan_Dedu;


cout << "\nTotal Deduction : " << Total_Dedu;
Total_Inc = gross_Inc + allow + ot;
cout << "\nTotal Income : " << Total_Inc;
net_salary = Total_Inc - Total_Dedu;
cout << "\nNet Salary : " << net_salary << endl;

}
return 0;
}

Task 2:

PROBLEM
Case Senario:

Now Suria need to enhance a payroll system for Infinity Design Solution, which is the the payroll
system will calculate Average Salary of 10 employee. Help Suria.

#include <iostream>
using namespace std;

int main ()
{
float total_net_salary = 0, ave_net_salary;

for(int i = 1; i <= 10; i++)


{
string employee_ID;
float gross_Inc, allow, ot, Inc_Tax, Loan_Dedu;
float Total_Inc, Total_Ded, net_salary;

cout << "[ " << i << " ]\n";


cout << "Input employee's ID : ";
cin >> employee_ID;
cout << "Input gross income : ";
cin >> gross_Inc;
cout << "Input allowance : ";
cin >> allow;
cout << "Input overtime : ";
cin >> ot;
cout << "Input income tax : ";
cin >> Inc_Tax;
cout << "Input loan deduction : ";
cin >> Loan_Dedu;

Total_Ded = Inc_Tax + Loan_Dedu;


cout << "\nTotal Deduction : " << Total_Ded;
Total_Inc = gross_Inc + allow + ot;
cout << "\nTotal Income : " << Total_Inc;
net_salary = Total_Inc - Total_Ded;
cout << "\nNet Salary : " << net_salary << endl;

total_net_salary = total_net_salary + net_salary;

if(i == 10)
{
cout << "\n[Average Area]";
ave_net_salary = total_net_salary / 10;
cout << "Average salary : " << ave_net_salary;
}
}
return 0;
}

You might also like