How code can solve problems
Computer programming is the act of telling a computer what to do. Without a program to follow (or ‘run’), a computer would do nothing. In this step, you’ll begin to explore what code can do.
A key skill for programming a computer is knowing how to write code in an appropriate language — but it’s not the only skill for being a good programmer. Before you even start to write the code to instruct the computer, you need to understand the problem you are asking the computer to solve. Then you need to break it down into a series of instructions, as you’ll see in the following example:
A problem with the temperature
An American friend is visiting Europe. They are confused by the weather forecast because all the temperatures are measured in degrees Celsius. They would rather know the temperature in degrees Fahrenheit.
You might know a set of instructions they can follow to solve that problem, for example:
- Take the temperature in Celsius
- Multiply it by nine
- Divide that answer by five
- Add 32
The weather forecast says it’s 29ºC. Our friend follows these instructions and realizes that it is 84ºF.
Introducing algorithms
A set of instructions, like the one above, is known in programming as an algorithm.
An algorithm takes some input (the temperature in ºC), follows the set of instructions, and returns an output (the temperature in ºF). An algorithm is unambiguous which means it shouldn’t be possible to perform it incorrectly. It is repeatable and supplying it identical input should return identical output.
When you program computers you are often trying to translate the problem you wish to solve into one or more algorithms, which we express in computer code.
More complex algorithms
More complex algorithms may have more inputs, more outputs or more complicated logic. Frequently, algorithms in computer programs become more complex in order for them to be more useful to people using them.
Think about finding the shortest route between two paths in a mapping app. The input and output for this algorithm are positions in the world. The simplest answer is just a mathematical one: drawing a straight line between the two. The result would look like this:
This is unambiguous and repeatable but it is not very practical. To make it more useful, you need more input and more complex logic.
You could improve the algorithm of finding the shortest route with more data input, as follows:
• Describe all the streets and paths between the two points as input. The logic should give an answer that only offers routes along roads. This is already more useful than the straight line.
• If the algorithm knew which roads were one-way, it could make sure it didn’t output a route that couldn’t be followed.
• If it knew what mode of transport was being used, it could offer a route that used cycle lanes or footpaths as appropriate, and offer an estimate for journey time.
• If it had live traffic information, it could use that to improve its time estimate.
All this adds complexity to the logic in the algorithm but it also makes it more useful to an end-user (the person who needs the answer to the problem).
Understanding a problem
The more complex routing algorithm shows the importance of understanding the problem being solved. The end-user isn’t really interested in the shortest distance between two points; they are interested in the shortest route they can actually use. The straight-line answer is correct for birds but useless for a cyclist or driver.
You can also now see how computer programs go wrong. Sometimes code doesn’t work because the code has mistakes or bugs in it because the algorithms are incorrect.
Sometimes code doesn’t work because it’s solving the wrong problem. The team building the code may not have understood the problem correctly, or not understood what the user wanted to do.
The work of programming isn’t just writing code — it’s understanding a problem from input to output. What are you really trying to solve? What would make your answer more accurate, or more useful? What do you need to know to make your algorithm right every time?
Reference
https://www.futurelearn.com/courses/computer-programming-for-everyone/4/steps/761943