Wednesday, January 17, 2018

A gentle re-introduction to programming



I assume that you have taken a class in programming before. And that you have passed the course, even if its with a certain hatred, even if you had passed with a certain sense of doubt as to why in the first place you chose to agree to take up programming. 

Ok, my page here is for haters and doubters. What I am setting out to do is to try to change you from haters/doubters to someone a little bit more positive, maybe even one who can actually do programming, somehow. At the very least, I hope to make you see it as something learnable, something surmountable. But I am making the assumption that you want to learn; it takes two to tango, and I can never do it alone. I want to teach, and you must want to learn. You must be willing to toil and burn, and do all the moves. My job will be to watch and to mentor.

So, let's begin. I will start with a simple question: What is programming all about? What have you learnt about programming? Or what you should have learnt about programming?




When you run a program, you are issuing instructions to a computer on how to process a certain set of input.




Hence, a program is just a sequence of instructions for the computer. But wait, do you really know what is a computer? 



It's certain not just a box. Not a box filled with wires and funny little boxes. How should we view a computer? The way we shall see it - it's a machine, a schematic diagram for it as shown below, the so-called von-Neumann architecture:




As you can see above, we have input and output devices, and we have the CPU and the memory unit playing a central role. The CPU executes instructions on data. Where sits all the instructions and data? Yes, in the memory unit. So the CPU fetches and executes one instruction at a time from the Memory Unit. And if the instructions require a piece of data, the data will also be retrieved from and/or written to the Memory Unit.

Ok, so far?

Now, consider this.. what sort of instructions are we talking about here? What sort of instructions that a computer can handle to do something?  Think about this: the instructions in a computer program must be arranged in a certain logical order. The instructions must perform a certain logic, in order to transform the input into the output. What sort of instructions would be sufficient to perform the logic required to perform a computational work or to solve a computational problem? What I mean by 'computational' here is basically something that can be done on a computer. Hence a computational problem is a problem that is solvable or do-able on a computer. Update of accounting figures can be done on a computer for example. Hence that is a computational problem or work. Falling in love is not a computational problem however (as yet). 

So back to the question: what sort of instructions must a computer be able to execute in order to be able to perform the sort of logic needed to solve a computational problem? It turns out 3 fundamental logical structure suffice to compose any computational solution:
  1. sequential: instructions are performed in a sequence monotonically
  2. conditional: instructions are performed based on certain conditions.
  3. repetitive: instructions are repetitively performed.
These 3 structures suffice in the sense that you use any combination of it to form the solution.


Let's take a look first at sequential..


As you can see here, a sequential structure is just one where the instructions are executed one after another from top to bottom.

Let's now on to repetitive structure.


Here we have a certain instruction, instruction 0 being executed repeatedly subject to the value of the repetition counter. The repetition counter here is i. After each iteration, i will be incremented by 1, and the iteration will repeat until i is 10.

Of course, more than one instruction may be repeated, as shown below:


In fact, as can be seen here, we are composing both sequential and repetitive structure together into a composite logical scheme. In the repetition structure, we have a sequence. Or we can even have a sequence involving a repetition,as shown below:




And finally, let's include a conditional structure:



So how do all these structures look like in C? Let's try something:


  1. Supposed we have a list of 10 numbers: 342, 12, 32, 98, 112, 54, 43, 33, 10, 123. Write a C program that compute the average of the numbers.
  2. Modify your solution for 1) so that only the average of the even numbers is calculated.
  3. Extend your solution for 1) so that it compute the standard deviation is calculated. The standard deviation is calculated as below:




The purpose of the exercises above is just to test your grasp of basic programming, and to help you to recall all those skills you might have learnt months ago! I really hope you are not having too much trouble with it :-D

You may have a little bit of problem with arrays. I talk about this in another post.

Now, let's move on to another concept - that of a function. What's a function?  Now I will re-introduce this concept to you via a problem. 
  • Read in a list of 10 numbers from the user, and display a simple text menu that offers the following functionality: a) compute the average b) the standard deviation c) compute the minimum, d) compute the maximum., e) read another 10 numbers, and f) quit

Can I write the solution for the above all in one function?   Yes, sure you can. But the outcome will likely be a clutter of code, one that's hard to read, especially after a fortnight. It would get you (or some other coder) confused, and imagine trying to issue instructions to a machine when you are in a state of confusion!




I'll go into modular programming in the next post. Let's take a break ok (ᵔᴥᵔ)
In the meantime (say, after dinner), take a look at this: https://www.embedded.com/design/prototyping-and-development/4023876/Modular-Programming-in-C





No comments:

Post a Comment