You are on page 1of 18

# 5 Lab Activity 1

Learning Objectives

Upon successful completion of this chapter, students will be
able to:

§ Understand programming and problem solving
concepts
§ Design a basic program
§ Design algorithm
§ Transform algorithm into pseudocode and flowchart
§ Translate pseudocode and flowchart into C++ program
§ Describe the process followed in creating a program

Get the base of triangle 3. For each of the following question.The aim is mainly to demonstrate the ability to solve problem and write a simple program. Announce the result 96 . Output and Process) Step 2 – Construct the Algorithm Step 3 – Construct the Pseudocode and Draw the Flowchart Step 4 – Write the C++ Program 5.0 Base float base 0. Step 1 – Problem Analysis Problem: To compute the area of a triangle 5 Input and Output: Data Data Type Variable Name Initial Value Input Height float height 0. Step 1 – Problem Analysis (Identify Input. Fundamentals to Programming This chapter is a self-test to recap what have been learned in chapter 1 to 4.0 Process: Area = Height * Base * 1/2 Step 2 – Construct the Algorithm 1. Get the height of triangle 2.1 Question 1 (Sample) Write a program that computes the area of a triangle given its base and height. you need to perform the steps listed below.0 Output Area float area 0. Calculate the area using the formula area = height * base * 1/2 4.

0.h> Declaration and main() Initialization { Declare variables float height = 0. } 97 . // Program to compute the area of a triangle Program #include <iostream. to be used in the program return 0.0. program to use } input and output functions contained in the external file called iostream. area = 0. Lab Activity 1 Step 3 – Construct the Pseudocode and Draw the Flowchart Pseudocode Flowchart Read height Read base Set area to height * base * 1/2 Print area 5 Step 4 – Write the C++ Program No Coding Remark 1.0. base = 0.h> Structure main() The include { directive enable return 0. // Program to compute the area of a triangle Variable #include <iostream.h 2.

area = 0.0. } 4. cout<<"Enter the base of triangle: ". area = 0. } 5. return 0. base = 0. return 0. Fundamentals to Programming 3. cin>>base. cout<<"Enter the height of triangle: ". cout<<"Enter the height of triangle: ".0. // Program to compute the area of a triangle Input #include <iostream.h> Display the result main() on screen { float height = 0.0. return 0. cin>>height. cin>>height. cin>>height. cout<<"Enter the base of triangle: ". area = height * base * 1 / 2. // Program to compute the area of a triangle Output #include <iostream. // Program to compute the area of a triangle Process #include <iostream.0. cin>>base. } 98 . base = 0.0. cin>>base.0. area = height * base * 1 / 2. cout<<"Enter the base of triangle: ".h> Prompt user for main() input { float height = 0. base = 0. cout<<"The area of the triangle is "<<area.h> Compute area of main() the triangle { float height = 0. area = 0. 5 cout<<"Enter the height of triangle: ".0.0.0.

0. Read height cout<<"Enter the height of triangle: ". base = 0. area = 0. cin>>height.0.h> main() { float height = 0. base * 1/2 5 Print area cout<<"The area of the triangle is "<<area. return 0. Lab Activity 1 Summary Pseudocode Flowchart C++ Program // Program to compute the area of a triangle #include <iostream. } 99 .0. cin>>base. Set area to height * area = height * base * 1 / 2. Read base cout<<"Enter the base of triangle: ".

The length and width of a room is in feet. Announce the result 100 . the cost of carpeting a rectangular room. Get the width of room 3.0 Price of 0.0 Carpeting Process: Area = Length * Width Cost = Area * Price of Carpeting per Square Foot Step 2 – Construct the Algorithm 1.2 Question 2 (Sample) Write a C++ program that compute and display with explanotary text. Step 1 – Problem Analysis Problem: To compute the cost of carpeting a rectangular room Input and Output: Data Data Type Variable Name Initial Value Input Length float length 0. Calculate the area using the formula area = height * width 5.0 5 Width float float width price 0. Get the price of carpeting per square foot 4.0 Carpeting per square foot Area float area 0. and the price of carpeting per square foot is in dollars and cents. Get the length of room 2. Fundamentals to Programming 5.0 Output The Cost of float cost 0. Calculate the cost using the formula cost = area * price 6.

width = 0.0.0. } 101 .h 2. input and output } functions contained in the external file called iostream. // Program to compute the cost of carpeting a Variable // rectangular room Declaration and #include <iostream.0. Lab Activity 1 Step 3 – Construct the Pseudocode and Draw the Flowchart Pseudocode Flowchart Read length Read width Read Start length Read price Set area to height * width Set cost to area * price Print cost Read Read price width area = height * cost = area * 5 width price End Print cost Step 4 – Write the C++ Program No Coding Remark 1. area = 0. cost = 0.0.h> Structure The include main() directive enable { program to use return 0.h> Initialization main() Declare variables { to be used in the program float length = 0. price = 0. // Program to compute the cost of carpeting a Program // rectangular room #include <iostream. return 0.0.

cin>>width. cin>>length. cin>>length. cost = 0. cost = area * price.h> of the room and the main() cost carpeting the { room float length = 0. area = length * width. cost = 0. // Program to compute the cost of carpeting a Input // rectangular room Prompt user for #include <iostream.0.0. price = 0.0. cout<<"Enter the width of room: ".0. cout<<"Enter the price of carpeting per square foot (RM): ". area = 0. cout<<"Enter the price of carpeting per square foot (RM): ".0. cin>>price.0.0. price = 0. width = 0.0. } 4. width = 0. return 0. } 102 . // Program to compute the cost of carpeting a Process // rectangular room 5 Compute the area #include <iostream.0. cout<<"Enter the length of room: ". cout<<"Enter the length of room: ".0. cin>>width. cin>>price. area = 0. return 0. cout<<"Enter the width of room: ". Fundamentals to Programming 3.h> input main() { float length = 0.

cout<<"The cost of carpeting the room is RM " <<cost. cout<<"Enter the length of room: ".0. } 103 . cost = area * price. Lab Activity 1 5. price = 0.0. cout<<"Enter the price of carpeting per square foot (RM): ".0. 5 return 0.0. area = 0. cout<<"Enter the width of room: ".h> on screen main() { float length = 0. width = 0. cin>>width. cost = 0. cin>>length. cin>>price. area = length * width. // Program to compute the cost of carpeting a Output // rectangular room Display the result #include <iostream.0.

width = 0. cost = 0. area = 0.0.h> main() { float length = 0. 5 Read price cout<<"Enter the price of carpeting per square foot (RM): ".0. Fundamentals to Programming Summary Pseudocode Flowchart C++ Program // Program to compute the cost of // carpeting a room #include <iostream. return 0. cin>>width. Read length cout<<"Enter the length of room: ". Read width cout<<"Enter the width of room: ".0. } 104 . width Set cost to area * price cost = area * price.0. cin>>price. price = 0. cin>>length. Set area to height * area = length * width.0. Print cost cout<<"The cost of carpeting the room is RM "<<cost.

0 Process: Total = Money Invested + (Money Invested * Interest) Step 2 – Construct the Algorithm 1. Calculate the total amount using the formula total amount = money invested + (money invested * interest rate) 3.3 Question 3 (Sample) Write a program that calculates and displays the amount of money you invested at 5 % interest for one year. Step 1 – Problem Analysis Problem: To calculate the amount of money invested at 5 % interest for one year Input and Output: Data Data Type Variable Name Initial Value Input Money invested float money_invested 0.05 5 Output Total amount float total 0. Lab Activity 1 5. Announce the result Step 4 – Construct the Pseudocode and Draw the flowchart Pseudocode Flowchart Read money_invested Set total to money_invested + (money_invested * interest) Print total 105 .0 Interest rate const float interest 0. Get the money invested 2.

h> input main() { float money_invested = 0.0. Fundamentals to Programming Step 4 .05. } 106 .0. total = money_invested + (money_invested * interest). total= 0. input and output } functions contained in the external file called iostream. // Program to calculate the total amount of money Program // invested with interest after one year Structure #include <iostream. program 5 } return 0. to be used in the const float interest = 0.h 2.0. } 4. cin>>money_invested. // Program to calculate the total amount of money Variable // invested with interest after one year #include <iostream.Write the C++ Program No Coding Remark 1. // Program to calculate the total amount of money Input // invested with interest after one year Prompt user for #include <iostream.0.05. cout<<"Enter the money invested: ".05. // Program to calculate the total amount of money Process // invested with interest after one year Compute the total #include <iostream. const float interest = 0. total= 0.0. return 0. return 0. cin>>money_invested.h> Declaration and main() Initialization { Declare variables float money_invested = 0. 3.0. total= 0.h> The include main() directive enable { program to use return 0. cout<<"Enter the money invested: ".h> amount of money main() invested with { interest after one float money_invested = 0. year const float interest = 0.

return 0.0. Read money_invested cout<<"Enter the money invested: ". (money_invested * interest) Print total cout<<"The total amount of money including 5% interest after one year is RM “<<total. cin>>money_invested. } 107 . total = money_invested + (money_invested * interest). cout<<"Enter the money invested: ". cin>>money_invested. Set total to total = money_invested money_invested + + (money_invested *interest). total= 0. const float interest = 0. // Program to calculate the total amount of money Output // invested with interest after one year Display the result #include <iostream. } Summary 5 Pseudocode Flowchart C++ Program // Program to calculate the total // amount of money invested with // interest after one year #include <iostream. cout<<"The total amount of money including 5% interest after one year is RM “<<total. return 0. total= 0.05.0.0.h> main() { float money_invested = 0. Lab Activity 1 5.0. const float interest = 0.05.h> on screen main() { float money_invested = 0.

Get the overtime hours worked 3.0 Process: Salary = (Regular Hours * Rate) + (Overtime Hours * Rate * 1.0 Output Salary float salary 0.0 5 Rate float rate 0.5) for overtime hours worked.4 Question 4 (Sample) Write a program that calculates and displays the weekly salary for an employee who earns RM14 an hours and one-half (wage * 1. Step 1 – Problem Analysis Problem: To calculate the weekly salary Input and Output: Data Data Type Variable Name Initial Value Input Regular hours float regular 0. Get the regular hours worked 2.0 Overtime hours float overtime 0. Fundamentals to Programming 5. Calculate the salary using the formula salary = (regular hours * rate) + (overtime hours * rate * 1.5) Step 2 – Construct the Algorithm 1. Announce the result 108 .5) 4.

overtime = 0.0.h> Declaration and main() Initialization { Declare variables float regular = 0. salary = 0. to be used in the rate = 0.0.h> Structure main() The include { directive enable return 0.0.h 2.5) Print Salary 5 Step 4 – Write the C++ Program No Coding Remark 1.0. Lab Activity 1 Step 3 – Construct the Pseudocode and Draw the Flowchart Pseudocode Flowchart Read regular Read overtime Read rate Set salary to (regular * rate) + (overtime * rate * 1. // Program to compute the weekly salary Variable #include <iostream. } 109 . program return 0. program to use } input and output functions contained in the external file called iostream. // Program to compute the weekly salary Program #include <iostream.

5). return 0.0. } 5. } 110 . salary = 0. return 0. cin>>overtime.0. overtime = 0. cout<<"Enter the regular hour worked a week: ". rate = 0.0. cin>>rate. // Program to compute the weekly salary Output #include <iostream. overtime = 0. rate = 0. return 0. Fundamentals to Programming 3.0. cout<<"Enter the rate per hour: ". cin>>overtime. cin>>regular. cout<<"Enter the rate per hour: ". cout<<"Enter the overtime hour worked a week: ". cout<<"Enter the overtime hour worked a week: ". cin>>regular. salary = (regular * rate) + (overtime * rate * 1. salary = (regular * rate) + (overtime * rate * 1. cout<<"Your weekly salary is \$ "<<salary.0.0.0.0. overtime = 0.h> Compute the main() weekly salary 5 { float regular = 0. // Program to compute the weekly salary Process #include <iostream. } 4.0.h> Display the result main() on screen { float regular = 0.0. cin>>regular. rate = 0. cout<<"Enter the regular hour worked a week: ". cout<<"Enter the overtime hour worked a week: ". salary = 0.h> Prompt user for main() input { float regular = 0. // Program to compute the weekly salary Input #include <iostream. cout<<"Enter the regular hour worked a week: ".0. cin>>overtime.5). salary = 0. cout<<"Enter the rate per hour: ". cin>>rate.0.

cin>>overtime.0. cin>>regular. Read overtime cout<<"Enter the overtime hour worked a week: ". cout<<"Enter the rate per 5 Read rate hour: ". return 0. (overtime * rate * 1. salary = 0.5) Print Salary cout<<"Your weekly salary is \$ " <<salary. Read regular cout<<"Enter the regular hour worked a week: ".0.0. Lab Activity 1 Summary Pseudocode Flowchart C++ Program // Program to compute the weekly salary #include <iostream.5). overtime = 0. cin>>rate. rate = 0.0. Set salary to salary = (regular * rate) (regular * rate) + + (overtime * rate * 1. } 111 .h> main() { float regular = 0.

6 Question 6 Write a program that asks the user to type 5 numbers and writes the sum and average of the 5 numbers. product. Fundamentals to Programming 5. 5. 5. The program must print the total price including taxes. quotient and modulus of the two numbers. the number of bottles you want to buy and the tax in percent units.5 Question 5 Write a program that computes the power of 4 for an integer. 112 . 5. difference.7 Question 7 Write a program that asks the user to enter two numbers and print the sum.8 Question 8 Write a program that asks the user to type the price without tax of one bottle of wines. 5. 5 5.10 Question 10 Write a program that asks the user to type 2 integers M and N and exchange the value of M and N.9 Question 9 Write a program that asks the user to type the width and the length of a rectangle and then outputs to the screen the area and the perimeter of that rectangle.