Professional Documents
Culture Documents
DIT210 Chapter05 v03
DIT210 Chapter05 v03
Learning Objectives
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.
Write a program that computes the area of a triangle given its base and height.
96
Lab Activity 1
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
return 0;
}
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;
return 0;
}
98
Lab Activity 1
Summary
main()
{
float height = 0.0, base = 0.0,
area = 0.0;
return 0;
}
99
Fundamentals to Programming
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.
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
100
Lab Activity 1
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
5
width price
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
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;
return 0;
}
102
Lab Activity 1
103
Fundamentals to Programming
Summary
main()
{
return 0;
}
104
Lab Activity 1
Write a program that calculates and displays the amount of money you invested at 5 %
interest for one year.
Problem: To calculate the amount of money invested at 5 % interest for one year
5
Output Total amount float total 0.0
Pseudocode Flowchart
Read money_invested
Set total to money_invested +
(money_invested * interest)
Print total
105
Fundamentals to Programming
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;
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;
return 0;
}
106
Lab Activity 1
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;
return 0;
}
107
Fundamentals to Programming
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.
5
Rate float rate 0.0
Output Salary float salary 0.0
108
Lab Activity 1
Pseudocode Flowchart
Read regular
Read overtime
Read rate
Set salary to (regular * rate) + (overtime *
rate * 1.5)
Print Salary
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
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;
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;
return 0;
}
110
Lab Activity 1
Summary
main()
{
float regular = 0.0,
overtime = 0.0, rate = 0.0,
salary = 0.0;
5
Read rate
hour: ";
cin>>rate;
return 0;
}
111
Fundamentals to Programming
5.5 Question 5
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