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

Algorithms 6 Arrays

Arrays allow storing a list of data in contiguous memory locations that can be accessed using an index. Each array has a name and set of locations denoted by an index. Arrays are useful for storing lists of data and processing elements using loops. They are static structures where the size is fixed once defined. Examples show using arrays to store numbers and names accessed by index. Arrays have many applications including storing data, computing sequences, and statistical computations.

Uploaded by

lordjebus2000
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Algorithms 6 Arrays

Arrays allow storing a list of data in contiguous memory locations that can be accessed using an index. Each array has a name and set of locations denoted by an index. Arrays are useful for storing lists of data and processing elements using loops. They are static structures where the size is fixed once defined. Examples show using arrays to store numbers and names accessed by index. Arrays have many applications including storing data, computing sequences, and statistical computations.

Uploaded by

lordjebus2000
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 9

Algorithms & DS

Arrays
Arrays
• How do we store a list of data, e.g. numbers
• Could us separate variables for each one,
a,b,c,d,e,…
• Not very efficient, so we use an array instead.
• An array is a set of memory locations that
– Are contiguous (adjacent) in memory,
– Store a list of items of the same data type.
• Each array has a name and a set of locations
denoted by an index.
Arrays
• Example. Array called A, holding a list of 9
elements.
• Each element has an index, e.g.
A(1),A(2),…,A(9). The index i is the
location and A(i) is the element itself.
• This array has 9 elements, the index is
shown. The elements are stored in the
boxes (memory locations)
• A
Arrays
• The elements can be accessed individually
using the index. Example.
• A(1) := 5
• A(5) := 10
• A(8) := 15
• A(9) := 7, etc
• More usefully, the array elements can also be
accessed/manipulated using a for loop.
• for i = 1, 9
• A(i) := i+3
• end for
Arrays
• This loops sets the array element A(i) to
the value i+1. Often the elements are
initialised to the value 0.
• So for loops are very useful for working
with loops, since all the elements (or some
of them) can be processed using a single
loop.
• The control variable in the loop is usually
the index of the array elements.
Arrays
• The array is the simplest data structure.
• The array is a static data struture, i.e. once
it is defined its size cannot be changed.
• This is because a fixed amount of memory
space allocated when it is created.
• In programming languages, arrays must
be declared. This is to
– Define the data type to be stored.
– Define the array size
Arrays
• Example 1. Array of numbers
• Marks(1) := 10
• Marks(2) := 14
• Marks(3) := 15
• Marks(4) := 20
• Marks(5) := 12
• Example 2. Array of names.
• Name(1) := “Jack”
• Name(2) := “Jill”
• Name(3) := “Jane”
• Name(4) := “Jake”
• Name(5) := “John”
• Since names are all different, must do them separately.
Arrays
• Many Applications of arrays. Some are
– Storing lists of data
– Computing sequences and series of numbers.
– Mathematical/statistical computations.
• The arithmetic sequence a, a+d, a+2d, a + 3d, …
• The geometric sequence a, ar, ar2, ar3,…
• In arithmetic sequence each term differs by a constant
number, d
• In geometric sequence each term differs by a constant
multiple factor r.
• Arithmetic sequence: 1, 4, 7, 10,…
• Geometric sequence: 1, 2, 4, 8, 16,…
Arrays
• A series is the sum of the terms of the sequence.
• E.g. Sn = sum of the first n terms of a sequence,
n = 1,2,3,…. The sum of the first n terms may be
computed using a for loop, which adds one term
each time the loop is executed.
• Other sequences, e.g. the Fibonacci sequence,
which is given by
• f1 = 1, f2 = 1, fi+1 = fi + fi-1, i = 2,3,4,…
• It’s 1,1,2,3,5,8,13,…

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy