COS 102 - Merged
COS 102 - Merged
NINAN O. D.
• Algorithms are a set of well-defined rules/instructions used to solve a specific
problem.
• Each algorithm is unique in the way it tackles a problem, taking into account
factors like the problem size, resources, and efficiency, among others.
READ degrees_Farenheit
degrees_Celcius = (5/9)*(degrees_Farenheit - 32)
DISPLAY degrees_Celcius
READ grade
IF (grade >= 95)
THEN student receives a distinction
• This is the simplest case of selection control. In fact, in essence it is a
sequential form but the second step is only taken when the condition
contained in the brackets is true, that is the last step (starting at THEN) is
selected for execution only when the condition is satisfied.
• Example 2. Control access to a computer depending on whether the user has
supplied a username and password that are contained in the system
database.
IF (username in database AND password corresponds to user)
THEN Accept user
ELSE
Deny user
• In this case, there is also a decision to be made but a more complex one. In
the condition is true only the Accept user step is executed and if the condition
is false only the Deny user step is executed.
• That is, the condition helps us make a selection which controls which of the
two steps will be executed.
• Now look more closely to the condition. Does it guarantees that the user is
valid? What if we had this condition instead, which looks pretty similar: IF
(username in database AND password in database)?
• What would happen if somebody typed a valid username but the password
of a different but also valid user?
• Does this second condition satisfies our requirement that to access this
account one should be the right user and know the corresponding password?
• When you write a condition always make sure that it performs exactly the
check that you intended it to! It is very easy to translate words in
pseudocode that does a different thing than what was intended!
Example 3. Decide on a student’s grade based on their coursework mark.
READ result
CASE (result >=90)
PRINT ‘A’
CASE (result >=80)
PRINT ‘B’
CASE (result >=70)
PRINT ‘C’
CASE (result >=60)
PRINT ‘D’
CASE (result <60)
PRINT ‘F’
• In example 3, there are five conditions which are checked, one after the other.
• Example: Design an algorithm to find the sum of all integers between 1 and
10,000 inclusive. Draw a flowchart to depict your algorithm.
• Algorithm
Step 1 Set sum = 0
Step 2 Set number = 0
Step 3 Set sum = sum + number
Step 4 Set number = number + 1
Step 5 If number < 10001 then go to step 3 else go to step 6
Step 6 Print sum
Step 7 Stop
• Flowchart
• Example 1. Compute the average of ten numbers:
total = 0
average = 0
FOR 1 to 10
Read number
total = total + number
ENDFOR
average = total / 10
• Example 2. Read numbers and add them up until their total value reaches (or
exceeds) a set value represented by S.
•
• The Concept of Algorithm
• One of the basic concepts of mathematics and computer science is algorithm.
• By an algorithm is meant a list of instructions specifying a sequence of
operations which will give the answer to any problem of a given type.
• In mathematics, a class of problems is considered solved when an algorithm
for solving them is found.
• The discovery of such algorithms is a natural aim of mathematics and
computer science.
• The concept of algorithm is very germane to the study of Computer Science.
• Computational problems are solvable only if there exist algorithms for solving
them.
• Characteristics of Algorithm
• It should be deterministic:
• An algorithm must be given in the form of a finite list of instructions
giving the exact procedure to be followed at each step of the calculation.
Thus the calculation does not depend on the calculator.; it is a deterministic
process which can be repeated successfully at any time and by anyone. So it
should not be ambiguous.
• . It should be general:
• An algorithm is a single list of instructions defining a calculation which
may be carried out on any initial data and which in each case gives the
correct result. In other words, an algorithm tells how to solve not just one
particular problem, but a whole class of similar problems.
• . It should be finite:
• Although the algorithm has definite length, its execution may repeat some
steps indefinitely that is, the computation may not terminate. In particular,
non-termination may occur for some input data. For example, an algorithm to
compute all the digits of the square root of x would terminate for x = 4, and
not terminate for x=3. To be accepted, an algorithm must terminate for all the
inputs.
• It should have one entry and exit: Multiple entries or exits may make the
algorithm ambiguous.
• It should act at least on one input and it should produce at least one output.
• DECISION TABLES
• A decision table, just like a flowchart is a tool of programming and systems
analysis. It can be used to define complex program logic.
Decision Table Format
Computer sciences accreditation Board (CSAB) identifies four general areas that
it considers crucial to the discipline of computer science :
• Theory of Computation
Investigates how specific computational problems can be solved efficiently.
• Algorithms and Data Structures
Investigates efficient ways to storing , organizing and using data.
INTRODUCTION TO CORE CONCEPT OF COMPUTING
(CONTINUATION)
• Computing is the process of giving the computer some tasks to do related to what it has
been programmed to do. They are majorly arithmetic operations.
COMPUTATIONAL PROBLEM OR PROBLEM
• Decision problem
• Search problem
• Counting problem
• Optimization problem
• Function problem
PROBLEM SOLVING
• WRITING PROGRAM
algorithm for finding the average of a set of grades and the program
• Test the program: this is the process you look at output of coding on the computer if is
giving you the desired output.
• Running a program is the process of telling the computer to evaluate the compiled
instructions.
• Bugs are problems/errors with a program that cause it to stop working or produce
incorrect or undesirable results.
• The process of finding and fixing errors in your code is called debugging
EXAMPLE OF PROBLEM SOLVED WITH ALGORITHM
• What Is a Flowchart?
A flowchart is a diagram that visually represents the progression of steps of a process or
workflows. They are commonly used as decision-making, problem-solving, system designing,
and educational tools.
There are multiple types of flowcharts including;
• Process flowchart - shows the steps of a process in sequential order
• Workflow chart - visualizes a workflow or actions that need to be carried out to achieve
a goal
FLOWCHART
• Flowchart Symbols; Listed below are some of most common flowchart symbols and their
meanings.
FLOWCHART
FLOWCHART MEANINGS
• Identify the process or the workflow you and your team want to document or visualize.
• Identify the different steps involved in executing this process. If several people are responsible
for carrying out the process, collaborate and get their input for an accurate visualization.
• Using the standard flowchart symbols available in the Creately flowchart maker, convert the
listed steps into a flowchart.
• First drag and drop the Start/End symbol onto the canvas. Using the Plus Create option, add
the next symbol depending on whether it’s a process/ step/ operation or decision. Make sure
to use the correct flowchart symbol to represent your data.
HOW TO CREATE A FLOWCHART
• Connect the shapes with arrows highlighting the flow of the process or workflow.
Creately’s Plus Create automatically recognizes the flow and adds the relevant connector
as you draw the flowchart.
• Customize the flowchart as necessary with Creately’s preset color themes before sharing
it with stakeholders for feedback.
• Review the flowchart frequently and update it as the process it represents, undergoes
changes.
ASSIGNMENT
i. Start
ii. Pour measure of juice into glass
iii. Take juice out of the cupboard
iv. drink juice
v. Take the glass orange squash out of the fridge to serve
vi. fill up glass to top with water
vii. Refrigerate to chill
viii. End/stop
EXAMPLE
• Consumption of a drink:
1. get off couch
2. walk to kitchen
3. open refrigerator
4. if there is a carton of lemonade or orange juice then {
ALGORITHM
PRINT ‘B’
CASE (result >=70)
PRINT ‘C’
CASE (result >=60)
PRINT ‘D’
CASE (result < 60)
PRINT ‘F’
In this case, there are five conditions which are checked, one after the other.
CONTROL STRUCTURES FOR PSEUDOCODE
• Condition: making a decision and doing one thing or somethingelse depending on the
outcome of the decision.
• Repetition : repeating something a fixed number of times or until some condition occurs.
CONTROL STRUCTURES FOR PSEUDOCODE
(CONTD)
• Storage: storing information for use in instructions further down the list.
• Abstraction is the process of reducing or factoring out details that are not necessary in
order to describe an algorithm.
• AlgorithmX: QuenchThirst
1. get off couch
2. walk to kitchen
3. open refrigerator
4. perform SubAlgorithm1
5. close refrigerator
6. drink it
EXAMPLE OF A SUB-ALGORITHM (CONTD)
SubAlgorithm1: GetDrink
1. if there is a carton of lemonade or orange juice then {
3. close refrigerator
4. go to the cupboard
5. open cupboard
6. take a glass
7. close cupboard
EXAMPLE OF A SUB-ALGORITHM (CONTD)
• AlgorithmX: QuenchThirst
• 1. get off couch
• 2. walk to kitchen
• 3. chooseDrink()
• 4. drink it
USE OF SUB-ALGORITHM
• Algorithm Y: SetTablefor4
1.Walk to kitchen
2. Repeat 4 times {
3. getGlass()
4. Place glass on table
5. getplate()
6. Place plate on table
7. getutensils()
8. Place knife and fork on table
}
PARAMETER
• Parameters are details required for a function to run. And a function is also known as
sub-algorithm.
USING PARAMETER IN FUNCTION
• GetGlasses(n):
• 1. go to the cupboard
• 2. open cupboard
• 3. repeat n times {
• 4. take a glass
• }
• 5. close cupboard
USING PARAMETER IN FUNCTION
• The n in the function above signify that there’s a parameter along the function that has to
be provided.
• Instruction number four will be repeated the numbers of time the value of n is.
ALGORITHM EFFICIENCY
• Time complexity simply means how long it takes a computer to run an algorithm.
• Space complexity simply means how much space the computer use while running an
algorithm.
COS 102
PROGRAMMING
Program
• A program is a list of instructions that the computer must follow to
process data into information.
• The instructions consist of statements used in a programming language,
such as BASIC.
• Programming is a five-step process for creating that list of instructions.
• The five steps in the programming process are as follows:
1. Clarify/define the problem—include needed output, input, processing
requirements.
2. Design a solution—use modeling tools to chart the program.
3. Code the program—use a programming language’s syntax, or rules, to
write the program.
4. Test the program—get rid of any logic errors, or “bugs,” in the program
(“debug” it).
5. Document and maintain the program—include written instructions for users,
explanation of the program, and operating instructions.
1. The problem clarification (definition) step consists of six mini-steps—
a. clarifying program objectives and users,
b. outputs,
c. inputs,
d. processing tasks;
e. studying the feasibility of the program; and
f. documenting the analysis.
a. clarifying program objectives and users,
• You need to write a statement of the objectives you are trying to
accomplish— the problem you are trying to solve.
• You also need to make sure you know who the users of the program
b. clarify desired outputs
• you need to understand the outputs—
• what to get out of the system—before you specify the inputs.
• what kind of hardcopy is wanted
• What information should the outputs include
c. Clarify desired inputs
• Once you know the kind of outputs required, you can then think about
input.
• What kind of input data is needed
• In what form should it appear
• What is its source
d. Clarify the desired processing
you need to understand the processing tasks that must occur in order for
input data to be processed into output data.
e. double-check the feasibility of implementing the program
Is the kind of program feasible within the present budget?
Will it take too long to accomplish
f. document the analysis
• This includes writing objective specifications of the entire process being
described.
2. Design the Program
• design the solution specified by the systems analysts
• you first need to create an algorithm.
• In computer programming, there are different algorithms to accomplish any
given task, and
• each algorithm has specific advantages and disadvantages in different
situations.
• Inventing elegant algorithms—algorithms that are simple and require the
fewest steps possible—is one of the principal challenges in programming.
• most programmers use a design approach called structured programming.
• Structured programming takes a top-down approach that breaks programs
into modular forms. (sequence, selection, case, and iteration).
a. determine the program logic, using a top-down approach
• The top-down program design is used to identify the program’s processing
steps, or modules.
• After the program is designed, the actual coding proceeds from the bottom
up, using the modular approach.
• Modularization: The concept of modularization is important. Modularization
dramatically simplifies program development, because each part can be
developed and tested separately.
• A module is a processing step of a program.
• Each module is made up of logically related program statements.
Sometimes called a subprogram or subroutine.
• Top-down program design: Top-down program design can be represented
graphically in a hierarchy chart.
• A hierarchy chart, or structure chart, illustrates the overall purpose of the
program, by identifying all the modules needed to achieve that purpose
and the relationships among them.
• The program move in sequence from one module to the next until all have
been processed.
• There must be three principal modules corresponding to the three principal
computing operations—input, processing, and output.
b. DESIGN DETAILS, USING PSEUDOCODE AND/OR FLOWCHARTS
• There are two ways to show details—write them or draw them; that is, use
pseudocode or use flowcharts. Most projects use both methods.
• Pseudocode is a method of designing a program using normal human-
language statements to describe the logic and the processing flow.
• A program flowchart is a chart that graphically presents the detailed series of
steps (algorithm, or logical flow) needed to solve a programming problem.
• The flowchart uses standard symbols—called ANSI symbols, after the
American National Standards Institute, which developed them.
• Control structures: A control structure, or logic structure, is a structure that
controls the logical sequence in which computer program instructions are
executed.
• In structured program design, three control structures are used to form the
logic of a program: sequence, selection, and iteration (or loop).
• One thing that all three control structures have in common is one entry and
one exit.
• The control structure is entered at a single point and exited at another single
point. This helps to avoid incomprehensible program known as spaghetti
code.