LAB ACTIVITY 2: BASIC C++ PROGRAM
Duration: 2 Hours
Learning Outcomes
This lab activity encompasses activities 2A, 2B, 2C, 2D and 2E
By the end of this practical session, you should be able to :
• Declare variables
• Use input output statements
• Apply operators and expression
Hardware/Software: C++ software (Microsoft Visual Studio, Turbo C++ 5.0/6.0)
SCENARIO:
Congratulations to you!!
You have impressed Miss Suria on your capability to solve the task handed to you in Activity
1. Therefore, Infinity Design Solution Sdn. Bhd, with the supervision of Miss Suria has given
you the task to help Finance Unit in completing payroll system. The scenario is that; In January
of each year, Infinity Design Solution Sdn. Bhd pays a 5% bonus to each of its salespeople.
The bonus is based on the amount of sales made by the sales person during the previous
year.
INSTRUCTION:
Before you continue to write codes in C++, Miss Suria gives you some exercises. Complete
the exercises below and follow the process in completing your codes.
Activity 2A
Activity Outcome: Declare variables
Duration : 90 minutes
Task 1: Fill in the blanks with the correct answer.
a)
Type Description
bool Stores either value true or false
char Typically a single octet(one byte). This is an integer type.
int The most natural size of integer for the machine
float A single-precision floating point value.
double A double-precision floating point value.
void Represents the absence of type.
wchar_t A wide character type
b)
Valid Declaration Valid Initialization
Type Variable List (choose one variable
as example)
int i,j,k int i, j, k; int i=10;
char c,ch char c, ch; char c=A;
float f,salary float f, salary; float salary=20.50;
double d double d; double d=10.52;
Activity 2B
:
Activity Outcome Write and compile a program using C++.
Duration : 90 minutes
Task 2 : Follow the procedure below step by step.
PROCEDURE OUTPUT
Program 1: 30
23.3333
Step 1: Type the programs given
below
#include <iostream>
using namespace std;
// Variable declaration:
int a,
b; int
c; float
f;
int main ()
{
// Variable definition:
int a,
b; int c;
float f;
// actual initialization
a = 10;
b = 20;
c = a + b;
cout << c << endl ;
f = 70.0/3.0; cout
<< f << endl ;
return 0;
}
Step 2: Compile the program.
Step 3: Write the output.
Activity 2C
Activity Outcome: Apply operators and expression
Duration : 90 minutes
Task 3 : Identify and match the types of operator with their example correctly.
Types of Examples
operator
Arithmetic
B = (A + C) * A
Operators
Relational
A++ will give 11
Operators
Logical Operators C *= A is
equivalent to C =
C*A
Increment
(A && B) is false.
Operators
Assignment (A == B) is not
Operators true.
Activity 2D
:
Activity Outcome Apply operators and expression
Duration : 90 minutes
Task 4 : Follow the procedure below step by step.
PROCEDURE OUTPUT
Program 1:
Step 1: Type the programs given below Line 1 - Value of c is :31
Line 2 - Value of c is :11
#include <iostream> Line 3 - Value of c is :210
using namespace std; Line 4 - Value of c is :2
Line 5 - Value of c is :1
main() Line 6 - Value of c is :21
{ Line 7 - Value of c is :22
int a = 21;
int b = 10; int
c;
c = a + b;
cout << "Line 1 - Value of c is :" << c << endl ;
c = a - b;
cout << "Line 2 - Value of c is :" << c << endl ;
c = a * b;
cout << "Line 3 - Value of c is :" << c << endl ;
c = a / b;
cout << "Line 4 - Value of c is :" << c << endl ;
c = a % b;
cout << "Line 5 - Value of c is :" << c << endl ;
c = a++;
cout << "Line 6 - Value of c is :" << c << endl ;
c = a--;
cout << "Line 7 - Value of c is :" << c << endl ;
return 0; }
Step 2: Compile the program.
Step 3: Write the output.
Activity 2E
Program 2: Line 1 - Condition is true
Line 2 - Condition is true
Step 1: Type the programs given below Line 4 - Condition is not true
Line 5 - Condition is true
#include <iostream>
using namespace std;
main()
{
int a = 5;
int b = 20;
int c ;
if ( a && b )
{
cout << "Line 1 - Condition is true"<< endl ;
}
if ( a || b )
{
cout << "Line 2 - Condition is true"<< endl ;
}
/* Let's change the values of a and b */
a = 0; b = 10;
if ( a && b )
{
cout << "Line 3 - Condition is true"<< endl ;
}
else
{
cout << "Line 4 - Condition is not true"<< endl
;
}
if ( !(a && b) )
{
cout << "Line 5 - Condition is true"<< endl ;
}
return 0; }
Step 2: Compile the program.
Step 3: Write the output.
Activity 2E.1
Activity Outcome Identify input, process and output
Duration : 20 minutes
Miss Suria has given you the task to help Finance Unit in completing payroll system. Rewrite
the steps to produce a complete output as in 2E.4 following the below procedure.
Task 1: You must identify the input, process and output from the given task in Case Study.
INPUT PROCESS OUTPUT
employee_name, save the employee employee_name,
employee_id, information employee_id,
gross_income, gross_income,
allowance, allowance,
overtime, overtime,
income_tax, income_tax,
loan, loan,
amount_sale amount_sale
:
Activity 2E.2
Activity Outcome: Constructing Flowchart
Duration : 20 minutes
Task 1: Construct a flowchart of the given task based on 2E.1.
Flowchart
Start
Input employee_name, employee_id,
gross_income, allowance, overtime,
income_tax, loan, amount_sale
Display employee_name,
employee_id, gross_income,
allowance, overtime, income_tax,
loan, amount_sale
End
:
Activity 2E.3
Activity Outcome Constructing Pseudocode
Duration : 20 minutes
Task 1: Construct a pseudocode of the given task based on 2E.2.
Pseudocode
Start
Input employee_name, employee_id, gross_income, allowance, overtime, income_tax, loan,
amount_sale
Display employee_name, employee_id, gross_income, allowance, overtime, income_tax, loan,
amount_sale
End
:
Activity 2E.4
Activity Outcome: Understand Input Output Statement in C++ . Duration
: 30 minutes
Task 1: Write code to display an output like below:
Output
:
CODE
#include <iostream>
using namespace std;
int main()
string employee_name;
int employee_id;
float gross_income, allowance, overtime, income_tax, loan, amount_sale;
cout<<"***********************************************************\n\n";
cout<<" Welcome to Infinity Design Solution Sdn. Bhd.\n";
cout<<" Payroll System.\n\n";
cout<<"***********************************************************"<<endl;
cout<<"Please Enter Your Name:"<<endl;
cin>>employee_name;
cout<<"Please Enter Your Employee ID:"<<endl;
cin>>employee_id;
cout<<"Please Enter Your Gross Income:"<<endl;
cin>>gross_income;
cout<<"Please Enter Your Allowance:"<<endl;
cin>>allowance;
cout<<"Please Enter Your Overtime:"<<endl;
cin>>overtime;
cout<<"Please Enter Your Income Tax:"<<endl;
cin>>income_tax;
cout<<"Please Enter Your Loan:"<<endl;
cin>>loan;
cout<<"Please Enter Your Amount Sale:"<<endl;
cin>>amount_sale;
cout<<"***********************************************************"<<endl;
cout<<"Your details Information are:"<<endl;
cout<<"Name: "<<employee_name<<endl;
cout<<"Employee ID: "<<employee_id<<endl;
cout<<"Gross Income: "<<gross_income<<endl;
cout<<"Allowance: "<<allowance<<endl;
cout<<"Overtime: "<<overtime<<endl;
cout<<"Income Tax: "<<income_tax<<endl;
cout<<"Loan: "<<loan<<endl;
cout<<"Amount Sale: "<<amount_sale<<endl;
cout<<"***********************************************************"<<endl;
return 0;
}
:
Activity 2E.5
Activity Outcome Write a program code and implement arithmetic operator Duration
: 30 minutes
Task 1: Write a complete code from the given task in Case Study. Then, compile the program
using C++ and write the output
The scenario is that; In January of each year, Infinity Design Solution Sdn. Bhd pays a 5%
bonus to each of its sales people. The bonus is based on the amount of sales made by the
sales person during the previous year. Display the pay roll and calculate the total salary for a
single worker.
#include <iostream>
using namespace std;
main()
{
#define bonus=0.05
float total_salary, salary;
int num_sales;
cout<<"What is the total amount of sales :";
cin>>num_sales;
cout<<"Enter employee salary :";
cin>>salary;
total_salary=salary+(num_sales*0.05);
cout<<"Total salary of employee :"<<total_salary<<endl;
return 0;
}