Turing β-0.10 is now available!
- Fix NumPy integral types overflow bug
- User can enter input before end of animation
- Disable unsafe evaluation by default for input
- Inclusive range function was not inclusive
- NOT operator not working in certain cases
- Round function was incorrectly checking types
- Input wrapper was not passing kwargs
- Qt fixer was working incorrectly
- Allow parameterless functions in pseudocode
- Choice function
- Upper and Lower functions
- “Text-only” setting for input statements
- Display newlines after display statements
- Allow array access operator for strings
- String can now be compared
- New home page
- Tabs are now switched automatically when creating and opening a file
- “Clear Recent” button is now working
- Converted Python code is now formatted automatically
Turing β-0.9 is now available!
- Fix thread lock (UI is not locked anymore when program is running)
- Fix “grad” parameters of graph window
- New and Open buttons are now disabled when program is running
- The Linux binary is now compatible with older versions
- A macOS binary is now available
- Fix line number in error messages (was being calculated incorrectly)
- Fix auto step in FOR-loops (e.g. when bounds are reversed)
- Embed editor backend in main binary (now only one binary is required)
- Add new French arrow notation
- Add Candy theme
- Add important program feature enhancement
- Add inverse Erf and Erfc
- Add normal distribution functions
Turing β-0.8 is now available!
- Width of side panel was calculated using a method that broke the Linux build because of an obscure Qt bug
- Performance bottleneck caused by too frequent graph updates (now you can draw 1000’s of points without crashing)
- Theme engine + customizer
In this tutorial, we will write step-by-step a simple algorithm that will compute the sum of all numbers from 1 to a user-inputted number.
When you open Turing, you should see a blank window like in the screenshot above. Otherwise, click on New.
Before writing an algorithm, you must first think about how it will work ; which values it will take in input, and what it will return in output. A pencil and a sheet of paper are recommended, to make a quick draft of the global structure of the algorithm, although it’s not necessary for small algorithms such as the one we’ll write today.
Let’s have a look at the wording: “a simple algorithm that will compute the sum of all numbers from 1 to a user-inputted number”.
It is true that formulas exist to compute such sums (e.g. Gauss’ formula for integer summations), but here we will do it in a simpler and more easily understandable way.
We will use here what we call variables. Although that name is also used in maths, these are two fundamentally different things. In maths, a variable is a thing, whose value can’t change (but doesn’t need to be known nor unique), that contains a number, and that can/must verify constraints (that are called equations). In programming, a variable can be assimilated to a box in which we can put anything we want at any moment.
To recap: a variable is a thing in which we can put whatever we want and that we can modify whenever we want.
First, we’ll ask the user for a number ; which number shall we use to compute the sum. We’ll use for that the Read user INPUT instruction. Click on the corresponding button:
A window will then open:
The Read user INPUT instruction has two parameters:
- the variable in which we will put the value inputted by the user
- the message to display so that the user knows what we the value is for
The message is optional ; if it’s not specified, a generic message containing the variable name will be displayed instead.
The Variable field shall contain the name of the variable. A variable name can contain letters and digits (but can’t start with a digit) and can’t contain spaces. Here, we’ll use “x”.
Next, click on OK.
Your algorithm should look like this:
We’ll try it to make sure everything works. Click on Run.
You should see this in the Output window:
As you can see, the generic message has been displayed, and the text field has been enabled. Type a number, for example 5, and either press Enter or click on the little green button.
The value showed up, and the program stopped. Which is quite logical given that the program only contains one instructin.
We can move on to the next step, which will consist of computing the sum of the numbers from 1 up to x. We can think about it differently: for example, we’ll say that we initialize the “counter” to 0, and then add 1, then 2, then 3 and so on until we end by adding x to the “counter”.
That counter would pretty much be a box in which we put a number that we can modify whenever we want. Reminds you of something?
As you may have guessed, we’ll use a variable for that.
Click on the DEFINE variable button.
The instruction has two settings:
- the variable that we want to define
- the value to put in the variable
Here, we want to initialize the counter to zero. Click on OK. The algorithm should look like this:
Now that the counter is at zero, we must add 1, then 2, then 3 and so on up to x. Put step by step, it would pretty much look like this:
- add 1 to the counter
- add 2 to the counter
- add 3 to the counter
- add x to the counter
That’s repetitive. That’s very repetitive. That’s too repetitive.
You may have heard that mathematicians are lazy. That’s true. You may also have heard that computers were invented by mathematicians. Thus, we can conclude that developers are lazy. Lazy people don’t like repetitive things, because it’s repetitive. It’s always the same thing. It’s not interesting. For that reason, we invented a cool thing called loops. A loop is something that allows you to do the same thing multiple times.
There are different kinds of loops in programming. In algorithms, only two will be used:
- the FOR variable FROM begin TO end loop : executes the instructions for all values of variable between begin and end (adding 1 each time)
- the WHILE condition loop : executes the instructions while condition is true
You’ve guessed it, the one we’ll use here is the FOR loop. We’ll add one. Click on the Blocks tab and on the button FOR loop.
Here, current will start at 1 and end at x.
Click on OK. Your algorithm should look like this:
Next, we want that at each “turn”, it adds the value to the counter. So, at each turn, it will modify the variable. Select the line of the loop and click on DEFINE variable.
It means that for each value of current, we add current to counter. In other words, for each value of current, counter takes the value counter + current.
The algorithm should look like this:
That’s nice and all, but what do we do about counter?
We need to display it at the end of the program, otherwise it will just compute its value and forget about it. Click on DISPLAY value.
Warning: depending on how you do it, the line may be added like this:
That is not what we want, since that way it would display the value at each iteration of the loop (instead of just one time at the end). Select the line and click on the arrow in the upper right corner:
The line should get to the right location and the final algorithm should look like this:
We can now test it. Click on Run and try inputting some values. For example, the number 100 should give the output 5050.