You are on page 1of 12

CC102 –

Introduction to
Programming 1

MODULE 1 – Introduction to
Programming

MODULE 2 –
Algorithm, Pseudocode

MRS. LUIZA M. MACAPANAS


Professor
A. Module Lesson 1 – Introduction to Programming 1

B. Module Overview:
Programming is the way humans write their software. It is written from another
language which could end up with another language for a specific purpose of for an
organization to use.

Programming involves comprehension of the problem and logic analysis. It is a way


of instructing the computer on what it should do.

Programming has a series of steps to follow in order to produce a solution in a


systematic way with the use of the hardware components of a computer system.
C. Module Objective:
1. To identify the different languages that is available in the market.
2. To be able to enumerate the steps in solving the problem.
3. To be able to identify the different parts of the computer system that is involved in the
programming process
4. To discuss how computer compiles the software created to produce a source code.

D. Discussion/Learning Activity:
Read the Reference Material attached for this module.
Parts of a Computer System:
Hardware: Electronic Devices
Software: Instructions and Computer
Programs

Hardware
Input : Keyboard, Mouse
System unit:
Random Access Memory (RAM)
Central Processing Unit (CPU)
Output: Monitor, Printer
Secondary Storage: Disk Drive

Software - Instructions for the hardware. Actions to be performed.


Program - A set of instructions is called a program. Driving force behind the computer
Without a program – What is a computer? Collection of Useless Hardware
2 purposes:
- Tell the computer what to do
- Tell other people what we want the computer to do.

Compiling Source Code


A program written in a high-level language is called a source program (or source code). Since a
computer cannot understand a source program. Program called a compiler is used to translate
the source program into a machine language program called an object program. The object
program is often then linked with other supporting library code before the object can be
executed on the machine.

Programming

Programming – the creation of an ordered set of instructions to solve a problem with a


computer. Only about 100 instructions that the computer understands - Different programs will
just use these instructions in different orders and combinations.

The most valuable part of learning to program is learning how to think about arranging
the sequence of instructions to solve the problem or carry out the task

Programming Fundamentals: Putting the Instructions Together


Sequential Processing
- A List of Instructions written in sequence or in order.
Conditional Execution
- Ifs; for reasoning with two outcomes produced, where it is a way of making two paths for
decision making.
Repetition
- Looping / Repeating; a loop is a programming structure that repeats a sequence of
instructions until a specific condition is met. Programmers use loops to cycle through
values, add sums of numbers, repeat functions, and many other things. ... Two of the most
common types of loops are the while loop and the for loop.
Stepwise Refinement / Top-Down Design
- Breaking Things into Smaller Pieces
Calling Methods / Functions / Procedures /Subroutines
- Calling a segment of code located elsewhere
- top-down approach (also known as stepwise design and stepwise refinement and in some
cases used as a synonym of decomposition) is essentially the breaking down of a system
to gain insight into its compositional sub-systems in a reverse engineering fashion.
- Reuse of previously coded code segment

Methods of Programming
Procedural
- Defining set of steps to transform inputs into outputs
- Translating steps into code
- Constructed as a set of procedures
- Each procedure is a set of instructions
Object-Oriented
- Defining/utilizing objects to represent real-world entities that work together to solve
problem
- Basic O-O Programming Components
- Class
- Object/Instance
- Properties
- Methods

Example: Solving Math Word Problem

Read the Problem: Understand the description of problem or scenario, identifying the knowns
and unknowns. Decide how to go about solving the problem: Determine what steps need to be
taken to reach the solution. Solve the Problem: Write the solution Test the Answer: Make sure
the answer is correct

Solving Computing Problems


In general, when we solve a computing problem we are taking some inputs, processing
(performing some actions on) the inputs, and then outputting the solution or results.

This is the classic view of computer programming – computation as calculation

Polya’s steps (UDIE) can be very effective when applied to solving computing problems
Applying Polya’s Problem Solving to Programming
Step 1 - Understand the Problem
What is the Problem to be solved? What is the unknown? What is the condition? What is the
data? What is needed to solve the problem? What actions need to take
place? Identify the inputs and outputs Identify the processes needed to produce the outputs
from the given inputs. Draw a figure. Introduce suitable notation. Isolate Principle parts of the
problem.

Step 2 - Devise a Plan


Find connections between the knowns and unknowns.
Simplify: Break the problem into smaller sub-problems
Design a solution: Make a plan or list of actions to implement the solution
Algorithm / Flowchart / Psuedocode
Algorithm - A FINITE set of clear, executable steps that will eventually terminate to
produce the desired outcome; Logical design used to solve problems – usually a list of actions
required to perform task
Pseudocode - Written like program code but more “English Like” and doesn’t have to
conform to language syntax
Flowchart - Diagram that visually represents the steps to be performed to arrive at
solution.

Step 3 - Implement the Plan


Implement in a Programming Language; Carry out the plan checking the preliminary results at
each step. Code A Little Test A lot

Step 4 - Evaluate the Solution


Run the Code; Check results repeatedly and thoroughly; Use numerous test cases or data sets;
Use highly varied test case, including expected as well as and unexpected cases; Look for new
solutions; Is there a better, easier, or more efficient solution; Can other problems be solved
using these techniques?

Summary
U - Read the Problem Statement; Identify the inputs, outputs, and processes
D - Decide how to Solve the Problem; Create an Algorithm / Flowchart / Psuedocode
I - Program the Code; Implement in Programming Language
E - Test the Solution; Run the Code using numerous, varied test Cases
Polya’s 4 Steps of Problem Solving

U – Understand the Problem


D – Devise a Good Plan to Solve
I – Implement the Plan
E – Evaluate the Solution

A. Module Lesson 2 – Algorithm, Pseudo code

B. Module Overview:
A way of solving the problem in a sequential manner, through how we approach day
to day activity to accomplish. This is by using the English like language and written in a
simple layman’s term to fully comprehend how we will write our solution to the problem.

We make use of numbers to show the sequence of solution from simple to complex
problems. We write them in detail to show the process of how the words later will be
converted to simple codes when we start writing them into source codes.

C. Module Objective:
1. To identify different algorithm and illustrate how one differs from the other.
2. To show the process of solving the problem either simple or complex by way of sentence.
3. To show the different decision making process in solving problems using algorithm.

D. Discussion/Learning Activity:
Read the Reference Material attached for this module.
Introduction to Algorithms
• A sequence of instructions.
• A procedure or formula for solving a problem.
• It was created mathematician, Mohammedibn-Musaal-Khwarizmi.
• Often used for calculation, data processing and programming.
• Algorithms can be expressed in any language.

Topics
• Algorithms
• Pseudocode
• Flowcharts

Introduction to Algorithms
• Algorithms for making things will often be divided into sections;
–the parts/components/ingredients(inputs)requiredtoaccomplishthetask–actions/steps/
methods(processing)toproducetherequiredoutcome(output).
• For example to build a model car, the parts(inputs) are needed plus instructions on how
to assemble the car(processing) and the result is the car(output).

Two forms of Algorithm:


– Pseudocode
– Flowchart

Pseudocode
• Pseudocode (which means fake code, because its not really programming code)
specifies the steps required to accomplish the task.
• Pseudocode is a type of structured English that is used to specify an algorithm.
• Pseudocode cannot be compiled nor executed, and there are no real formatting or
syntax rules.

Example of Pseudocode:
Ready and open subscriber file
Get a record
Do while more records
If current subscriber subscription count is > 3 then
Output the record
Get another record
End
Advantages of Pseudocode
• Reduced complexity.
• Increased flexibility.
• Ease of understanding.

Why is Pseudocode Necessary?


• The programming process is a complicated one.
• You must first understand the program specifications.
• Then you need to organize your thoughts and create the program.
• You must break the main tasks that must be accomplished into smaller ones in order to be
able to eventually write fully developed code.
• Writing Pseudocode will save you time later during the construction & testing phase of a
program's development.

How to Write Pseudocode Statements?


There are six basic computer operations
1. A computer can receive information
Read (information from a file)
Get (information from the keyboard)
2. A computer can put out information
Write (information to a file)
Display (information to the screen)
3. A computer can perform arithmetic
Use actual mathematical symbols or the words for the symbols
Example:
Add number to total
Total = total + number
+, -, *, /
Calculate, Compute also used
4. A computer can assign a value to a piece of data3
Cases
i. to give data an initial value
Initialize, Set
ii. to assign a value as a result of some processing ‘=’
*x=5+y
iii. to keep a piece of information for later use Save, Store

5. A computer can compare two piece of information and select one of two alternative
actions
IF condition
THEN some action
ELSE alternative action
END IF
6. A computer can repeat a group of actions
WHILE condition (is true)
some action
END WHILE
FOR a number of times
some action
END FOR

Data Dictionaries
• The pseudo code by itself doesn't provide enough information to be able to write
program code.
• Data Dictionaries are used to describe the data used in the Pseudo Code.
• The standard data types used in Pseudo Code are Integer, Double, String, Char and
Boolean.

Examples for Algorithm writing solution to the problem:


Using our day to day activity would be the easiest. Think of the process as a computer system
would think. There is always an input, process and output.

We need to boil eggs in the frying pan.


Algorithm in sequential order as always would be:
1. Get the frying pan.
2. Put water enough for the eggs to get soaked.
3. Put the frying pan on top of the stove burner.
4. Turn on the stove.
5. Let it come to a boil.
6. As soon as it boil count 100 slowly to have a well cooked egg.
7. When you reach 100, turn off the stove and remove the egg from the boiling water and
place in a container.

Here you can see the


input: frying pan with water, fire, stove and egg.
Process: waiting for the boiling process to start and the counting to 100
Output: when 100 counting was reached, turn off and remove from pan.

Let us do another with a twist, for decision making: Say the same problem but this time, the
frying pan is not big enough to hold the egg with water. There is no fire coming out of the
burner.

Steps would then increase with two paths to make:


1. Get the frying pan.
Frying pan not big enough to hold the eggs altogether for cooking.
Decision 1: Group and Cook the eggs a time. Then proceed to Step 2.
Decision 2: Get a bigger frying pan to cook the egg altogether. Proceed to 2.
2. Put water enough for the eggs to get soaked.
3. Put the frying pan on top of the stove burner.
4. Turn on the stove.
No Fire lets out as when it was ignited.
Decision 1: Get the pan and place in a localized cooking stove (meaning use wood),
proceed to 5
Decision 2: Buy a tank of gas to cook . then proceed to 5
5. Let it come to a boil.
6. As soon as it boil count 100 slowly to have a well cooked egg.
7. When you reach 100, turn off the stove and remove the egg from the boiling water and
place in a container.

You see the process is increase and many inputs have taken in place and decisions are shown
in a step by step manner with a precise output and flow.

Example: Write an algorithm that will display the sum of two numbers:
1. Accept two numbers
2. Add the two numbers
3. Place the result of the two numbers added in sum
4. Display sum

Here if we analyze it:


INPUT: two numbers; no specific value. Why? Because if we execute the user working
or applying our program to test or evaluate will do many entries of different combination.

Example:
instance 1: first number: 1
Second number : 3
instance 2: first number: 5
Second number : 2

PROCESS: The term to display the sum means an operation involve. There are only two
operation that would likely occur in the CPU or computer. Arithmetic or Logic. Here in
this problem it is Arithmetic, USING THE OPERATOR ADDITION. Thus we say
add the two numbers.
OUTPUT: only one and specific. The result of the added number and we place it in a
container, called SUM. Why? because it changes its value all the time, as instances
changes also.

If we write the answer in a pseudo code manner, this is how we will write the
mathematical problem:
1. Enter two numbers
2. Accept and place in containers Num1, Num2
3. Add the two number or we could write it as
Sum = Num1 + Num2
4. Display Sum

Let us have another problem:


1. Write the solution in Algorithm and Pseudocode. You purchase 2 items in the store and
pay the amount where in your money is only 500.00.

Algorithm:
1. Get the item
2. Read the amount of the items
3. Add the prices of the items and place in total
4. Pay the item.
5. Compute the amount money given against the total and place the result in Change.
6. If there is change, give the change.

Pseudo Code:
1. Enter Prices of 2 items
2. Read Item1Price, Item2Price
3. Total all the prices
Total Price = Item1Price + Item2Price
4. Received the Payment 500.00
5. Compute for change
Change = Total Price – Payment
6. If change is available, display change and give change.

You might also like