Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Republic of the Philippines

POLYTECHNIC UNIVERSITY OF THE PHILIPPINES


LOPEZ QUEZON, BRANCH
Republic of the Philippines
POLYTECHNIC UNIVERSITY OF THE PHILIPPINES
LOPEZ QUEZON, BRANCH

Overview

This course introduces the fundamentals of logic formulation and allows the student to
learn and apply the art and style of procedural programming to solve computational problems
adhering to the standards and guidelines of documentation. It includes discussion on I/O
statements, loop and branching instructions, and creating functions and procedures. This
course will help the students in designing, implementing, testing and debugging a program
based on a given specification that uses such of the following fundamental programming
components: (1) primitive data types (2) basic computation (3) simple I/O (4) conditional and
iterative structures (5) definition of functions and parameters.

Module 1. Programming Concepts


Contents
1.1 Introduction to Programming
1.1.1 A brief history of programming languages
1.2 Programming a computer
1.2.1 Program Development Process
1.2.1.1 Analyze the problem
1.2.1.2 Design Algorithm
1.2.1.3 Implement the algorithm
1.2.1.4 Test and verify program
1.2.1.5 Maintain and update program

Objectives
At the end of the course, the students should be able to:
✓ Understand the concept of programming
✓ Define the terminology used in programming
✓ Learn the different programming paradigm and understand their uses
✓ Apply steps in program development.
✓ Design flowcharts and algorithms to solve the problems.
✓ Predict the results when an expression is evaluated.

COMP 20023 PROGRAMMING 1 1


Republic of the Philippines
POLYTECHNIC UNIVERSITY OF THE PHILIPPINES
LOPEZ QUEZON, BRANCH

Programming Concepts

1.1 Introduction to Programming

Figure 1 Computer programming [1]


In this module, we will be learning about the concept of programming. It is important to
understand what do we mean when we say computer programming. According to Bebbington,
computer programming is the process of designing and building an executable computer
program to accomplish a specific computing result. Programming involves tasks such as:
analysis, generating algorithms, profiling algorithms' accuracy and resource consumption, and
the implementation of algorithms in a chosen programming language. [2]

In essence, the word programming means giving a mechanism the direction to


accomplish a task. Now this may sound complicated but if you think about it, you might have
already programmed several mechanisms. For example, you might have programmed your
mobile phone to notify you of an appointment in a specific date and time. Or you may have
programmed your television to save your favorite channels. You also may have program your
washing machine to spin your laundry for a specific amount of time. Like your mobile phone, tv,
and washing machine, a computer is also a mechanism that needs directions. These instructions
that are given to a computer are called computer programs. A computer program is a collection

COMP 20023 PROGRAMMING 1 2


Republic of the Philippines
POLYTECHNIC UNIVERSITY OF THE PHILIPPINES
LOPEZ QUEZON, BRANCH

of instructions that performs a specific task when executed by a computer. Your computer will not
be able to accomplish a task if you do not provide instructions for it to follow.

The people who write these instructions are called programmers. In order to write a
program to instruct a computer, you need to think about each problem clearly, breaking them into
something called methods. A typical computer program will be constructed of lots of these
methods, and each will contain commands and statements to perform the operations required.

In able to write computer instructions, programmers use a specific set of language, called
programming languages to create computer programs. Some popular languages are C++,
Visual Basic, C# and Java. In this module, you will use C++ programming language.

1.1.1 A brief history of programming languages

Just as human begins communicate with each other through the use of languages such
as English, Spanish, Hindi and Chinese, programmers use a variety of programming languages
to communicate with the computer. Following are the development of programming languages
from machine learning, assembly languages, and then to high-level languages.

Machine Languages

Within a computer, all data is represented by


microscopic switches that can be either off or on. The
off switch is designated by a 0, and the on switch is
designated by a 1.

Computers can understand only these on and off


switches, therefore, the first programmers had to write
a program instruction using nothing but combinations
of 0s and 1s;

For example, a program might contain the


Figure 2 Example of Machine Language [3] instruction 00101 10001 10000. Instructions written in
0s and 1s are called machine language or machine
code.

COMP 20023 PROGRAMMING 1 3


Republic of the Philippines
POLYTECHNIC UNIVERSITY OF THE PHILIPPINES
LOPEZ QUEZON, BRANCH

The machine language represents only the way to communicate directly with the
computer. As you can imagine, programming in machine language is very tedious and error-prone
and requires highly trained programmers.

Assembly Languages

Slightly more advanced programming


language are called assembly language. The
assembly languages simplify the programmer’s
job by allowing the programmer to use
mnemonics in place of 0s and 1s in the program.

Mnemonics are memory aids – in this case,


alphabet abbreviations for instructions.

For example, most assembly languages use


the mnemonic ADD to represent add operation
and the mnemonic MUL to represent multiply
operation.
Figure 3 Example of Assembly Language [4]

Program written in an assembly language require an assembler, which is also a program,


to convert the assembly instructions into machine code – the 0s and 1s the computer can
understand. Although, it is much easier to write programs in assembly language than in machine
language, programming in assembly language still is tedious and requires highly trained
programmers. Programs written in assembly language are machine specific and usually must be
written in a different assembly language to run on a different computer.

COMP 20023 PROGRAMMING 1 4


Republic of the Philippines
POLYTECHNIC UNIVERSITY OF THE PHILIPPINES
LOPEZ QUEZON, BRANCH

High-Level Languages

High-level languages represent the next major development in programming languages.


High-level languages are a vast improvement over machine and assembly language, because
they allow the programmer to use instructions that are more closely resemble the English
language.

Figure 4 Example of High-level Language using php [5]

An example of an instruction written in a high-level language in figure 4 uses PHP where


you can see English-like words used in the codes. In addition, programs written in a high-level
language can be used on many different types of computers. Programs written in a high-level
language are more machine independent than machine and assembly language. As a result,
programs written in a high-level language usually require a compiler, which is also a program, to
convert the English-like instruction into the 0s and 1s the computer can understand.

Some high-level languages also offer additional program called an interpreter. Unlike
compiler, which translates all of a program’s high-level instructions before running the program,
an interpreter translates the instructions line by line as the program is running.

COMP 20023 PROGRAMMING 1 5


Republic of the Philippines
POLYTECHNIC UNIVERSITY OF THE PHILIPPINES
LOPEZ QUEZON, BRANCH

Difference between Machine Language and High-Level Language

Figure 5 Difference of a High-Level Language and a Machine Language [6]

Figure 6 Difference of a High-Level Language and Low-Level Language

COMP 20023 PROGRAMMING 1 6


Republic of the Philippines
POLYTECHNIC UNIVERSITY OF THE PHILIPPINES
LOPEZ QUEZON, BRANCH

1.2 Programming a Computer

Two types of Programming paradigm

Programming paradigms are a way to classify programming languages based on their


features. Languages can be classified into multiple paradigms.

1. Structured Programming

A programming paradigm aimed to improving the clarity, quality, and development


time of a computer program by making extensive use of subroutines, block structures, and
for and while loops – in contrast to using simple tests and jumps such as the go to
statement which could lead to “spaghetti code” which is difficult to both follow and to
maintain.
When writing a procedure-oriented program, the programmer concentrates on the
major tasks that the program needs to perform. In a procedure-oriented program, the
programmer determines and controls the order in which the computer processes the
instructions. In other words, the programmer must determine not only the proper
instructions to give the computer, but the correct sequence of those instructions as well.
Examples of high-level languages used to create procedure-oriented programs include
COBOL (Common Business Oriented Language), BASIC (Beginner’s All-Purpose
Symbolic Instruction Code), and C.

a. Sequence Structure – in a computer program directs the computer to process the


program instructions, one after another, in the order listed in the program.
b. Selection Structure – indicates that a decision (based on some condition) needs
to be made, followed by an appropriate action derived from that decision.
c. Repetition Structure – directs the computer to repeat one or more instructions
until some condition is met at which time the computer should stop repeating the
instruction. The repetition structure also referred to as a loop or as iteration.

COMP 20023 PROGRAMMING 1 7


Republic of the Philippines
POLYTECHNIC UNIVERSITY OF THE PHILIPPINES
LOPEZ QUEZON, BRANCH

2. Object Oriented Programming

More advance high-level languages can be used to create object-oriented programs in


addition to procedure-oriented ones. Different from a procedure-oriented program, which focuses
on individual tasks the program must perform, an object-oriented language requires the
programmer to focus on the objects that the program can use to accomplish its goal. The objects
can take on many different forms. Objects can be viewed as an independent unit and can be used
in more than one program, usually with little or no modification. The ability to use an object for
more than one purpose enables code-reuse, which saves programming time and money – an
advantage that contributes to the popularity of object-oriented programming. Examples of high-
level languages that can be used to create both procedure-oriented and object-oriented programs
include C++, Visual Basic, Java, and C#.

1.2.2 Program Development Process

The following are six steps in the Program Development Life Cycle:

1. Analyze the problem. The computer user must figure out the problem, then decide how
to resolve the problem - choose a program.
2. Design the algorithm. A flow chart is important to use during this step of the PDLC. This
is a visual diagram of the flow containing the program. This step will help you break down
the problem.
3. Implement the algorithm. This is using the language of programming to write the lines
of code. The code is called the listing or the source code. The computer user will run an
object code for this step.
4. Test and Verify Program. The computer user must debug. This is the process of finding
the "bugs" on the computer. The bugs are important to find because this is known as errors
in a program. One must run the program to make sure there are no syntax and logic errors.
Syntax are grammatical errors and logic errors are incorrect results.
5. Maintain and update the program. This step is the final step of gathering everything
together. Internal documentation is involved in this step because it explains the reason
one might have made a change in the program or how to write a program.[7]

COMP 20023 PROGRAMMING 1 8


Republic of the Philippines
POLYTECHNIC UNIVERSITY OF THE PHILIPPINES
LOPEZ QUEZON, BRANCH

Step 1 – Analyze the Problem.

You cannot solve a problem unless you understand it, and you cannot understand a
problem unless you analyze it – in other words, unless you identify its important components. The
two most important components of any problems are the problem’s output and its input. The
output is the goal of solving the problem, and the input is the item or items needed to achieve
the goal. When analyzing a problem, you always search first for the output and then for the input.

A helpful way to identify the output is to search the problem for an answer to the following
question: What does the user want to see displayed on the screen, printed on paper, or stored in
a file? The answer to this question typically is stated as nouns and adjectives in the problem
specification.

Treyson Mobley wants a program that calculates and displays the amount he
should tip a waiter at a restaurant. The program should subtract any liquor
charge from the total bill and then calculate the tip (using a percentage) on the
remainder.

Figure 7 Problem specification for Treyson Mobley

Figure 7 indicates that Treyson (the program’s user) wants to see the amount of the
waiter’s tip displayed on the screen; therefore, the output is the tip.

After determining the output, you then determine the input. A helpful way to identify the
input is to search the problem specification for an answer to the following question: What
information will the computer need to know to display, print, or store the output items? As with the
output, the input typically is stated as nouns and adjectives in the problem specification. When
determining the input, it helps to think about the information that you would need to solve the
problem manually, because the computer will need to know the same information. In this case, to
determine the tip, both you and the computer need to know the total bill, the liquor charge, and
the tip percentage; these items, therefore, are the input.

COMP 20023 PROGRAMMING 1 9


Republic of the Philippines
POLYTECHNIC UNIVERSITY OF THE PHILIPPINES
LOPEZ QUEZON, BRANCH

Step 2 – Design the algorithm.

The second step in the problem-solving process is to plan the algorithm that will transform the
problem’s input into its output. An algorithm is a step-by-step instruction that accomplish a task.
It is an English-like way of writing and expressing a solution to a problem. An algorithm helps
construct the actual program easily and clearly because it is like a blueprint of a program, it served
as an effective guide to our programming tasks.
Treyson Mobley wants a program that calculates and displays the amount he should tip a waiter at a restaurant. The
program should subtract any liquor charge from the total bill and then calculate the tip (using a percentage) on the
remainder.

Input Algorithm Output


Total bill 1. Enter the total bill, liquor change, and tip percentage tip
Liquor change 2. Calculate the tip by subtracting the liquor charge
Tip percentage from the total bill and then multiplying the
Remainder by the tip percentage
3. display the tip

Figure 8 Problem specification and IPO for Treyson Mobley


Problem

The algorithm in figure 8 is composed of short English statements, referred to as


pseudocode. The word pseudocode means false code. It’s called false code because although
it resembles programming language instructions, pseudocode cannot be understood by a
computer. Programmers use pseudocode to help them while they are planning an algorithm.
Although the word pseudocode might be unfamiliar to you, you already have written pseudocode
without even realizing it. Think about the last time you gave directions to someone. You wrote
down each direction on paper, in your own words. Your directions were a form of pseudocode.

Beside using pseudocode, programmers also use flowcharts when planning algorithms.
Unlike pseudocode, a flowchart uses standardized symbols to visually depict an algorithm. It is
the graphical representation of a program. A flowchart shows the logical sequence of instructions
which a computer has to follow. The major uses of flowchart are in program documentation and
program presentation.

COMP 20023 PROGRAMMING 1 10


Republic of the Philippines
POLYTECHNIC UNIVERSITY OF THE PHILIPPINES
LOPEZ QUEZON, BRANCH

Figure 9 Example structure of a flowchart.

You can draw the flowchart symbols by hand; or you can use drawing or shapes feature
in a word processor. You can also use a flowcharting program, such as SmartDraw or Visio.

COMP 20023 PROGRAMMING 1 11


Republic of the Philippines
POLYTECHNIC UNIVERSITY OF THE PHILIPPINES
LOPEZ QUEZON, BRANCH

The flowchart contains different symbols which are connected using lines, called
flowlines. Below are some of the basic symbols that are used in flowcharting:

Figure 10 Symbols and functions used in flowcharting

COMP 20023 PROGRAMMING 1 12


Republic of the Philippines
POLYTECHNIC UNIVERSITY OF THE PHILIPPINES
LOPEZ QUEZON, BRANCH

Treyson Mobley wants a program that calculates and displays the amount he should tip a waiter at a
restaurant. The program should subtract any liquor charge from the total bill and then calculate the tip
(using a percentage) on the remainder.

Input Processing Output


Total bill Processing items: total bill without liquor change tip
Liquor change
Tip percentage Algorithm (pseudocode)
1. Enter the total bill, liquor change, and tip percentage
2. Calculate the total bill without liquor charge by
subtracting the liquor charge from the total bill
3. Calculate the tip by multiplying the total bill
without liquor charge by the tip percentage
4. Display tip

Algorithm(flowchart)

start

Enter total bill,


Liquor charge, and
Tip percentage

totalNoLiquor = totalBill – liquor;

tip = totlNoLiqour * tipPercent;

Display tip

stop

Figure 11 Solution to the Treyson Mobley Problem with flowchart

COMP 20023 PROGRAMMING 1 13


Republic of the Philippines
POLYTECHNIC UNIVERSITY OF THE PHILIPPINES
LOPEZ QUEZON, BRANCH

Step 3 – Implement the algorithm

After analyzing a problem and planning its algorithm, you then desk-check the algorithm
before implementing it using a chosen programming language. The term desk-check refers to
the fact that the programmer reviews the algorithm. You desk-check the algorithm to verify that
it is not missing any instructions and that the existing instructions are correct and in the proper
order. Once you checked that your algorithm is correct you can now convert the pseudocode
into the programming language that you will use to code your program.

Treyson Mobley wants a program that calculates and displays the amount he should tip a waiter at a
restaurant. The program should subtract any liquor charge from the total bill and then calculate the tip (using
a percentage) on the remainder.

Input C++ instructions


Total bill
double totalBill = 0.0;
Liquor charge
double liquor = 0.0;
Tip percentage
double tipPercent = 0.0;

Processing double totalNoLiqour = 0.0;


Total bill without liquor charge
double tip = 0.0;
Outout
Tip cout << “Enter the total bill: “;
cin >> totalBill;
Algorithm (pseudocode) cout << “Enter the liquor charge: ”;
cin >> liquor;
1. Enter the total bill, liquor change, and tip percentage
cout << “Enter the tip percentage: ”;
2. Calculate the total bill without liquor charge by
cin >> tipPercent;
subtracting the liquor charge from the total bill
3. Calculate the tip by multiplying the total bill totalNoLiquor = totalBill – liquor;
without liquor charge by the tip percentage
4. Display tip tip = totlNoLiqour * tipPercent;

cout << “Tips: $” << tip << endl;

Figure 12 Calculation Statements for Treyson Mobley Problem

COMP 20023 PROGRAMMING 1 14


Republic of the Philippines
POLYTECHNIC UNIVERSITY OF THE PHILIPPINES
LOPEZ QUEZON, BRANCH

In figure 12 you can see who the algorithm for the Treyson Mobley problem is converted
into a C++ statements. Do not worry if you do not understand yet the codes that are used in
C++. We will be discussing the C++ statements on the next modules. This diagram is just to
show you how a programmer uses pseudocodes as guide in writing program codes.

Step 4 – Test and Verify the program

The next step is to evaluate and modify (if necessary) the program. You can test and
evaluate a program by entering your C++ instructions into the computer and then using the
computer to run (execute) the program. While the program is running, you enter the same sample
data used when desk-checking the program. If the results obtained when running the program
differ from those shown in the program’s desk-check, it indicates that the program contains errors,
referred to as bugs. The bugs must be located and removed from the program before the program
is released to the user. The programmer’s job is not finished until the program runs without errors
and produces the expected results.

The process of locating and correcting a bug in a program is called debugging. Program
bugs typically are caused by either syntax errors or logic errors. A syntax error occurs when you
break one of the programming language’s rules. A logic error can occur for a variety of reasons,
such as forgetting to enter an instruction or entering the instructions in the wrong order. Some
logic errors occur as a result of calculation statements that are correct syntactically but incorrect
mathematically.

In order to enter your C++ instructions into the computer, you need to have access to a
text editor, more simply referred to as an editor. You will also need a compiler which scans the
entire program and translate it as a whole into a machine code for the computer to understand.
The instructions you enter into an editor are called source code. Many C++ development tools
contain both editor and compiler in one integrated environment, referred to as IDE (Integrated
Development Environment). Examples include Microsoft Visual C++, Borland C++ Builder, and
CodeBlocks.

COMP 20023 PROGRAMMING 1 15


Republic of the Philippines
POLYTECHNIC UNIVERSITY OF THE PHILIPPINES
LOPEZ QUEZON, BRANCH

Step 5 – Maintain and update the program

After testing, the software project is almost complete. The structure charts, pseudocodes,
flowcharts and decision tables developed during the design phase become documentation for
others who are associated with the software project. This phase ends by writing a manual that
provides an overview of the program’s functionality, tutorials for the beginner, in-depth
explanations of major program features, reference documentation of all program commands and
a thorough description of the error messages generated by the program. Even after the software
is completed, it needs to be maintained and evaluated regularly. In software maintenance, the
programming team fixes program errors and updates the software.[8]

COMP 20023 PROGRAMMING 1 16


Republic of the Philippines
POLYTECHNIC UNIVERSITY OF THE PHILIPPINES
LOPEZ QUEZON, BRANCH

Module 1: ACTIVITIES/ASSESSMENT
Activity 1.1 – Writing your own step-by-step procedure

There’s no one correct answer for each task. This exercise is to give you practice in converting
heuristic commands into equivalent algorithms.

Exercise 1: Determine a step-by-step procedure (list the steps) to do the following tasks:
a. Fix a flat tire.
b. Make a telephone call
c. Log on to a computer
d. Cook a sunny side up egg

Sample: (You can have more steps if depending on your answer)


a. Steps to fix a flat tire
1st __________________________
2nd __________________________
3rd __________________________

b. Make a telephone call


1st __________________________
2nd __________________________
3rd __________________________

c. Log on to a computer
1st __________________________
2nd __________________________
3rd __________________________

e. Cook a sunny side up egg


1st __________________________
2nd __________________________
3rd __________________________

NOTE: Submit your Activities on our Google Class page in a document format on the specified
date. Late submission will have deduction in grades.

COMP 20023 PROGRAMMING 1 17


Republic of the Philippines
POLYTECHNIC UNIVERSITY OF THE PHILIPPINES
LOPEZ QUEZON, BRANCH

Module 1: ACTIVITIES/ASSESSMENT
Activity 1.2 – Program problem analysis

In each of these exercises, a programming problem is given. Read the problem statement first,
and then answer the question pertaining to the problem. Do not attempt to write a program to
solve problems. Instead, simply answer the questions following the program specifications.

Exercise 1: You’ve been asked to write a program to calculate the value of distance, in miles,
given this relationship: distance = rate X elapsed time

a. For this programming problem, how many outputs are required?


b. How many inputs does this problem have?
c. Determine a formula for converting input items into output items.
d. Test the formula written for letter c, using the following sample data: rate is 55 miles per
hour and elapsed time is 2.5 hours.

Exercise 2: You’ve been asked to write a program that calculates the average grade of a student
based on his/her 3 quizzes.

a. For this programming problem, how many outputs are required?


b. How many inputs does this problem have?
c. Determine a formula for converting input items into output items.
d. Test the formula written for letter c, using the following sample data: quiz 1 is 85, quiz 2 is
90, and quiz 3 is 82.

NOTE: Submit your Activities on our Google Class page in a document format on the
specified date. Late submission will have deduction in grades.

COMP 20023 PROGRAMMING 1 18


Republic of the Philippines
POLYTECHNIC UNIVERSITY OF THE PHILIPPINES
LOPEZ QUEZON, BRANCH

Module 1: ACTIVITIES/ASSESSMENT

Activity 1.3 – Writing an algorithm (pseudocode) and Creating a flowchart

Exercise 1: Write an algorithm and create a flowchart that computes and prints the sum of two
input numbers.

Exercise 2: Write an algorithm and create a flowchart that determines if the input number is
ODD or EVEN number.

Exercise 3: Write an algorithm and draw a flowchart that will compute the average of 3 quizzes
entered by the user. And then determine if the average of the 3 quizzes is passing grade or
failing grade. The passing grade is 75 and above. Display the average grade and remark (Pass
or failed)

NOTE: Submit your Activities on our Google Class page in a document format on the specified
date. Late submission will have deduction in grades.

COMP 20023 PROGRAMMING 1 19


Republic of the Philippines
POLYTECHNIC UNIVERSITY OF THE PHILIPPINES
LOPEZ QUEZON, BRANCH

References

1. https://www.assignmentexpert.com/blog/wp-content/uploads/2019/10/hardest-
programming-language.png
2. Bebbington, Shaun (2014). "What is coding". Tumblr. Archived from the original on 2020-04-29.
Retrieved 2014-03-03.
3. https://medium.com/@rats/java-basics-how-java-is-understood-by-your-machine-
57161a212b85
4. https://microcontrollerslab.com/pic-microcontroller-assembly-language/
5. https://techterms.com/definition/high-level_language
6. https://www.techdotmatrix.com/2018/01/high-level-programming-language-low-level-
programming-language/
7. http://blog.teachbook.com.au/index.php/computer-science/software-development/program-
development-lifecycle/
8. https://www.geeksforgeeks.org/software-engineering-program-development-life-cycle-pdlc/
9. Pomperada, Jake (2018). Introduction to Java Programming

COMP 20023 PROGRAMMING 1 20

You might also like