Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
100% found this document useful (1 vote)
2K views

Algorithm Programming

Here is a flowchart for the solution: [FLOWCHART] Start Input cost per kilo Input kilos bought Calculate total cost = cost per kilo * kilos bought Output "Total cost: " total cost Stop

Uploaded by

rajeshkripal3945
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
2K views

Algorithm Programming

Here is a flowchart for the solution: [FLOWCHART] Start Input cost per kilo Input kilos bought Calculate total cost = cost per kilo * kilos bought Output "Total cost: " total cost Stop

Uploaded by

rajeshkripal3945
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 34

Programming In C

MCA-102(N2)

Algorithms and Flow Charts

Objectives of these slides:


 introduce C using a few
examples

Programming In C 1
What is an algorithm?
… a well-defined procedure that allows an agent
to solve a problem.

Note: often the agent is a computer or a robot…

Example algorithms
 Cooking a dish
 Making a peanut-butter jelly sandwich
 Shampooing hair
 Programming a VCR
 Making a pie

Programming In C 2
Example
Is this an algorithm?

 Step 1: Wet hair


 Step 2: Lather
 Step 3: Rinse
 Step 4: Repeat

Would you manage to wash your hair with


this algorithm? How about a robot? Why
(not)?
Programming In C 3
Algorithms

An algorithm must:

1. Be well-ordered and unambigous


2. Each operation must be effectively
computable
3. Terminate.

Programming In C 4
Example
Problem: Find and print the 100th prime number
 A prime number is a whole number not evenly divisible
by any other number other than 1 and itself

Algorithm (?):
1. Generate a list of all primi numbers L1, L2, L3,

2. Sort the list into ascending order
3. Print out the 100th element in this list

Is this an algorithm?

Programming In C 5
Algorithm for Programming
a VCR
 Step 1: If the clock and calendar are not correctly set, then go
to page 9 of the instruction manual and follow the instructions
before proceeding
 Step 2: Place a blank tape into the VCR tape slot
 Step 3: Repeat steps 4 through 7 for each program that you
wish to record, up to a maximum of 10 shows
 Step 4: Enter the channel number that you wish to record, and
press the button labeled CHAN
 Step 5: Enter the start time and press TIME-START
 Step 6: Enter the end time and press END-TIME
 Step 7: This completes the programming of one show. If you
do not wish to program anything else press END-PROG
 Step 8: Press te button labeles TIMER. Your VCR is ready to
record.

Programming In C 6
Types of Operations
 Basic operations
 Wet hair
 Rinse
 Turn on VCR

 Conditional operations
 If batter is too dry add water

 Repeat/looping operations
 Repeat step 1 and 2 three times
 Repeat steps 2,3,4,…10 until batter becomes
soft.

Programming In C 7
Algorithm
 How to come up with an algorithm?
 That is, how to discover an algorithm
underlying a problem?
 Problem solving

 How to represent an algorithm?


 In English??
 In a programming language??

Programming In C 8
Example
 Problem: Given two positive integers, compute their greatest
common divisor

 Euclid’s algorithm:
 Step 1: Get two positive integer values from the user

 Step 2: Assign M and N the value of the larger and smaller of


the two input values, respectively
 Step 3: Divide M by N, and call the remainder R

 Step 4: If R is not 0, then assign M the value of N, assign te


value of R, and return to step 2; otherwise, the greatest
common divisor is the value currently assigned to N

Programming In C 9
Coming up with
algorithms..
 How do people think????

 Puzzle:
 Before A, B, C and D ran a race they made the following
predictions:
 A predicted that B would win
 B predicted that D would be last
 C predicted that A would be third
 D predicted that A’s prediction would be correct.
 Only one of these predictions was true, and this was the
prediction made by the winner.
In what order did A, B, C, D finish the race?

Programming In C 10
Example
 Problem: Adding two n-digit numbers

7597831 +
1287525
-------------------
8885356

How would you write an algorithm to solve


this problem? Assume the basic operation
is adding one-digit numbers.
Programming In C 11
Expressing algorithms
 Is natural language good?
 For daily life, yes…but for CS is lacks structure and
would be hard to follow
 Too rich, ambiguous, depends on context

 How about a programming language?


 Good, but not when we try to solve a problem..we
want to think at an abstract level
 It shifts the emphasis from how to solve the
problem to tedious details of syntax and grammar.

Programming In C 12
Pseudocode
 Pseudocode = English but looks like programming

 Good compromise
 Simple, readable, no rules, don’t worry about punctuation.

 Lets you think at an abstract level about the problem.

 Contains only instructions that have a well-defined structure


and resemble programming languages

Programming In C 13
Pseudocode
 Basic (primitive) operations
 Read the input from user

 Print the output to the user

 Cary out basic arithmetical computations

 Conditional operations
 Execute an operation if a condition is true

 Repeat operations
 Execute a block of operation multiple times
until a certain condition is met

Programming In C 14
Variables
Variable
 A named memory location that can store a value
 Think of it as a box into which you can store a
value, and from which you can retrieve a value

Examples:

i M
Example of operations
 Set the value of i to 3
 Set the value of M to i*3 + 12
 Set the value of i to i+10

Programming In C 15
A model for visualizing an algorithm

Algorithm

Variables

Operations

An algorithm consists of operations that involve variables

Programming In C 16
Primitive operations
 Get input from user
 Get the value of x from user

 Assign values to variables using basic arithmetic


operations

 Set the value of x to 3


 Set the value of y to x/10
 Set the value of z to x +25

 Print output to user


 Print the value of y, z to the user

Programming In C 17
Example 1
Problem: For any three numbers input by the user,
compute their sum and average and output
them

Example of algorithm in pseudocode:

Variables: a,b,c, sum, avg


 Get the values of a, b, c from user
 Set avg to (a+b+c)/3
 Set sum to (a+b+c)
 Print sum and avg

Programming In C 18
Example 2
Problem: Given any value of radius from the user,
compute and print the circumference of a circle
with that radius

Algorithm in pseudocode:

variables: r, c
1. Get the value of r from user
2. Set c to 2 * pi * r
3. Print “The circumference of your circle is “ c

Programming In C 19
What is a flowchart?
 Logic diagram to describe each step
that the program must perform to
arrive at the solution.

 A popular logic tool used for showing


an algorithm in graphics form.

Programming In C 20
Continue flowchart
 Programmer prepares flowchart
before coding.
 Most common flowchart symbols are:

Logic
Direction Beginning/ Input/output comparison data
of end of a task operation operations. manipulation
logic flow Have one operations
entry and
e.g arithmetic
two exit
points

Programming In C 21
Purpose of Flowcharting:
 An aid in developing the logic of a
program.
 Verification that all possible
conditions have been considered in a
program.
 Provides means of communication
with others about the program.
 A guide in coding the program.
 Documentation for the program.
Programming In C 22
Example
 Olive Peach has a fruit stall in the local
market
 She wants a program which will calculate a
customer’s bill, given the cost per kilo of the
fruit purchased and the number of kilos bought
 The program should then print out the bill
 This problem will only deal with one purchase
per customer
 Input will be from the keyboard; output will be
to the screen using appropriate headers.

Programming In C 23
Solution (1)
 Analyse
 identify the problem
 Calculate the cost of fruit, given cost per kilo and the number of
kilos bought
 Design a solution (Do this on paper)
 A typical purchase might be bananas:
 Cost per kilo – 90c
 Kilos bought - 2
 Total cost of purchase = 90 * 2 = €1.80c

Programming In C 24
Solution (2)
 Design the output to be displayed
Olive Peach – Fruit Merchant
The Old Market
Cost per kilo = $0.90c
Kilos bought = 2
Total cost = $1.80c
 Sketch the interface design (form)
 Implement your solution in VB
 Debug and test
 Document each stage

Programming In C 25
Olive Peach: Flow
Diagram
Start

Get cost
per kilo

Get
weight

Calculate cost

Display bill
Programming In C 26
Desk Checking
 The process of testing the flowchart with
different data as input, and checking the
output.

 The test data should include nonstandard


data as well as typical data.

Programming In C 27
What is a Pseudocode?
 A program design technique that
uses English words.
 Has no formal syntactical rules.

Programming In C 28
Olive Peach: Pseudo
Code (1)
 Steps
 1 Get cost per kilo
 2 Get weight (in kilos)

 3 Calculate total cost

 4 Display the bill

Programming In C 29
Olive Peach: Pseudo
Code (2)
 Steps
 1 Get cost per kilo
 1.1 Prompt user to enter cost
 1.2 Read in the cost from the keyboard
 2 Get weight
 2.1 Prompt user to enter weight
 2.2 Read in the weight from the keyboard
 3 Calculate total cost
 3.1 Set total cost equal to weight multiplied by cost
 4 Display the bill
 4.1 Display bill headings
 4.2 Display bill details (the cost, weight and total cost)

Programming In C 30
What is a Hierarchy
Chart?
 Shows the overall program’s
structure.
 Describes what each part, or module,
of the program does.
 Also how each module relates to
other modules in the program.

Programming In C 31
Example of Hierarchy
Chart:

Olive Peach
program

Calculate
Get cost Get weight Display bill
total cost
per kilo

Programming In C 32
More than 1 item: Flow
Chart Start

Initialize item=1
total cost = 0

No
Are there more items

Yes
Get next cost per kilo

Get next weight

Calculate cost

Add cost to total cost

Increment item

Programming In C 33
More than 1 item
:Pseudocode
 Determine the total cost if more than
one item:
Do while there are more items
Get the next cost per kilo
Get the next weight
Calculate the cost
add it to total cost
Increment items
Loop
Display total cost

Programming In C 34

You might also like