Laboratory Activity 2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 19

Course: Experiment No.

:
Name: Section:
Date Performed:
Date Submitted:
Instructor:

LABORATORY ACTIVITY NO.2

BASIC STRUCTURE OF C++ PROGRAM

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.

2. Intended Learning Outcomes (ILOs):

The students shall be able to:

1. Demonstrate skills in using the components of C++ programming language


effectively.
2. Understand and identify the C++ program elements and its functions.
3. Apply program elements in program development.
4. Develop a program that will execute different data types.

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

A compiler is necessary to translate-level


the high
instructions into machine
– the
code
0s and 1s
that the computer
canunderstand. Figure 2 shows on how to use compiler component In the of C++.
execute menu, select compile. and
In figure
run 3, it shows the progress of compiling process.

Figure. 2Use Compiler Component of C++

Figure 3. Compiling Progress


A compiler cannot successfully translate source code into machine code if the source code has errors.
The compiler detects two kinds of errors:

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

This error means that the program


cessfully
has suctranslated by the compiler, no errors have
detected and produces an output. However, the desired output has no been met.

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.

Process in Creating and Executing C++ Program

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)

Figure 3. Stages in Creating and Executing C++ Program


Editor is one of the components of C++ programming language to create the instructions,
statements and commands for the computer to follow and it is known as source code. The source
code is then saved in a file on a secondary storage such as a disk. Source code files created using
C++ have the filename extension .cpp, which stands for C plus plus. The file containing the source
code is called the source file, prog1.cpp as shown in the above figure..

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.

Lne - is apreprocesso directiv


. C++ programs typically include at least one directive,
i4 r e and standard library of C++ to be used for
most include many directives. A directive
contains
proper operation of the program.
is aItspecial instruction
s the
thatcompiler to include
tell the
contents of another file. The #include <iostream> means that iostream file must
be included in any program that uses cout to output data to the screen or cin to
input data from the keyboard. A #include directive provides a convenient way to
mere the source code from one file with the source code in another file, without
having to retype the code.

Line 6 - is using statements and ends with a semicolon. If a program uses


commands such as cout, cin, and endl, using statement must be declared. This
located in the std namespace; “std” means standard.

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 9 – open brace { marks the start of the function/program body.

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.

Line 16- 30 – is the body of the function. It composed of instructions, statements,


commands for the computer to perform. Instructions, statements and commands
are composed of different components.

4. Resources:

DevC++

5. Procedure and Output


Exercise No.1

Create an algorithm, flowchart and program that will do the following:

1. Ask the user to input two integers.

2. Multiply, divide, add, and subtract the two integers.

3. Display the result of all mathematical operations used

4. Compile and run the code.

5. What are the values of your input?

___________________________________________________ 6. What are the

values outputs?_______________________________________________________

7. Complete the table below.

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?
_____________________________________________________________________

_____________________________________________________________________

_____________________________________________________________________

3. Omit break statements in your program. What did you observe?

_____________________________________________________________________

_____________________________________________________________________
_____________________________________________________________________

4. Omit default statement in your program. What did you observe?


_____________________________________________________________________

_____________________________________________________________________

_____________________________________________________________________

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?

________________________________________________________________

_____________ 2. What statements in the program that determines interest earned

of each type of account?

___________________________________________________________________

__________

3. What was the condition applied to satisfy the requirement?

________________________________________________________________
_____________

4. What have you observed in using nested if statement in this program?

______________________________________________________________________
________

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:

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
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 the average grade

remarks?

_____________________________________________________________________

_____________________________________________________________________

_____________________________________________________________________

3. What have you observed in using switch statement in this program than using if-
else statement?
_____________________________________________________________________

_____________________________________________________________________

_____________________________________________________________________

SUPPLEMENTAL ACTIVITIES

1. Get the value of variable num assuming it is equal to 5.


if (num >= 6)
cout << "Yes";
cout << No;

a. Yes
b. No
c. YesNo
d. No output; no error
e. No output; compile-time error

Explain your answer.

_____________________________________________________________________
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

Explain your answer.

_____________________________________________________________________

3.After execution of the following code, what is stored in valueNum?


All variables are using data type int. Note that num1=3, num2=5, and num3=7.

if (num2>
num3) if
(num1 > num2)
valueNum
=num2; else
valueNum
=num3; else
if (num2 > num3)
valueNum
=num1; else
valueNum = num3;

a. the smallest value of num1, num2, and num3


b. the largest value of num1, num2, and num3
c. smaller of num1 and num2
d. larger of num1 and num3
e. randomly selected value from num1, num2, and num3

Explain your answer.

_____________________________________________________________________

6. Conclusion:
_______________________________________________________________________

_______________________________________________________________________

_______________________________________________________________________

_______________________________________________________________________

_______________________________________________________________________

_______________________________________________________________________

You might also like