0% found this document useful (0 votes)
2 views

Arrays

The document explains the concept of arrays, which are collections of similar data types that allow for the storage of multiple items under a single variable name. It covers how to declare arrays, access their elements using indices, and the importance of defining the correct size to avoid errors. Additionally, it introduces the use of loops for data entry and mentions the potential for index out of bound errors when accessing array elements.

Uploaded by

Hope
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Arrays

The document explains the concept of arrays, which are collections of similar data types that allow for the storage of multiple items under a single variable name. It covers how to declare arrays, access their elements using indices, and the importance of defining the correct size to avoid errors. Additionally, it introduces the use of loops for data entry and mentions the potential for index out of bound errors when accessing array elements.

Uploaded by

Hope
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

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.

Looking at the structure:


datatype refers to the type if the data that is to be stored in the arrays. This is just like the
datatypes that we have used so far. Integers, float, char or double.
arrayName is the variable name/ identifier of our array.
Unlike functions, what comes after an array if the [ ] brackets, that hole the size of the array.
Example, if the size of the array is 10, then you can not input more than 10 values into the
arrays.
Let’s look at how to declare an array and assign values.
Create an array called age that stores the ages for 10 people.

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.

The array above contains 8 ages.


Now that we can create an array, lets look at how we can access the items in our array.
First – We need to know how data is stored in an array.
Our arrays use the idea of Array index.
For our arrays, every element is associated with a number that specifies the position of the
item in the array. The number is called the index.
The index system in an array starts at 0, that’s 0 is the first position f an array.
Example, our ages array in the first example:
Age/ data 17 18 22 25 18 32 33 19 19 20
Array index/ position 0 1 2 3 4 5 6 7 8 9

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:

NB- for every call, you call the arrayName[arrayIndex].


If you want a single age, you just declare its position.
Let’s say we want our array to collect data from a different part of our project. Let’s see the
example to insert the 10 values in our first array.

Oooooops!!!!!, Really tiresome, but still it works.

Promdee
Scanning data from the user:
Example

Even more tiresome. But it works.


We can actually change our values from the array, for example changing our first age from
our first array. We want first age to be 13.

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.

That was a bit relaxing.


But its perfectly up to one to decide.

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.

Where did the two values come from.

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.

For now attempt this and enjoy the sweetness of arrays.


Create a program that computes the average marks of a student.
Steps
1. Create an array that stores the marks of 5 subjects.
2. Create the total marks by adding all the marks.
3. Divide the total marks by the total number of subjects
4. Print the average marks

----It’s actually Funny, ---YES ----it’s like magic ---------

-- –It’s really funny making a program that gives a lot of output from a small code -- -
-

----------“1D array” completed--------


Next: (to be updated ) ------

Promdee

You might also like