Scriviamo un algoritmo che, dato in ingresso un numero num, restituisca il numero di divisori pari di num.
Vectors & 2D matrices in the Flowgorithm DOWLOAD Only one dimensional arrays are supported in the Flowgorithm and limited to the integer, real (floats), boolean and string types. However, two dimensional arrays are natively required in many algorithms.
L’algoritmo è molto semplice:
When the computer needs to “decide” which branch of a flowchart (or algorithm to follow) it evaluates a variable against some condition that we place on it. These decisions are frequently documented in a condition/action table.For instance for the decision below: if age >= 18 we could document it like this:
path | condition | Action |
---|---|---|
User is 18 or older | age >= 18 | Print “Go vote!” |
User is less than 18 | age < 18 | Print “Sorry, not yet” |
The diamond shape is used to designate a decision. The “if” is not listed, just the condition.
In the case of multiple conditions that need to be met, we would use Boolean operators to chain two or more conditions together. Boolean operators:
For instance for the decision below: if age >= 18 AND enrolled = true we could document it like this:
path | condition | Action |
---|---|---|
User is 18 or older | age > = 18 AND enrolled = true | Print “Go vote!” |
User is less than 18 or not enrolled | age < 18 OR enrolled = false | Print “Sorry, not yet” |
Many times we need to force the user to enter a certain value that we are looking for. This is especially true when we need to validate the users input before continuing on with the program.
The != operator (not equal) is very useful for this task.The flowchart below asks the user to enter a specific character from the menu. If the condition is met (the user didn’t enter an A, B or C) the true path is chosen. If the condition is not met (user entered an A, B or C) the false path is taken.
Note: We use AND rather than OR here because all conditions need to evaluate to true. For example, they didn’t enter any of the valid inputs (A, B and C).
At times we may need to test for a certain condition, and then run a further test on another condition. However, if we want the program to be able to keep track of these individual evaluations we will need a nested conditional statement.
Think of this as an if statement inside another if statement.
In a previous section we were evaluating if a person was old enough to vote AND was enrolled to vote. The condition action table looked like the following:
path | condition | Action |
---|---|---|
User is 18 or older | age > = 18 AND enrolled = true | Print “Go vote!” |
User is less than 18 or not enrolled | age < 18 OR enrolled = false | Print “Sorry, not yet” |
In this case we can only respond to the two conditions:
If we want to be able target each individual condition, we will need a nested structure.
Path | Condition | Action |
---|---|---|
User is 18 or older and enrolled | age >= 18 AND enrolled = true | Print “Go vote!” |
User is 18 or older and is not enrolled | age >= 18 AND enrolled = false Print | “You are old enough to vote, but are not enrolled.” |
User is not 18 or older and is enrolled | age < 18 AND enrolled = true | Print “You are not yet 18 and are enrolled” |
User is not 18 or older and is not enrolled | age < 18 18 AND enrolled = false | Print “You are not yet 18 and are not enrolled” |