Fundamentals of Programming: Part 4 – Variables and Arrays

by
Inf

Welcome to Part 4 where we will talk about variables and arrays are. If you’ve read the previous 3 parts, you would have noticed that I’ve used a few variables already, like A, B, Tc and Tf for example. In this section, we go deeper into what variables are, how to use them and finally, how to cope with situations where you have lots of variables to use, or don’t really know how many you need in fact!

So let’s begin this fairly short part, shall we?

Variables

If you’ve studied Mathematics before, you should already know what variables are. You must have encountered questions that involved X and Y. For example, “X apples cost 10. How many do 2X apples cost?” or much more complex questions involving “dy/dx” etc.

Variables in Maths, as the name suggests, represents a piece of information that can change i.e. it is variable. It acts as a place-holder for various values and you can plug-in different values in a variable and it will change  the result at the end.

For example, “X + 2″. If I say, X = 1. The result is 3. If now X = 8, the result is 10. If X = 20, the result is 22. The result will change depending on the value you give to the variable X.

Similarly in programming, you need those “placeholders”. For example, if you ask the user to input two numbers, you need somewhere to store those. You’d put them in two variables, which you can use later. You don’t really care which values the user inputs. Whatever they will input, they will go in two variables you define, say A and B.

Then in your calculation, you can use the inputs directly, without caring about their values. Variables can be used at all stages of a program. They can store user inputs, store intermediate results during processing or store results of processing before output. They are an essential component of any program.

What to add them? A+B. Multiply them? A*B. Divide? A/B. Did you notice something? The symbols are a bit different from normal mathematics. Let’s check that for a moment:

Addition is +. Subtraction is -. Those two are same as in Maths. However, multiplication is represented by an asterisk, *. Division is represented by a slash. Power is represented by ^. And depending on context, % can either represent percentage, or more often, modulo (the remainder of a division operation).

Back to our variables. So just to make it clear, variables are placeholders that will eventually store values or results of operations. You name variables and can then access their values by this name. Do notice, YOU name variables so be sure to use meaningful names for your variables! This is really important if you don’t want to get lost in your program. Unless in very specific cases, don’t use names like A, B or X. Instead, use names like “firstinput”, “second input” and “result”. It makes this clearer when you’re reading code. This is called “self-documenting” code, code which is clear just by reading it.

Here’s an example where variables are used:


Define variables: firstnum, secondnum

Input two numbers: firstnum, secondnum

resultadd = firstnum + secondnum

resultsub = firstnum - secondnum

resultmult = firstnum * secondnum

resultdiv = firstnum / secondnum

Output resultadd, resultsub, resultmult, resultdiv

We made use of 6 variables, two numbers and four to store the result of four operations. We then output them to the user.

Check the first line. We used a line: define. Before you use variables, you have to define them, i.e create them. It’s like that in most programming languages: you define variables before you can use them otherwise the compiler will complain bitterly! Not all languages force this, but it’s good-practice to define all your variables before you use them. Normally you will also specify the data type of the variable too, but we’ll see that in the next part.

Do you notice something? The variable names are rather hard to understand. “resultmult”? “secondnum”? There are conventions for variable formats, and most programming languages consider variables to be case-sensitive. That is, if you write a variable name all in caps and the same variable all in lower-case, they’d be two different variables. “NAME”, “name”, “Name”, “nAmE” would all be different variables. Therefore, there should be conventions to make sure everyone is writing the same way.

The two most commonly used ways are: camel-casing and underscore:

Here’s how you name variables for camel-casing: the first letter of the first word is lowercase. All other words have their first letter as uppercase. There are no spaces. E.g. “thisIsAVariable”, “anotherVariable”. If there is a single word, it starts with just a lowercase e.g. “variable”.

If you’re using underscore, you use all lowercase letters and separate multi-words with underscores. Thus, “this_is_a_variable”, “another_variable”, “variable”.

Whichever you use, stick to it. Normally, most people use camel-casing since it takes less space and is generally easier to type than having to reach the underscore button. But again, your choice unless imposed by a project.

So just to summarize, variables are placeholders for data needed by your program. Name them correcly to make sure it’s clear what they’re storing. Stick to conventions.

Arrays

Now, arrays. What are they? You can think of them as many variables together. Why would you need that? Well, what if I wanted the user to input 100 numbers? Would you create 100 variables, like “firstNum”, “secondNum”, “thirdNum” etc up to “hundredthNum”? Ok good luck if you want to try that. How about adding them all? Will you go re-writing each one?

Nah, you need arrays. Think of arrays as a series of variables stuck together, they’re reachable under a single name.

You can visualize an array like this [ ] [ ] [ ] [ ] [ ]. This would represent an array with 5 slots, and each of those slots can be thought of as a variable in itself. Like variables, arrays also have names and you reference to a particular slot inside an array by number. Array slots usually start from zero up to whatever you define.

So for example, if I create an array of 5 slots, called myArray, its slots will be o, 1, 2, 3, 4. For a total of 5 slots. If I wanted to get the 3rd slot of the array? I’ll reference it like this: myArray[2]. Huh, 2? But you said 3rd slot! It’s correct! Remember, you start from zero! The nth slot is referred to as n-1. Confusing huh? Don’t worry, you’ll get used to it. We are all confused at the start. (Arrays are zero-indexed.)

So if we wanted to re-write that program above, but instead use arrays for the two numbers input and arrays for the results too, it’ll be like this:


Define arrays: myNum[2], result[4]

Input two numbers: myNum[0], myNum[1]

result[0] = myNum[0] + myNum[1]

result[1] = myNum[0] - myNum[1]

result[2] = myNum[0] * myNum[1]

result[3] = myNum[0] / myNum[1]

Output result[0], result[1], result[2], result[3]

Whoa, that’s a lot to write. Copy-paste is your friend! No really, it is. Use it! That’s what I did.

Check the 1st line. We defined our two arrays. The numbers in this case represent the size of the array. That is, we said, we want two arrays, one having 2 slots and one having 4. Just like variables, you will need to define your arrays and their size before you can start using them.

Of course, there are situations when you don’t really how know big an array you need. Languages provide various mechanisms to cover this and we won’t do that here since this is a “fundamentals” series. But if you do want more information, go look for “dynamic arrays” and “arraylists” as a start.

If you want to go into arrays, Inception style, you can have arrays inside arrays… Yes! Arrays inside arrays inside arrays inside arrays inside arrays too if you want, although, that would be a bit too much. Normally, we only use arrays-in-arrays to represent tables. Conceptually, it’d look like this:


       0     1    2              0     1     2
0 [  [ a ]  [ ]  [ ]  ]   1 [  [ b ]  [ ]  [ c ]  ]

or if that helps, vertically:

       0     1     2

0 [  [ a ]  [ ]  [   ]  ]

1 [  [ b ]  [ ]  [ c ]  ]

How would you go about referencing the various slots? Well, two brackets. Let’s say this array is called “myArray”.

To reference “a” you reference the outer array first, then the inner one. Thus:

a would be: myArray[0][0]

b would be: myArray[1][0]

c would be: myArray[1][2]

What you just saw above is an array of size 3 inside an array of size 2.To define it, you’d call: myArray[2][3]. Thus, an array of size 2, with an array of size 3 inside. Want more? myArray[2][3][4]. An array of size 2, containing an array of size 3, containing an array of size 4. That’d be a 3-dimensional table. Too complex for this series! Those are called multi-dimensional arrays and are pretty useful to store, as you can guess, tabular data. E.g. students and their corresponding marks. It’ll be something like this, for simplicity:


[  [ "John" ]  [ 84 ]  ]

[  [ "Ann" ]   [ 89 ]   ]

But above, you see how arrays can be used to store inputs and results for output. They’re not hard to understand and use once you’ve done it a couple of times.

As you can guess, arrays have data types too, and you will need to specify it before you can put any data inside. Some languages restrict arrays so that an array can only contain a single data type. Others don’t care and you can stuff whatever you want in an array. We’ll get to the basics of that in the next part on data types.

Careful when using arrays. If you reference a slot that doesn’t exist or has no data yet, you might either get an error or get junk data. So check where you’re reading from or writing to to avoid weirdness.

Finally, loops are particularly useful when working with large arrays but this will be for another part!

I guess that’s it for this part on Variables and Arrays. If you have questions, suggestion etc, just ask in the comments section below. Hope this section was informational for you, and see you next time.

[seriesposts title="In the same series:" titletag="h3" listtype=ul show_date=0]