Arrays
Arrays
For the past tutorials, we have been dealing with variables that store only a single value of a
particular datatype. But in reality, do we always do this or we need to store multiple items of
the same type in a single location so that we may reuse the whole pack of the items in the
future.
Lets say we want to store a list of names, numbers or IDs , should we create multiple
variables that match the whole list of the data available. We actually can, but with a lot of
work, and probably limitations when we want to perform some functions on our data. For
example, we want to store all items in a 3 X 3 matrix, or a 4 X 4 matrix, or actually find the
sum of 2 (n X n) matrices. We need to create a single variable that will store all the elements
of the first array in their correct positions and so in the second. For this we need a new to
create a new storage that has a single variable name. One of the things we can use is:
Arrays
What are arrays: An array is a collection of similar types of data.
We have used an array to actually store a string as we did when we wanted the user to input a
name or some text.
Here is the syntax of an array.
Promdee
If you are not sure about the size of the array, let’s say you array is going to be gilled by the
user, you just leave the size empty. The array will determine its own size once the array is
filled so that it matches the number of items in the array.
Leaving the size empty helps avoid some errors, because if the array has to contain 5 values
and you declare size 4, an error is generated as we will see below. Also if the size is 5 and
you enter 4 values, you will see a random value being generated that will create an
undesirable behavior in line with your data.
Example 2: Create an array that can change size in accordance to data entered.
Let’s say we have an array of characters called name, and our name is Jason.
Age/ data J a s o n
Array index/ position 0 1 2 3 4
This implies that, if you want an array that contains 10 characters, your size should be 9. And
for our first example, our size was supposed to be 9 and not 10.
We declare our array size as follows, arraySize = number of items - 1
Promdee
Calling the items in an array.
Let’s see the list of our items in the first array we created:
Promdee
Scanning data from the user:
Example
Look, the value was mutated to 13. Our array actually now contains 13 instead of 17.
Awesome.
Promdee
As programmers, we need to actually make our lives easier. We can’t write 1000 lines so that
we store or print 995 lines.
Thus, we now know that loops can actually assist us in printing as much as functions can. Or
we actually need our program to continuously accept values form the user. How would we
do that.
Example. Let’s use a loop to make our user enter values then print the values in our age array.
Promdee
One most possible error when dealing with arrays.
Index Out of Bound Error
Let’s change our limit to i < 12
Look carefully, the last 2 number we entered are 43 and 23. However the last two elements of
our array result are 10 and 24. Why’s that.
The program will say, Oooooh our array should contain 10 elements. So for the last 2, it will
generate some random number. Sometimes we get an error. However, this is a very bad error
and we should never make such a mistake of trying to access an array out of its.
This was all about 1 dimensional array, we shall look at 2 dimensional arrays(2D)
Promdee
What is it we are saying when mentioning dimensions in arrays. Let’s catch up in the next
arrays tutorials.
-- –It’s really funny making a program that gives a lot of output from a small code -- -
-
Promdee