Vous êtes sur la page 1sur 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
Fundamentals to Programming

This chapter is a self-test to recap what have been learned in chapter 1 to 4.The aim is mainly
to demonstrate the ability to solve problem and write a simple program.

For each of the following question, you need to perform the steps listed below.

Step 1 – Problem Analysis (Identify Input, Output and Process)


Step 2 – Construct the Algorithm
Step 3 – Construct the Pseudocode and Draw the Flowchart
Step 4 – Write the C++ Program

5.1 Question 1 (Sample)

Write a program that computes the area of a triangle given its base and height.

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.0

Base float base 0.0

Output Area float area 0.0

Process: Area = Height * Base * 1/2

Step 2 – Construct the Algorithm

1. Get the height of triangle


2. Get the base of triangle
3. Calculate the area using the formula
area = height * base * 1/2
4. Announce the result

96
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. // Program to compute the area of a triangle Program
#include <iostream.h>
Structure
main() The include
{ directive enable
return 0; program to use
} input and output
functions contained
in the external file
called iostream.h
2. // Program to compute the area of a triangle Variable
#include <iostream.h>
Declaration and
main() Initialization
{ Declare variables
float height = 0.0, base = 0.0, area = 0.0; to be used in the
program
return 0;
}

97
Fundamentals to Programming

3. // Program to compute the area of a triangle Input


#include <iostream.h>
Prompt user for
main() input
{
float height = 0.0, base = 0.0, area = 0.0;

cout<<"Enter the height of triangle: ";


cin>>height;
cout<<"Enter the base of triangle: ";
cin>>base;

return 0;
}

4. // Program to compute the area of a triangle Process


#include <iostream.h>
Compute area of
main() the triangle
{
float height = 0.0, base = 0.0, area = 0.0;

5 cout<<"Enter the height of triangle: ";


cin>>height;
cout<<"Enter the base of triangle: ";
cin>>base;

area = height * base * 1 / 2;

return 0;
}
5. // Program to compute the area of a triangle Output
#include <iostream.h>
Display the result
main() on screen
{
float height = 0.0, base = 0.0, area = 0.0;

cout<<"Enter the height of triangle: ";


cin>>height;
cout<<"Enter the base of triangle: ";
cin>>base;

area = height * base * 1 / 2;

cout<<"The area of the triangle is "<<area;

return 0;
}

98
Lab Activity 1

Summary

Pseudocode Flowchart C++ Program


// Program to compute the area of a
triangle
#include <iostream.h>

main()
{
float height = 0.0, base = 0.0,
area = 0.0;

Read height cout<<"Enter the height of


triangle: ";
cin>>height;

Read base cout<<"Enter the base of


triangle: ";
cin>>base;

Set area to height * area = height * base * 1 / 2;


base * 1/2
5
Print area cout<<"The area of the triangle
is "<<area;

return 0;
}

99
Fundamentals to Programming

5.2 Question 2 (Sample)

Write a C++ program that compute and display with explanotary text, the cost of carpeting a
rectangular room.

The length and width of a room is in feet, and the price of carpeting per square foot is in
dollars and cents.

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.0

5 Width float

float
width

price
0.0

Price of 0.0
Carpeting per
square foot
Area float area 0.0
Output The Cost of float cost 0.0
Carpeting

Process: Area = Length * Width


Cost = Area * Price of Carpeting per Square Foot

Step 2 – Construct the Algorithm

1. Get the length of room


2. Get the width of room
3. Get the price of carpeting per square foot
4. Calculate the area using the formula
area = height * width
5. Calculate the cost using the formula
cost = area * price
6. Announce the result

100
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. // Program to compute the cost of carpeting a Program
// rectangular room
#include <iostream.h>
Structure
The include
main() directive enable
{ program to use
return 0; input and output
} functions contained
in the external file
called iostream.h
2. // Program to compute the cost of carpeting a Variable
// rectangular room
Declaration and
#include <iostream.h>
Initialization
main() Declare variables
{ to be used in the
program
float length = 0.0, width = 0.0, price = 0.0,
area = 0.0, cost = 0.0;

return 0;
}

101
Fundamentals to Programming

3. // Program to compute the cost of carpeting a Input


// rectangular room
Prompt user for
#include <iostream.h>
input
main()
{

float length = 0.0, width = 0.0, price = 0.0,


area = 0.0, cost = 0.0;

cout<<"Enter the length of room: ";


cin>>length;
cout<<"Enter the width of room: ";
cin>>width;
cout<<"Enter the price of carpeting per square
foot (RM): ";
cin>>price;

return 0;
}
4. // Program to compute the cost of carpeting a Process
// rectangular room

5
Compute the area
#include <iostream.h>
of the room and the
main() cost carpeting the
{ room
float length = 0.0, width = 0.0, price = 0.0,
area = 0.0, cost = 0.0;

cout<<"Enter the length of room: ";


cin>>length;
cout<<"Enter the width of room: ";
cin>>width;
cout<<"Enter the price of carpeting per square
foot (RM): ";
cin>>price;

area = length * width;


cost = area * price;

return 0;
}

102
Lab Activity 1

5. // Program to compute the cost of carpeting a Output


// rectangular room
Display the result
#include <iostream.h>
on screen
main()
{

float length = 0.0, width = 0.0, price = 0.0,


area = 0.0, cost = 0.0;

cout<<"Enter the length of room: ";


cin>>length;
cout<<"Enter the width of room: ";
cin>>width;
cout<<"Enter the price of carpeting per square
foot (RM): ";
cin>>price;

area = length * width;


cost = area * price;

cout<<"The cost of carpeting the room is RM "


<<cost;
5
return 0;
}

103
Fundamentals to Programming

Summary

Pseudocode Flowchart C++ Program


// Program to compute the cost of
// carpeting a room
#include <iostream.h>

main()
{

float length = 0.0, width = 0.0,


price = 0.0, area = 0.0,
cost = 0.0;

Read length cout<<"Enter the length of


room: ";
cin>>length;

Read width cout<<"Enter the width of


room: ";
cin>>width;

5 Read price cout<<"Enter the price of


carpeting per square
foot (RM): ";
cin>>price;

Set area to height * area = length * width;


width

Set cost to area * price cost = area * price;

Print cost cout<<"The cost of carpeting the


room is RM "<<cost;

return 0;
}

104
Lab Activity 1

5.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.0

Interest rate const float interest 0.05

5
Output Total amount float total 0.0

Process: Total = Money Invested + (Money Invested * Interest)

Step 2 – Construct the Algorithm

1. Get the money invested


2. Calculate the total amount using the formula
total amount = money invested + (money invested * interest rate)
3. 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
Fundamentals to Programming

Step 4 - Write the C++ Program

No Coding Remark
1. // Program to calculate the total amount of money Program
// invested with interest after one year
Structure
#include <iostream.h>
The include
main() directive enable
{ program to use
return 0; input and output
}
functions contained
in the external file
called iostream.h
2. // Program to calculate the total amount of money Variable
// invested with interest after one year
#include <iostream.h>
Declaration and
main() Initialization
{ Declare variables
float money_invested = 0.0, total= 0.0; to be used in the
const float interest = 0.05; program

5 }
return 0;

3. // Program to calculate the total amount of money Input


// invested with interest after one year
Prompt user for
#include <iostream.h>
input
main()
{
float money_invested = 0.0, total= 0.0;
const float interest = 0.05;

cout<<"Enter the money invested: ";


cin>>money_invested;

return 0;
}
4. // Program to calculate the total amount of money Process
// invested with interest after one year
Compute the total
#include <iostream.h>
amount of money
main() invested with
{ interest after one
float money_invested = 0.0, total= 0.0; year
const float interest = 0.05;

cout<<"Enter the money invested: ";


cin>>money_invested;

total = money_invested + (money_invested *


interest);

return 0;
}

106
Lab Activity 1

5. // Program to calculate the total amount of money Output


// invested with interest after one year Display the result
#include <iostream.h>
on screen
main()
{
float money_invested = 0.0, total= 0.0;
const float interest = 0.05;

cout<<"Enter the money invested: ";


cin>>money_invested;

total = money_invested + (money_invested *


interest);

cout<<"The total amount of money including 5%


interest after one year is RM “<<total;

return 0;
}

Summary
5
Pseudocode Flowchart C++ Program
// Program to calculate the total
// amount of money invested with
// interest after one year
#include <iostream.h>

main()
{
float money_invested = 0.0,
total= 0.0;
const float interest = 0.05;

Read money_invested cout<<"Enter the money


invested: ";
cin>>money_invested;

Set total to total = money_invested


money_invested + + (money_invested *interest);
(money_invested *
interest)

Print total cout<<"The total amount of money


including 5% interest after one
year is RM “<<total;

return 0;
}

107
Fundamentals to Programming

5.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.5) for overtime hours worked.

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.0

Overtime hours float overtime 0.0

5
Rate float rate 0.0
Output Salary float salary 0.0

Process: Salary = (Regular Hours * Rate) + (Overtime Hours * Rate * 1.5)

Step 2 – Construct the Algorithm

1. Get the regular hours worked


2. Get the overtime hours worked
3. Calculate the salary using the formula
salary = (regular hours * rate) + (overtime hours * rate * 1.5)
4. Announce the result

108
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.5)
Print Salary

Step 4 – Write the C++ Program

No Coding Remark
1. // Program to compute the weekly salary Program
#include <iostream.h>
Structure
main() The include
{ directive enable
return 0; program to use
} input and output
functions contained
in the external file
called iostream.h
2. // Program to compute the weekly salary Variable
#include <iostream.h>
Declaration and
main() Initialization
{ Declare variables
float regular = 0.0, overtime = 0.0, to be used in the
rate = 0.0, salary = 0.0; program
return 0;
}

109
Fundamentals to Programming

3. // Program to compute the weekly salary Input


#include <iostream.h>
Prompt user for
main() input
{
float regular = 0.0, overtime = 0.0,
rate = 0.0, salary = 0.0;

cout<<"Enter the regular hour worked a week: ";


cin>>regular;
cout<<"Enter the overtime hour worked a
week: ";
cin>>overtime;
cout<<"Enter the rate per hour: ";
cin>>rate;

return 0;
}
4. // Program to compute the weekly salary Process
#include <iostream.h> Compute the
main() weekly salary

5 {
float regular = 0.0, overtime = 0.0,
rate = 0.0, salary = 0.0;

cout<<"Enter the regular hour worked a week: ";


cin>>regular;
cout<<"Enter the overtime hour worked a
week: ";
cin>>overtime;
cout<<"Enter the rate per hour: ";
cin>>rate;

salary = (regular * rate) + (overtime * rate *


1.5);

return 0;
}
5. // Program to compute the weekly salary Output
#include <iostream.h> Display the result
main() on screen
{
float regular = 0.0, overtime = 0.0,
rate = 0.0, salary = 0.0;

cout<<"Enter the regular hour worked a week: ";


cin>>regular;
cout<<"Enter the overtime hour worked a
week: ";
cin>>overtime;
cout<<"Enter the rate per hour: ";

salary = (regular * rate) + (overtime * rate *


1.5);

cout<<"Your weekly salary is $ "<<salary;

return 0;
}

110
Lab Activity 1

Summary

Pseudocode Flowchart C++ Program


// Program to compute the weekly salary
#include <iostream.h>

main()
{
float regular = 0.0,
overtime = 0.0, rate = 0.0,
salary = 0.0;

Read regular cout<<"Enter the regular hour


worked a week: ";
cin>>regular;

Read overtime cout<<"Enter the overtime hour


worked a week: ";
cin>>overtime;

cout<<"Enter the rate per

5
Read rate
hour: ";
cin>>rate;

Set salary to salary = (regular * rate)


(regular * rate) + + (overtime * rate * 1.5);
(overtime * rate
* 1.5)

Print Salary cout<<"Your weekly salary is $ "


<<salary;

return 0;
}

111
Fundamentals to Programming

5.5 Question 5

Write a program that computes the power of 4 for an integer.

5.6 Question 6

Write a program that asks the user to type 5 numbers and writes the sum and average of the 5
numbers.

5.7 Question 7

Write a program that asks the user to enter two numbers and print the sum, product,
difference, quotient and modulus of the two numbers.

5 5.8 Question 8

Write a program that asks the user to type the price without tax of one bottle of wines, the
number of bottles you want to buy and the tax in percent units. The program must print the
total price including taxes.

5.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.

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.

112

Vous aimerez peut-être aussi