Pseudocode: Difference between revisions

From Computer Science Wiki
No edit summary
 
(39 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<center>
<blockquote style="padding: 5px; background-color: #FFF8DC; border: solid thin gray;">
  [[File:Exclamation.png]] This is an '''important concept''' you should fully understand this.
</blockquote>
</center>


[[File:pseudocode.png|frame|right|This is a basic concept in computer science]]
[[File:pseudocode.png|frame|right|This is a basic concept in computer science]]


In mathematics and digital electronics, a binary number is a number expressed in the binary numeral system or base-2 numeral system which represents numeric values using two different symbols: typically 0 (zero) and 1 (one). The base-2 system is a positional notation with a radix of 2. Because of its straightforward implementation in digital electronic circuitry using logic gates, the binary system is used internally by almost all modern computers and computer-based devices. Each digit is referred to as a bit.<ref>https://en.wikipedia.org/wiki/Binary_number</ref>
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 normal programming language, but is intended for human reading rather than machine reading.<ref>https://en.wikipedia.org/wiki/Pseudocode</ref>


Pseudocode is a simple way of writing programming code in English. Pseudocode is not actual programming language. It uses short phrases to write code for programs before you actually create it in a specific language. Once you know what the program is about and how it will function, then you can use pseudocode to create statements to achieve the required results for your program.<ref>http://study.com/academy/lesson/pseudocode-definition-examples-quiz.html</ref>


== Binary ==
This is one of the better videos I've seen on binary.


== Official IB pseudocode guide ==
There are two different versions of the IB Pseudocode guide:


<html>
* [[:media:Approved notation for developing pseudocode.pdf | Click here to view a file describing approved notation, including pseudocode This is the '''approved''' notation sheet from the IB.]]
<iframe width="560" height="315" src="https://www.youtube.com/embed/hacBFrgtQjQ" frameborder="0" allowfullscreen></iframe>
* [[:media:IB-Pseudocode-rules.pdf | Click here for a more in-depth pseudocode guide]]
</html>


== Binary Translation table ==  
== Some practice links ==  
I find it helpful to draw this table when I must convert binary to [[base 10]]. It also helps when looking at the video above.  
This is a SUPERB guide I have used with high gratitude from [https://www.qacps.org/ Queen Anne's County Public Schools]
{| style="width: 95%;" class="wikitable"
<br>
|-style="text-align:center;"
* [[:media:Pseudo Code Practice Problems.pdf|Click here for an excellent pseudocode practice guide]]
! '''128''' !!  '''64'''  !! '''32''' !! '''16''' !! '''8''' !! '''4''' !! '''2''' !! '''1'''
|-
|<br /> || || || || || || ||
|}


== How to add two binary numbers ==  
== Please remember ==  
Adding binary is straight forward. Line up the numbers as you would if you were adding base-10 numbers.


Remember this:
# Always open an IF  and close an IF as clearly as possible
# Always open a LOOP and close a LOOP as clearly as possible
# Always make your variable names meaningful and obvious. X isn't good but student_first_name is excellent.
# Always make sure you have comments to explain you logic and process.
# Ensure your pseudocode is legible
# Ensure your pseudocode is at the right level of depth


<code>
== Common psuedocode verbs ==
0 + 0 = 0
 
Common Action Keywords<ref>http://users.csc.calpoly.edu/~jdalbey/SWE/pdl_std.html</ref>
 
Several keywords are often used to indicate common input, output, and processing operations.
* '''Input:''' READ, OBTAIN, GET
* '''Output''': PRINT, DISPLAY, SHOW
* '''Compute:''' COMPUTE, CALCULATE, DETERMINE
* '''Initialize:''' SET, INIT
* '''Add one:''' INCREMENT, BUMP
 
== Examples of pseudocode ==


0 +  1 = 1


1 + 0 = 1  
<ref>http://www.unf.edu/~broggio/cop2221/2221pseu.htm</ref> Example 1:
<br />
<code>
If student's grade is greater than or equal to 60


1 + 1 = 10, so write a 0 and carry the 1 to the next column.
Print "passed"
else
Print "failed"
End if
</code>
</code>
<br />


== What you must know ==
Question for you to think about:
* What do you notice about the structure of this code?
* Could you program this in [[Python]] or [[PHP]]?


You must be able to correctly answer the following questions:


<div class="toccolours mw-collapsible mw-collapsed">
Define the term:  '''bit''' Click the expand link to see the answer, but make sure you have tried to answer first!--->
<div class="mw-collapsible-content">
<code>A bit is the basic unit of information in computing and digital communications</code>.  A bit can have only one of two values, and may therefore be physically implemented with a two-state device. These values are most commonly represented as either a 0 or 1. The term bit is a portmanteau of binary digit.<ref>https://en.wikipedia.org/wiki/Bit</ref>
</div>
</div>


<ref>http://www.unf.edu/~broggio/cop2221/2221pseu.htm</ref>  Example 2:
<br />
<code>
# Set total to zero
# Set grade counter to one
# While grade counter is less than or equal to ten
# Input the next grade
# Add the grade into the total
# end while
# Set the class average to the total divided by ten
Print the class average.
</code>


== Do you understand this? ==


<div class="toccolours mw-collapsible mw-collapsed">
Review the [[python]] code below and then re-type it in pseudocode.
Define the term:  '''byte''' Click the expand link to see the answer, but make sure you have tried to answer first!--->
<div class="mw-collapsible-content">
<code>The byte is a unit of digital information that most commonly consists of eight bits</code>. Historically, the byte was the number of bits used to encode a single character of text in a computer and for this reason it is the smallest addressable unit of memory in many computer architectures. The size of the byte has historically been hardware dependent and no definitive standards existed that mandated the size. The de facto standard of eight bits is a convenient power of two permitting the values 0 through 255 for one byte. The international standard IEC 80000-13 codified this common meaning. Many types of applications use information representable in eight or fewer bits and processor designers optimize for this common usage. The popularity of major commercial computing architectures has aided in the ubiquitous acceptance of the 8-bit size.<ref>https://en.wikipedia.org/wiki/Byte</ref>
</div>
</div>




<div class="toccolours mw-collapsible mw-collapsed">
Please paste and run this code into your Python [[IDE]] :
Define the term:  '''binary''' Click the expand link to see the answer, but make sure you have tried to answer first!--->
<div class="mw-collapsible-content">
<code>In mathematics and digital electronics, a binary number is a number expressed in the binary numeral system or base-2 numeral system which represents numeric values using two different symbols: typically 0 (zero) and 1 (one).</code><ref>https://en.wikipedia.org/wiki/Binary_number</ref>
</div>
</div>


<syntaxhighlight lang="python">
import random
secret_number = random.randint(0,100)
guess = 0
game = 1
while True:
    print("This is guess: " + str(guess))
    print("This is game: " + str(game))
    players_guess = int(input("I'm thinking of a number between 1 and 100. What is your guess: "))
    if players_guess == secret_number:
        print("You won!")
        play_again = input("Do you want to play again (Y or N)? ")
        if play_again == "Y" or play_again == "y" or play_again == "yes":
            guess == 0
            game = game + 1
            secret_number = random.randint(0,100)
        else:   
            break
    elif players_guess < secret_number:
        print("This guess is to low.")
        guess = guess + 1
    elif players_guess > secret_number:
        print("This guess is to high")
        guess = guess + 1
</syntaxhighlight>


<div class="toccolours mw-collapsible mw-collapsed">
Define the term:  '''denary/decimal''' (they refer to the same thing) Click the expand link to see the answer, but make sure you have tried to answer first!--->
<div class="mw-collapsible-content">
<code>Denary, also known as "decimal" or "base 10," is the standard number system used around the world. It uses ten digits (0, 1, 2, 3, 4, 5, 6, 7, 8, and 9) to represent all numbers.</code><ref>techterms.com/definition/denary</ref>
</div>
</div>




<div class="toccolours mw-collapsible mw-collapsed">
<div class="toccolours mw-collapsible mw-collapsed">
Define the term:  '''hexadecimal''' Click the expand link to see the answer, but make sure you have tried to answer first!--->
Click the expand link to see one possible solution, but NOT before you have tried and failed! The solution below was graciously provided by Emiko, an 8th grade student in 2016.
<div class="mw-collapsible-content">
<div class="mw-collapsible-content">
<code>In mathematics and computing, hexadecimal (also base 16, or hex) is a positional numeral system with a radix, or base, of 16. </code> It uses sixteen distinct symbols, most often the symbols 0–9 to represent values zero to nine, and A, B, C, D, E, F (or alternatively a, b, c, d, e, f) to represent values ten to fifteen.<ref>https://en.wikipedia.org/wiki/Hexadecimal</ref>
</div>
</div>


[[Media:Binary conversion.pdf | Click here to test yourself]]
== Why is this so important? ==
If we can represent numbers as '''1 and 0''', why not represent numbers as '''on and off'''? If we can represent letters as numbers (A = 65, B = 66) couldn't we also say A =  01000001 and B = 01000010?
Binary representation is the essence of how computers work.


<syntaxhighlight lang="python">
Pick a random number.
Set the secret number to a number that is between 0  and 100.
Set the number of guesses to start with.
Set the number of games to 1.
While true, print ("This is guess: " +(number of guesses) and print ("This is game: + (number of games).
Obtain the player's guess by asking for an input of their guess.
If the player guessed the secret number, print ("You won!") and ask "do you want to play again?"
If play again is "y", set guess back to zero and add one to the number of games played. Also, choose another different random secret
number between 0 and 100. Else, if play again is no, end or break the code.
If the player's guess is is lower than the secret number, print ("The guess is too low.") and add one to the number of guesses.
Else, if the number is higher than the secret number, print ("This guess is too high.") and add one to the number of guesses.
</syntaxhighlight>






</div>
</div>


== Why is this so important? ==


When you are first building a program, we don't really care about the actual LANGUAGE or SYNTAX you will use. Rather, we care about your logic. Pseudocode helps you by forcing you to think logically rather than about syntax.


== References ==  
== References ==  

Latest revision as of 16:03, 3 April 2024

This is a basic concept in computer science

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 normal programming language, but is intended for human reading rather than machine reading.[1]

Pseudocode is a simple way of writing programming code in English. Pseudocode is not actual programming language. It uses short phrases to write code for programs before you actually create it in a specific language. Once you know what the program is about and how it will function, then you can use pseudocode to create statements to achieve the required results for your program.[2]


Official IB pseudocode guide[edit]

There are two different versions of the IB Pseudocode guide:

Some practice links[edit]

This is a SUPERB guide I have used with high gratitude from Queen Anne's County Public Schools

Please remember[edit]

  1. Always open an IF and close an IF as clearly as possible
  2. Always open a LOOP and close a LOOP as clearly as possible
  3. Always make your variable names meaningful and obvious. X isn't good but student_first_name is excellent.
  4. Always make sure you have comments to explain you logic and process.
  5. Ensure your pseudocode is legible
  6. Ensure your pseudocode is at the right level of depth

Common psuedocode verbs[edit]

Common Action Keywords[3]

Several keywords are often used to indicate common input, output, and processing operations.

  • Input: READ, OBTAIN, GET
  • Output: PRINT, DISPLAY, SHOW
  • Compute: COMPUTE, CALCULATE, DETERMINE
  • Initialize: SET, INIT
  • Add one: INCREMENT, BUMP

Examples of pseudocode[edit]

[4] Example 1:
If student's grade is greater than or equal to 60

Print "passed" else Print "failed" End if

Question for you to think about:

  • What do you notice about the structure of this code?
  • Could you program this in Python or PHP?


[5] Example 2:

  1. Set total to zero
  2. Set grade counter to one
  3. While grade counter is less than or equal to ten
  4. Input the next grade
  5. Add the grade into the total
  6. end while
  7. Set the class average to the total divided by ten

Print the class average.

Do you understand this?[edit]

Review the python code below and then re-type it in pseudocode.


Please paste and run this code into your Python IDE :

import random
secret_number = random.randint(0,100)
guess = 0
game = 1 
while True: 
    print("This is guess: " + str(guess))
    print("This is game: " + str(game))
    players_guess = int(input("I'm thinking of a number between 1 and 100. What is your guess: "))
    if players_guess == secret_number:
        print("You won!")
        play_again = input("Do you want to play again (Y or N)? ")
        if play_again == "Y" or play_again == "y" or play_again == "yes":
            guess == 0
            game = game + 1
            secret_number = random.randint(0,100)
        else:    
            break
    elif players_guess < secret_number:
        print("This guess is to low.")
        guess = guess + 1
    elif players_guess > secret_number: 
         print("This guess is to high")
         guess = guess + 1


Click the expand link to see one possible solution, but NOT before you have tried and failed! The solution below was graciously provided by Emiko, an 8th grade student in 2016.


Pick a random number.
Set the secret number to a number that is between 0  and 100.
Set the number of guesses to start with.
Set the number of games to 1.
While true, print ("This is guess: " +(number of guesses) and print ("This is game: + (number of games).
Obtain the player's guess by asking for an input of their guess.
If the player guessed the secret number, print ("You won!") and ask "do you want to play again?"
If play again is "y", set guess back to zero and add one to the number of games played. Also, choose another different random secret
number between 0 and 100. Else, if play again is no, end or break the code.
If the player's guess is is lower than the secret number, print ("The guess is too low.") and add one to the number of guesses.
Else, if the number is higher than the secret number, print ("This guess is too high.") and add one to the number of guesses.


Why is this so important?[edit]

When you are first building a program, we don't really care about the actual LANGUAGE or SYNTAX you will use. Rather, we care about your logic. Pseudocode helps you by forcing you to think logically rather than about syntax.

References[edit]