[go: up one dir, main page]

0% found this document useful (0 votes)
20 views107 pages

Computational Problem Solving

Uploaded by

zedoquaile20xx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views107 pages

Computational Problem Solving

Uploaded by

zedoquaile20xx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 107

COMPUTATIONAL PROBLEM

SOLVING
Program Development Cycle
Introduction
● Computer can perform a variety of tasks like receiving data,
processing it and producing useful results.

● Computer need to be instructed to perform even a simple task


like addition of two numbers

● Computer work on a set of instructions called computer program


Introduction
❖ Computer program: specifies a way to carry out a task

❖ Computer Programmer: responsible for designing, writing, and


modifying computer programs
1. System Programmer: write programs, which provides
interface and functionality to the hardware programs
2. Application Programmer: writes programs to fulfil a specific
task such as payroll system, inventory control
Developing a Program
Program consists of a series of instructions that a computer
processes to perform the required operation

Programmer should specifies:


1. The instruction to be performed
2. The order in which those instructions are to be performed
3. The data required to perform those instructions
Program Development Cycle
● Before writing a program (coding), the programmer has to
determine the problem that needs to be solved

● One common approach to problem solving is to use


program development cycle

● While developing a program for a real-world problem, an


organized plan should always be prepared. The program
development concept focuses on dividing the process of
building a program.
Steps involved in Program Development
Cycle
The program development concept defines seven phases/steps that each
program goes through. For each phase, a certain set of steps is defined. The
seven phases of program development are:

1. Problem Analysis

2. Task Analysis

3. Algorithm Development

4. Algorithm Testing

5. Coding

6. Testing and Debugging

7. Documentation

8. Implementation

9.Maitenance and Enhancement


Program Development Cycle
❖ Problem Analysis:
◦ Problem is analyzed precisely and completely
◦ Developer knows about the scope within the problem
needs to developed

❖ Task Analysis:
◦ Developer needs to develop various solutions to solve the
given problem
◦ From numerous solutions, optimum solution is chosen
Program Development Cycle
❖ Algorithm Development:
◦ An algorithm (ex. flowchart) is developed to depict the
basic logic of the selected solution
◦ Algorithm depicts the solution in logical steps
Program Development Cycle
❖ Algorithm Testing:
◦ Before converting the algorithm into actual code, it should be
checked for accuracy
◦ Test data need to be ‘walk through’ each step in the algorithm
◦ That verify that the instructions described in the algorithm
actually perform the required functions or not
◦ Identify major logical errors, if any
◦ Ensure that algorithm work for both normal and unusual data
Program Development Cycle
❖ Coding:
◦ Program takes place in the chosen programing language

❖ Testing and Debugging:


◦ Find logical errors (semantic errors) or due to the incorrect use
of programming language (syntax error)
◦ Results obtained are compared with results calculated manually
from the test data
◦ Depend upon the complexity, several rounds of testing may be
required
Program Development Cycle
❖ Documentation:

◦ Once the program is free from the errors, it is the duty of programmer
developer to ensure that program us supported by suitable documentation

◦ Documentation enables the user to operate the program correctly

❖ Implementation:

◦ The program is installed on the end user’s machine

◦ The implementation can be viewed as the final testing because only after
using the program, the user can point out the drawbacks and report them
to developers

❖ Maintenance and Enhancement:

◦ Program should properly maintained by taking care of the changing


requirements of its users and system
Pseudocode
Pseudocode
Flowcharts were the first design tool to be widely used, but
unfortunately the do not very well reflect some of the
concepts of structured programming.
Pseudocode, on the other hand , is a newer tool and has
features that make it more reflective of the structured
concepts. Unfortunately, the narrative presentation is not as
easy to understand and follow.
Pseudocode
The first thing we do when designing a program is to
decide on a name for the program.

Let’s say we want to write a program to calculate interest, a


good name for the program would be CalculatorInterest.
Pseudocode
Pseudocode
Pseudocode
Pseudocode
● When we write a program, we assume that the computer
executes the program starting at the beginning and
working its way to the end.

● This is the basic assumption of all algorithm design.

● We call this SEQUENCE.


Pseudocode
Pseudocode
● What ie we want to make a choice, for example, do we
want to add sugar or not to the tea?

● We call this SEQUENCE.


Pseudocode
Pseudocode
Pseudocode
● What if we need to tell computer to keep doing
something until some condition occurs?

● Let’s say we wish to indicate that the you need to


keep filling the kettle with water until it is full.

● We need a loop, or ITERATION.


Pseudocode
Pseudocode
● Pseudo code
Pseudo code can be broken down into five components.
• Variables:
• Assignment:
• Input/output:
• Selection:
• Repetition:

A variable has a name, a data type, and a value. There is a location in memory
associated with each variable. A variable can be called anything or be given any
name. It is considered good practice to use variable names that are relevant to the
task at hand.
Assignment is the physical act of placing a value into a variable. Assignment can
be shown using set = 5; set = num + set;
● Input / Output both deal with an outside source (can be a user or another
program) receiving or giving information. An example would be assuming a
fast food restaurant is a program. A driver (user) would submit their order for a
burger and fries (input), they would then drive to the side window and pick up
their ordered meal (output.) •
● Output – Write / display / print
● Input – Read / get / input Selection construct allows for a choice between
performing an action and skipping it. It is our conditional statements.
● Selection statements are written as such:
if ( conditional statement)
statement list
else
statement list
Repetition is a construct that allows instructions to be executed multiple times (IE
repeated). In a repetition problem
– Count is initialized
– Tested
– incremented

Repetition problems are shown as:


while ( condition statement)
statement list
Write pseudo code that reads two numbers and multiplies
them together and print out their product.
Pseudo code:

PROGRAM Product
Read num1 , num2
Set multi to num1*num2
Write multi
Write pseudo code that tells a user that the number they
entered is not a 5 or a 6.
Pseudo Code:

PROGRAM Num
READ isfive
IF(isfive = 5)
WRITE "your number is 5"
ELSE IF (isfive = 6)
WRITE "your number is 6"
ELSE
WRITE "your number is not 5 or 6"
Write pseudo code to print all multiples of 5 between 1 and
100 (including both 1 and 100).
Pseudo Code:

PROGRAM XYZ
SET x to 1
WHILE(x < 20)
WRITE x
x = x*5
Write pseudo code that will count all the even numbers up
to a user defined stopping point.
For example, say we want to see the first 5 even numbers
starting from 0.
well, we know that evens numbers are 0, 2, 4, etc.
The first 5 even numbers are 0, 2, 4, 6, 8.
The first 8 even numbers are 0, 2, 4, 6, 8 ,10 ,12, 16
Pseudo Code:

PROGRAM Num
READ count
SET x to 0;
WHILE(x < count)
SET even to even + 2
x=x+1
WRITE even
Write pseudo code that reads in three numbers and writes
them all in sorted order
Pseudo Code:

PROGRAM Sort
Read num1, num2, num3
If (num1 < num2)
If(num2 < num3)
Write num1 , num2, num3
Else
If(num3 < num1)
Write num3, num1, num2
Else
Write num1, num3, num2
else
If(num1 < num3)
Write num2 , num1, num3
Else
If(num3 < num2)
Write num3, num2, num1
Else
Write num2, num3, num1
Flowchart
Some Terminologies

➢ Algorithm / Flowchart
➢ A step-by-step procedure for solving a particular
problem.
➢ Independent of the programming language.

➢ Program
➢ A translation of the algorithm/flowchart into a form that
can be processed by a computer.
➢ Typically written in a high-level language like C, C++,
Java, etc.
Variables and Constants

➢ Most important concept for problem


solving using computers

➢ All temporary results are stored in terms of


variables
➢ The value of a variable can be changed.
➢ The value of a constant do not change.

➢ Where are they stored?


➢ In main memory.
Variables and Constants…

➢ How does memory look like (logically)?

➢ As a list of storage locations, each having a unique


address.
➢ Variables and constants are stored in these storage
locations.
➢ A variable is like a bin
➢ The contents of the bin is the value of the variable
➢ The variable name is used to refer to the value of the
variable
➢ A variable is mapped to a location of the memory,
called its address
Data Types

➢ Three common data types used:

➢ Integer :: can store only whole numbers


➢ Examples: 25, -56, 1, 0

➢ Floating-point :: can store numbers with fractional


values.
➢ Examples: 3.14159, 5.0, -12345.345

➢ Character :: can store a character


➢ Examples: ‘A’, ‘a’, ‘*’, ‘3’, ‘ ’, ‘+’
Flowchart: basic symbols

Computation

Input / Output

Decision Box

Start / Stop
Flowchart: basic symbols…

Flow of
control

Connector
Example 1: Adding three numbers

START

READ A, B,
C

S=A+B+C

OUTPUT S

STOP
Example 2: Larger of two numbers

START

READ X, Y

YES NO
IS
X>Y?

OUTPUT X OUTPUT Y

STOP STOP
Example 3: Largest of three numbers
START

READ X, Y, Z

YES IS NO
X > Y?

Max = X Max = Y

YES IS NO
Max >
Z?
OUTPUT
OUTPUT Z
Max

STOP STOP
Example 4: Sum of first N natural numbers

START

READ N

SUM = 0
COUNT = 1

SUM = SUM + COUNT

COUNT = COUNT + 1

NO YES OUTPUT
IS
SUM
COUNT > N?

STOP
Example 5: SUM = 12 + 22 + 32 + N2

START

READ N

SUM = 0
COUNT = 1

SUM = SUM + COUNT * COUNT

COUNT = COUNT + 1

NO YES
IS
OUTPUT SUM
COUNT > N?

STOP
Example 6: SUM = 1.2 + 2.3 + 3.4 + to N terms

START

READ N

SUM = 0
COUNT = 1

SUM = SUM + COUNT * (COUNT + 1)

COUNT = COUNT + 1

NO YES
IS OUTPUT
COUNT > N? SUM

STOP
Example 7: Computing Factorial

START

READ N

PROD = 1
COUNT = 1

PROD = PROD * COUNT

COUNT = COUNT + 1

NO YES
IS OUTPUT
COUNT > N? PROD

STOP
Example 8: Roots of a quadratic
equation

ax2 + bx + c = 0

TRY YOURSELF
Example 9: Grade computation

● MARKS ≥ 90 Ex
● 89 ≥ MARKS ≥ 80 A
● 79 ≥ MARKS ≥ 70 B
● 69 ≥ MARKS ≥ 60 C
● 59 ≥ MARKS ≥ 50 D
● 49 ≥ MARKS ≥ 35 P
● 34 ≥ MARKS F
Example 9: Grade computation…
START

READ
MARKS

NO NO NO
MARKS MARKS MARKS
≥ 90? ≥ 80? ≥ 70? A
YES YES YES

OUTPUT OUTPUT OUTPUT


“Ex” “A” “B”

STOP STOP STOP


Representation Information as bits

➢ Representing Text
➢ Representing Numeric Value
➢ Representing Image
➢ Representing Sound
Representing Text

● Information in the form of text is normally represented by


means of a code in which each of the text is assigned a
unique bit pattern.
● The text is then represented as a long string of bits in
which the successive symbols in the original text.

● ASCII – American Standard Code for Information


Interchange
● A portion of ASCII is an eight bit per symbol format.
Representing Text
Representing Text

● Unicode was developed through the


cooperating of several of the leading
manufactures of hardware and software
● It uses a unique pattern of 16 bits to represent
each symbol.
● It consist of 65,536 different bit pattens.
Representing Numeric Values
○ Binary Notation
○ 1. Fraction in binary
○ Two’s complement notation
○ 1. Negative value
○ 2. Length of the bit pattern
○ Excess notation
○ 1. Length of the bit pattern
○ Floating point notation
○ 1. Different field (sign, exponent, mantissa)
○ 2. Radix point
○ 3. Sign bit
○ 4. Use 1 bytes, which is 8 bit length
Binary Representation

● Computer store and process all information in binary, a


series bits, 1s and 0s.
➢ Bit – Smallest element, either 0 and 1.
➢ Byte – Grouping of 8 bits, 1101 0011
➢ Kilobyte – 2^10 = 1024 bytes
➢ Megabyte – 2^20 = 1048576 bytes
➢ Gigabyte – 2^30 bytes
➢ Terabyte – 2^40 bytes
Decimal Numbers
Other Number System
Conversion Among Bases
Binary

● How many numbers can be represented by a 4 digits


decimal numbers?
● 1-9999 (10^5 - 1), 10^5 different number
● Binary 1-1111 (2^5 - 1) = 2^5 different number
Signed and Unsigned Numbers
Two’s Complement
● Two’s complement is a binary representation usually used to represent positive and negative
integers.
● Unlike the previous representation, every value has a single representation.
● First bit tell you whether given bit is positive – 0 or negative - 1.
● 7(2^4 - 1) is the largest possible number representable in 4 bit 2’s complement (otherwise the
first bit would be a 1).
● Positive number
0111 - 7
0010 - 2
● Negative number
1. 1 means negative number
2. (1. binary representation, 2. 1st complement, 3. 2nd complement)
eg. 1111 = -0001 = -1
1110 = -0010 = -2
Two’s Complement
Range
Binary Values
Floating Point Number
● Floating point numbers (number that can have a decimal point) eg. 0.15,
0.4456 required more complex representation that we won’t get into
Unicode

● A character set that has been design to store the


character for all written language
● More bit required to stored the characters
Strings
Relationship among four kinds of coding system
Storing Integer

● Two’s complement notation


The most popular means of representing integer values.
Addition problems converted to two’s complement
notation
Storing Fractions

● Floating point notation


The storage of a value with a fractional part required that we
store not only the pattern of 0s and 1s representing its binary
representation but also the position of the radix point. A
popular way of doing this is based on scientific notation and
is called floating point notation.
Storing Fractions
● Scientific notation
N = 110.011 = 1.10011 * 2^10 = 11001.1 * 2^-10 = 0.110011 * 2^+11

Floating Point Notation consist of two fields:


1. The exponent field
2. The mantissa field

Single precision floating point: 32 bits, a precision of 7decimal digits.


Double precision floating point: 64 bits, a precision of 15 decimal digits.
The storage of single precision floating point

Exponent Field Mantissa Field


(1 Byte) (3 Byte)
Sign of Exponent Sign of Mantissa Mantissa
Exponent

1 Bit 7 Bit 1 Bit 23 Bit

0 0000101 0 110101000000000000000
00
Excess Notation
Excess Eight Notation
What Is Computer Science?

● It is fundamentally about computational problem solving


● i.e., solving problems by the use of computation.
What is computation?

● It is any type of calculation that includes both


arithmetical and non-arithmetical steps and follows a well-
defined model
• E.g.: Algorithm.
Types of Computational Problems

1. Decision Problems
2. Search Problems
3. Counting Problems
4. Optimization Problems
Computational thinking can be split into
four parts,

1. Decomposition
2. Pattern recognition

3. Abstraction

4. Algorithmic design
• Computational thinking allows us,
1. To take a complex problem
2. Understand what the problem is
3. Develop possible solutions
4. Present these solutions in a way that a computer, a human, or both, can
understand.

• In order to solve a problem computationally, two things are needed,


1. A representation that captures all the relevant aspects of the problem
2. An algorithm that solves the problem by use of there presentation
What Is an Algorithm?

• It is a finite number of clearly described, unambiguous


“doable” steps that can be systematically followed to produce a
desired result for given input in a finite amount of time
• There are two main ways that algorithms can
be represented,
1. Pseudocode
2. Flowcharts
Pseudocode

• “Pseudo” -> Imitation or False


• “code” -> Instructions written in a programming language
• Pseudocode -> will be a representation that almost looks like a code
written in a programming language.
• It is also called Program Design Language (PDL)
• There are several formats which are used to write pseudo-codes and
most of them take down the structures from languages such as C, Lisp,
FORTRAN, etc.
• It allows you to include several control structures such as While, If-then-
else, Repeat-until, for and case, which is present in many high-level
languages
• It should be concise and the keyword should be in capital letter.
Flowchart

•It is the graphical or pictorial representation of an algorithm


General Rules for flowcharting
• Only one flow line is used in conjunction with terminal symbol.
• Only one flow line should come out from a process symbol.
• Only one flow line should enter a decision symbol, but two or three flow lines, one for
each possible answer, should leave the decision symbol.
General Rules for flowcharting
• Write within standard symbols briefly. As necessary, you can
use the annotation symbol to describe dataor computational
steps more clearly.
• Avoid the intersection of flow.
• If the flowchart becomes complex, it is better to use connector
symbols to reduce the number of flowlines
• Ensure that the flowchart has a logical start and finish
• It is useful to test the validity of the flowchart bypassing
through it with a simple test data.
Building Block of Algorithm
1. Sequence structure

2. Decision Structure or Selection Structure

3. Repetition or Iteration Structure


Sequence structure
• This is the most elementary structure
• In this, the steps in an algorithm are constructed in such a
way that, no condition step is required
• It is the logical equivalent of a straight line.
SEQUENCE
1.Compute the area and perimeter of a rectangle.
2.Accept three numbers from the user and find
the average.
3.Evaluate the expression ((a*b) + (c / d))-f) by
getting the values for each variable.
4.Calculate the distance walked by a person
around a circular ground with a diameter of100m.
(distance=πd)
SELECTION
1.Find the biggest among 2 numbers.
2.Find whether the given number is positive or negative.
3. Find the biggest among 3 numbers.
4.Find all roots of a quadratic equation
a x 2+bx+c=0.5. Read a number from the user. If the num is between 0 and 100,
inclusive then display the message 'within range' else display the message 'out of
range' otherwise.
ITERATION
1.Display 1 ton natural numbers.
2.Find the factorial of a number entered by user.
3.Find the sum of integers between given two
ranges.
4.Find the sum of numbers until the sum is less than
100.
5.Check whether a number entered by user is prime
or not.
Decision Structure or Selection Structure
Repetition or Iteration Structure

●This structure causes the certain steps to be repeated


●•The Repetition structure can be implementedusing,
➢The While Loop
➢The For Loop
➢Repeat Until Loop
●•Any program instruction that repeats somestatement or sequence of
statements a number oftimes is called
● iteration or a loop
● •The commands used to create iterations or loopsare all based on
● logical tests

● Pseudocode: (WHILE loop)
● WHILE (a condition is true)
● A statement or block ofstatements
● END WHILE

● Pseudocode:(DOWHILE loop)
● DO
● A statement or block of statements
● WHILE (a condition is true)

● Pseudocode
● (FOR loop):
● FOR (Initialization, stopping
condition, increment/decrement)
● Statements
● END FOR

● Pseudocode
● (REPEAT UNTIL):
● REPEATA statement or block of statements
● UNTIL condition

Problem:To print from 1 to 20

You might also like