Laboratory Activity 2
Laboratory Activity 2
Laboratory Activity 2
:
Name: Section:
Date Performed:
Date Submitted:
Instructor:
1. Objective(s):
The activity aims to introduce a different elements and functions of C++ and to develop a
program using different data types base on the given requirements.
3. Discussion:
A C++ programming language composed of the following components:
a. Editor
Editor as shown in figure 1 is used to enter the C++ instruction, statements and
commands known as source code. A programmer can user other editor program in
creating codes such as notepad and
word.
2
3 Menu
Tool Bars
1
Workspace of
an Editor
Figure 1. Editor Environment of C++ Programming Language
b. Compiler
1. Syntax Error
This error means that the instruction, statement, commands constructed is not following the
required format of C++ programming language.
2. Logical Error
c. Linker
Linkercombines the object file with other machine code necessary for C++ program to run
correctly
– such as mach
ine code that allows program to communicate with input and output devices.
To create and execute a C++ program, it consists of different stages as shown in figure 4.
C++ Programming
Language
Secondary
Source Code ObjectCode
Storage
(prog1.cpp) (prog1.obj)
Editor
(Create code)
Compiler
(Translating sourcecode into object code)
Linker
(Createexecutablecode)
Executable code
(prog1.exe)
Compiler translates source code prog1.cpp into machine code which is called object code,
prog1.obj. The compiler generates the appropriate object code, saving it automatically in a file
whose filename extension is .obj, which stands for object. The file containing the object code is
called the object file.
Linker combines the object file with other machine code necessary for C++ program to run
correctly – such as machine code that allows program to communicate with input and output
devices. The linker produces an executable file prog1.exe, which is a file that contains all of the
machine code necessary to run C++ program as many times as desired without the need for
translating the program again. The executable file has an extension of .exe, which stands for
executable, on its filename.
Below is a sample program showing a structure of a C++ program. The lines are numbered so that
it is easier to refer to them in the text; the line numbers are not entered in the program.
1 // Lab1– Calculates and displays the new weekly
pay by <your name> on > Comments
2 // Created <current
3date
4 #include > Directive
5<iostream
6 using namespace Using Statement
7std;
8 int main ) Start of Main function
9({ Start of the function
1 //declare /program body
0
1 variables
double currentPay =
1 0.0; le raiseRate = Variable Declaration
2
1 doub 0.0; raise =
double
3
1 0.0;double newPay =
4 0.0;
1
5
1 //enter input
6
1 itemscout << "Enter current weekly
7 pay:
1 "; >> currentPay ;
cin
8
1 cout << "Enter raise
9 rate:
2 cin"; >>
0
2 raiseRate;
1
2 /calculate raise and new
Function/program body
2 / raise
pay = currentRate *
3 raiseRate;
2 newPay = raise +
4 currentPay;
2
5
2 //display output
6
2 itemcout << "New Pay: " << newPay <<
7 endl;
2
8
2 system
9
3 (“pause”);
return
0 0;
3
1
3 } End of the function
2 /program body
Line 1 and- arecomment. Acommeis a message to the person reading the program
2 s nt andorit is referred
reminder of the programmer internal to . A comment can be
as documentation
located anywhere in the program. This will not affect the functionality of the
program.
Line 8 - is int main ( ). The word main ( ) is the name of a function and must be
typed using lowercase letters. A function is a block of code that performs a task.
Functions have parentheses following their names. Some functions require you
to enter information between the parentheses; other functions, like main ( ), do
not. Every C++ program must have a main ( ) function, because this is where the
execution of a C++ always begins. Notice that int, which stands for integer and is
typed using lowercase letters, precedes main ( ) in the code. The int indicates
that the main ( ) function returns a value of the integer data type. The entire line
of code, int main ( ), is referred to as a function header, because it marks the
beginning of the function. A program can only contain one main ( ) function.
Line 10-14 – the variables and its data type to be used in this program are being
declared. In this program, the variables are locally declared. Local variable
declaration means that these variables are exclusively used in this main function
header. Some other variables declared globally if the program has several
functions. Global variables are declared before the main function header.
4. Resources:
DevC++
values outputs?_______________________________________________________
ALGORITHM
NARRATIVE PSEUDOCODE
FLOWCHART
QUESTIONS
1. What are the data needed for the solution of the program to produce the desired
output?
________________________________________________________________
_____
________________________________________________________________
_____
2. What statements in the program that determine interest earned of each type of
account?
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
5. What have you observed in using switch statement in this program compare to if-
else statement?
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
EXERCISE No. 2
Mary Jane would like to invest her money amounting 100,000 at the bank. Before
investing her money, she would like to determine which type of account that she will
chose. The amount of interest that her money earns depends on which type of account
the money is in. The bank has 6 different types of accounts and they earn interest as
follows:
Account type Interest earned
personal financial 1.5%
personal homeowner 2.0%
personal gold 2.5%
small business 3.0%
big business 3.5%
gold business 4.0%
Mary Jane as a programmer would like to make a program that computes automatically
the interest earned of her money in each type of account.
REQUIREMENTS:
1. Write the corresponding algorithm
a. Narrative
b. Pseudocode
2. Create the equivalent flowchart based on the algorithm of the given problem
3. Construct the program and record your screen display result
ALGORITHM
QUESTIONS
1. What are the data needed for the solution of the program to produce the desired
output?
________________________________________________________________
___________________________________________________________________
__________
________________________________________________________________
_____________
______________________________________________________________________
________
EXERCISE No.3
A student took four quizzes in a term and would like to compute their average.
He also would like to know what will be the remarks. Follow the range of grades and its
equivalent remarks, and then fill-up the table below:
100-95 - Excellent
94-90 – Very
Satisfactory
89-85 - Satisfactory
84-80 - Fine
79-75 - Fair
74 and below – Poor
REQUIREMENTS:
FLOWCHART
QUESTIONS
1. What are the data needed for the solution of the program to produce the desired
output?
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
remarks?
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
3. What have you observed in using switch statement in this program than using if-
else statement?
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
SUPPLEMENTAL ACTIVITIES
a. Yes
b. No
c. YesNo
d. No output; no error
e. No output; compile-time error
_____________________________________________________________________
2.What is the output of the following code fragment assuming num is 10?
if (num == 20)
cout << "teenager";
cout << "adult"
a. teenager
b. adult
c. teenageradult
d. No output; no error
e. No output; compile-time error
_____________________________________________________________________
if (num2>
num3) if
(num1 > num2)
valueNum
=num2; else
valueNum
=num3; else
if (num2 > num3)
valueNum
=num1; else
valueNum = num3;
_____________________________________________________________________
6. Conclusion:
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________