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

Programming Fundamentals

CS-1001
Lecture 02

Dr. Muhammad Mobeen Movania


Outlines
• What is programming?
• The programming process
• What is an algorithm
• Examples of algorithms
• What is pseudocode
• Examples of pseudocode
What is programming
• A program is a set of instructions that the computer
follows to perform a task
• A computer only understands machine language which
is difficult for humans
• To enable humans to program computer, high level
languages were introduced
• These languages introduce simple constructs that are
then converted by a compiler to machine language
• The process whereby a program is written to enable a
computer to perform a specific task is called
programming
The programming process
• We do not start straight with coding
• For a given problem, we first develop an algorithm that
details the steps required to solve a given problem
• An algorithm is a list of well defined steps required to
solve a given problem
– It is very descriptive and is much like spoken language
• A pseudocode is an informal high-level description of
the operating principle of a computer program or
algorithm
• A flow chart is a pictorial representation of an
algorithm or pseudocode. It uses symbols to detail the
programs control flow
What is an Algorithm?
• An algorithm is a self-contained step-by-step set of
operations to be performed
• Algorithms perform calculation, data processing, and
automated reasoning
• An algorithm is an effective method expressed as a
finite list of well-defined instructions for calculating a
function.
– Starting from an initial state and initial input, the
instructions describe a computation that, when executed,
proceeds through a finite number of well-defined
successive states, eventually producing output and
terminating at a final ending state

http://en.wikipedia.org/wiki/Algorithm
Algorithm – Example 1
Problem: Given a number, output if the number
is Even or Odd
Algorithm: Find if a given number is even or
odd
1. Get the input number from the user
2. If the number is evenly divisible by 2, output
“Even”
3. Otherwise output “Odd”
Algorithm – Example 2
Problem: Find the largest number in a list of numbers
Algorithm: Find the largest number in a list of numbers
1. If the list is empty, there is no largest number
2. Assume the first number in the list is the largest number
in the list
3. For each remaining number in the list, if this number is
larger than the current largest number, consider this
number to be the largest number in the list
4. When there are no numbers left in the list to iterate
over, consider the current largest number to be the
largest number of the list
Short Exercise
• Write an algorithm to count total vowels in a
given string?
Answer
• We can give an algorithm for this problem as
follows:
1. Initialize count to 0
2. For each letter in the given string
1. Convert the letter to lower case
2. if the lowercase letter is one of ‘a’ or ‘e’ or ‘i’ or ‘o’ or
‘u’, increment count by 1
3. Output the value of count
What is Pseudocode?
• Pseudocode is an informal high-level
description of the operating principle of a
computer program or other algorithm
• It uses the structural conventions of a
programming language, but is intended for
human reading rather than machine reading
• Pseudocode generally does not obey the
syntax rules of any particular language; there
is no systematic standard form
http://en.wikipedia.org/wiki/Pseudocode
http://www.cs.cornell.edu/Courses/cs482/2003su/handouts/pseudocode.pdf
Inputs Assumed and Outputs Expected
• Preconditions
– The initial assumptions of the input (parameters)
that the pseudocode, algorithm or function
assumes

• Postconditions
– Are the expected outputs or return value from the
pseudocode, algorithm or function
Short Exercise
• Write pseudocode to find if a given number is
an even or odd?
Answer
Preconditions:
num is the given input number

Postconditions:
Outputs if the input is even or odd

If num mod 2 = 0
output “even”
Else
output “odd”
Pseudocode - Example 1
Problem: Find the largest number in a list of numbers
Preconditions:
list is the list of input number
N is the total numbers in the list

Postconditions:
returns the maximum number in the given list

if N = 0
output “No highest number”
max = list[1]
repeat for each number x in list
if x > max
max = x
end repeat
output max;
Pseudocode - Example 2
Problem: Calculate sum of a given set of numbers input by the user

Preconditions:
None
Postconditions:
returns the sum of all numbers given by the user

sum=0
N=0
output “Enter total numbers for input?”
input N
repeat N times
tmp = 0 //temp variable to store input
output “Enter number: ”
input tmp
sum = sum + tmp
end repeat
output sum
Next Lecture
• Flow Chart

You might also like