Tuesday, October 15, 2019

Lesson 6 : ARRAY

An array is a collection of value or items stored in a memory location in a contagious way. The idea is to store multiple values or data of the same type together or close to each other using an offset to the base value to make it easier to calculate.

Think about the idea of an array as a staircase where your friends are standing on each step from you who is standing on the first step as the base value of the array. Now imagine the view if you look at it from the top of them.

Another example is a shoe rack where it is only one shoe rack but holds more than one shoes, or a lunch box which has divisions and is capable of storing different types of food.

In programming, an array is used as a data structure, see example below:

Memory Address
201
202
203
204
205
206
207
208
209
Value
and
is
or
it
if
lie
shy
go
born
Index/Offset
0
1
2
3
4
5
6
7
8

The indexing method of the array is based on the programming language used. Example there are programming languages which allows the programmer to start at index 1 or negative index like -1 but most commonly used index as a base of an offset is positive zero (0).

Example:

arr[10] = arr is the name of the variable and the number inside the bracket is the length of the array or the number of offset an array could have. In this example we have [10] so the total number of offset is 10 or 0,1,2,3,4,5,6,7,8,9


Memory Address
201
202
203
204
205
206
207
208
209
210
Value
and
is
or
it
if
lie
shy
go
born
gym
Index/Offset
0
1
2
3
4
5
6
7
8
9

so if I say arr[8] I am referring to value “born”

How to initialize an array variable

Example:

Java/C/C++

int[] arr = new int[10]; = this one is created without default value but with default length
int arr = {0,1,2,3,4,5,6,7,8,9}; = this one is created with default value and length

VB.Net
Dim arr(0 to 9) as Integer = this one is created without default value but with default length
Dim arr as Integer = {0,1,2,3,4,5,6,7,8,9} = this one is created with default value and length

To add a value on an offset simply access the offset number

arr[5] = “sample value”;

To update the value of an offset simply access the offset number

arr[5] = “new value”;

or arr[5] = “”; to remove the value

To retrieve or display a value from a specific offset or index of an array you can do the following:

Example:

C Programming Language
     Printf(“The value of index 5 is: %s”,arr[5]);



C++ Programming Language
     Cout<<“The value of index 5 is: %s”,arr[5]
Java Programming Language
     System.out.print(“The value of index 5 is: ”+arr[5]);

VB.net
     Console.Writeline(“The value of index 5 is: ” & arr[5])

Download PDF File

No comments:

Post a Comment

A REVIEW ON CONNIE DABATE’S MURDER CASE: Fitbit One Wearable

T he Author   ROSITO D. ORQUESTA MSIT Student at Jose Rizal Memorial State University-Dapitan Campus OIC-ICT Dean, Eastern Mindanao College ...