Programming ? What is

35
What is Programming ? CheeZe : )

Transcript of Programming ? What is

What is Programming ?

CheeZe : )

AlgorithmEp.4 :

Contents

● What is Algorithm ?● Why Algorithm is Important?● Characteristics of Algorithm● Advantages of Algorithm● Disadvantage of Algorithm● How to Design an Algorithm● Flowchart (Algorithm Representation)● What is Pseudocode ?● Pseudocode Essentials● Let’s Practice !

What is Algorithm ?

An algorithm is a detailed step-by-step instruction set or formula for solving a problem or completing a task.

In computing, programmers write algorithms that instruct the computer how to perform a task.

In short, it is a set of instructions for accomplishing a task.

What is Algorithm ?

Set of rules to obtain the expected

output from the given input

Input Output

Algorithm

Why Algorithm is Important?

● It is a universal and common for everyone !

● It helps organizing thoughts and actions.

● It makes implementation process more precise for any programming language.

● A computer program is actually an implemented algorithm.

Characteristics of Algorithm

Characteristics of

an Algorithm

Well-Defined Input

Clear and Unambiguous

Language Independent

Well-Defined Output

Finite-ness

Feasible

Advantages of Algorithm● It is easy to understand and universal.

● Algorithm is a step-wise representation of a solution to a given problem.

● In Algorithm the problem is broken down into smaller pieces or steps. Therefore, it is easier for the programmer to convert it into an actual program.

Disadvantage of AlgorithmWriting an algorithm takes a long time so it is time-consuming.

(For pro only ! But for newbies, practice is necessary !)

How to Design an AlgorithmBefore writing an algorithm, you should be able to define these 5 factors (PICOS)

● Problem to be solved by this algorithm.

● Input to be taken to solve the problem.

● Constraints of the problem that must be considered while solving the problem.

● Output to be expected when the problem the is solved.

● Solution to this problem, in the given constraints.

How to Design an Algorithm

Very Simple Example: add 2 numbers and print the sum.

Step 1 : Fulfilling the PICOS

● Problem: Add 2 numbers and print the sum

● Input: 2 numbers

● Constraints: The numbers must be digits only.

● Output: The sum of the 2 inputted numbers

● Solution: Using + (add) operator or sum function.

How to Design an Algorithm

Step 2 : Designing the algorithm

Algorithm to add 2 numbers and print their sum:

1. Start

2. Take 2 integer numbers as inputs and assign in 2 variables

3. Add the 2 numbers and store the result in a new variable

4. Print the value of variable sum.

5. End

Flowchart (Algorithm Representation)

● A diagram that illustrates the steps and structure of an algorithm

● Using symbols to represent the actions.

Basic Flowchart Symbols

● The oval, or terminator, is used to represent the start and end of a process.

● Use it at the very beginning of a flowchart.

● Remember to use the same symbol again to show that your flowchart is complete.

The Oval (terminator)

An beginning or ending

Terminator

Basic Flowchart Symbols

● It represents any step in the process you’re diagramming

● It is used the most in a flowchart.(if you are not sure which symbol to use in the flowchart the Rectangle is the go-to symbol)

● Use rectangles to capture process steps.

The Rectangle

A Step in the Flowcharting Process

Process

Basic Flowchart Symbols

● The arrow is used to guide the reader to follow along the flowcharting path.

● It is used indicate the direction of the progression of the program (e.g. What is the next step ?)The Arrows

Indicate directional flows

Arrows

Basic Flowchart Symbols

● The diamond symbolizes that a

decision is required to move forward.

● This could be a binary

(yes / no or True / False choice)

or a more complex decision with

multiple choices. The Diamond

Indicate a decision

Decision

Basic Flowchart Symbols

● It’s simply used for representing inputs

and outputs .

● Input and output symbols show where

and how data is coming in and out

throughout your process.

The Parallelogram

Input/Output

Input/Output

Flowchart ExampleAlgorithm to add 2 numbers and

print their sum:

1. Start

2. Take 2 integer numbers as inputs and

assign in 2 variables

Add the 2 numbers and store the result

in a new variable

3. Print the value of variable sum.

4. End

Start

Get input num1

Get input num2

Sum = num1+num2

Print sum

End

What is Pseudocode ?

● Pseudocode is a simplified, half-English, half-code outline of a computer program.

● It is a method of writing a set of instructions for a computer program using Human language with programming syntax

Pseudocode Essentials

Syntax Topic Pseudocode Result

Assignment INITIALIZE x = 10

SET name = “CheeZe”

● Initialize value 0 to variable x

● Assign value “CheeZe” to variable name

Output to screen OUTPUT(“Hello World”) Output “Hello World” to screen

Input from keyboard SET name = INPUT(“Enter name”) ● Output “Enter name” to screen.

● User types text ● The text is stored in

variable name

Pseudocode Essentials

Syntax Topic Pseudocode Result

Selection IF conditional statement 1 statement list 1ELSE IF conditional statement 2 statement list 2ELSE statement list 3

● If the choice meets the condition in statement 1 then do according to statement list 1

● else if the choice meets the condition in statement 2 then do according to statement list 2

● else, do according to statement list 3

Pseudocode EssentialsSyntax Topic Pseudocode Result

Iteration (for loop) FOR counter = 1 to 10 statement list

Do according to the statement list for 10 times

Iteration (while loop)

WHILE conditional statement statement list

While the conditional statement is met, do according to the statement list

Function FUNCTION functionname(argument) statement list return (return value)

Create a function that takes argument to perform task according to the statement list and return some values.

Function call CALL functionname(argument) Call a function with given argument

Pseudocode Essentials

Syntax Topic Pseudocode Result

Logical and comparison operators

AND OR NOT==!= < <= > >=

● And● Or● Not● Equal to● Not equal to● Less than● Less than or equal to ● Greater than● Greater than or equal to

Pseudocode EssentialsSyntax Topic Pseudocode Result

Arithmetic operators +-*/MODDIV^

● Addition

● Subtraction

● Multiplication

● Division

● Modulus (Remainder)

● Quotient/ Floor division

● Exponentiation/ power of

Casting INT(x)FLOAT(x)STRING(x)

● Cast value of x to integer type

● Cast value of x to float type

● Cast value of x to string type

Pseudocode ExampleAlgorithm to add 2 numbers and print their sum: 1. Start

2. Take 2 integer numbers as inputs and

assign in 2 variables

(e.g. num1 and num2, respectively)

3. Add the 2 numbers and assign

the result in a new variable

(e.g. total)

4. Print the value of variable total.

5. End

SET num1 = INPUT(“Enter 1st number: ”)

SET num2 = INPUT(“Enter 2nd number: ”)

SET total = num1 + num2

OUTPUT(total)

Let’s practice !!!!

1. Write a simple program that converts from Fahrenheit to Celsius or from Celsius to Fahrenheit, depending on the user's choice.(Simple Fahrenheit-Celsius Conversion)

2. Write a program with a function that can choose a random number between min and max value according to the user’s inputs.(Randomizer)

3. Write a program in which a password is set and the program will keep prompting the user to guess it, until they get the word right.(Password)

Simple Fahrenheit-Celsius Conversion

Write a simple program that converts from Fahrenheit to Celsius or from Celsius to Fahrenheit, depending on the user's choice.PICOS

● Problem: Converting Fahrenheit-Celsius

● Input: choice (F-to-C or C-to-F) and temperature to be converted

● Constraints: digit only

● Output: converted temperature

● Solution: Formula for F-to-C: ctemp =(ftemp-32)/1.8

Formula for C-to-F: ftemp =((ctemp*1.8)+32)

Simple Fahrenheit-Celsius Conversion

SET choice = INPUT("Press 1 to convert from Fahrenheit to Celsius or Press 2 to convert from Celsius to Fahrenheit.")

SET temp = INPUT("Enter temperature to be converted: ")

IF choice == 1SET result = (temp-32)/1.8

ELSE IF choice == 2 SET result = (temp*1.8)+32

FLOAT(result)OUTPUT(result)

RandomizerWrite a program with a function that can choose a random number between min and max value according to the user’s inputs.

PICOS● Problem: Randomly pick a number for given min and max value

● Input: min value and max value

● Constraints: digit only, function needed

● Output: a random number

● Solution: create a function to return a random number from min and max

Randomizer

FUNCTION Randomizer(minValue, maxValue)SET result = a random number between minValue and maxValueRETURN result

if choice == 1do fahrenheit to celsius conversion with given tempand formula (temp-32)/1.8)assign the result to outputprint output

else if choice == 2 do celsius to fahrenheit conversion with given tempand formula (temp*1.8)+32)assign the result to outputprint output

elseprint "Please enter either 1 or 2"

SET minValue = INPUT(“Enter the min value”)

SET maxValue = INPUT(“Enter the max value”

CALL Randomizer(minValue, maxValue)

OUTPUT(“The random number is”, randomValue)

SET randomValue = result from function

Guessing PasswordWrite a program in which a password is set and the program will keep prompting the user to guess it, until they get the word right and print out the total number of guess.

PICOS● Problem: Keep asking for password until the correct password is inputted and count

the number of user’s guesses

● Input: password that the user inputted

● Constraints: the actual password (HelloWorld!)

● Output: total number of guesses and actual password

● Solution: Use WHILE loop and use IF to check whether the guessed password is

equal to the actual password or not until the password is correct.

Guessing Password

SET password = “OpenSesame!”

SET userGuess = INPUT(“Enter your password”)

WHILE userGuess != password

INITIALIZE counter = 0

OUTPUT(”Access Granted !”)OUTPUT(“Total number of guesses is ”,counter)

SET counter = counter+1

OUTPUT(”Access Denied, please try again”)

SET userGuess = INPUT(“Enter your password again”)

IF userGuess == password

SET counter = counter+1

Special Exercise

Let’s try !BMI calculation !

TO-DO-LIST● Get their height● Get their weight● Calculate their BMI using this following formula

(weight (kg) / height (cm) / height (cm)) * 10,000

● Display their BMI

BMI Calculation

PICOS

● Problem: Calculate user’s BMI

● Input: User’s weight and height

● Constraints: integer-type-only inputs (in kg and cm)

● Output: User’s BMI

● Solution: Use formula

(weight (kg) / height (cm) / height (cm)) * 10,000

BMI Calculation

SET weight = INPUT(“Enter your weight (kg)”)

if choice == 1do fahrenheit to celsius conversion with given tempand formula (temp-32)/1.8)assign the result to outputprint output

else if choice == 2 do celsius to fahrenheit conversion with given tempand formula (temp*1.8)+32)assign the result to outputprint output

elseprint "Please enter either 1 or 2"

SET result = (weight/height/height)*10000

OUTPUT(“Your BMI is: ”, result)

SET height = INPUT(“Enter your height (cm)”)