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

Lec09 11 Arrays

Uploaded by

Kusbu
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)
12 views

Lec09 11 Arrays

Uploaded by

Kusbu
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/ 47

Problem Solving and

Algorithmic Thinking

Data Organization
Lists, Arrays
Organizing data
• Humans hate disorder

• We try and organize our lives, our cities, our homes

• There are many ways of organizing things, and we naturally


organize different things in different ways

• The organization we use depends on the things we are trying


to organize and often more importantly, what we actually
wish to do with them once they are organized
Organizing data
Addresses are organized into postal areas so that it is easier for
letters to be delivered.

A bookshop organizes its novels on the shelves usually


alphabetically by author

• The way we organize the things we must manipulate to achieve a


task can make a massive difference to how quickly, easily or
successfully the task can be done
Is there a better way to store and access
related items?
• How should I store the details of all the student names of my class?

• If 60 students in a class can I have 60 different variable names?


• E.g. name1,name2, name3 ……., name60.

 Is it organized for efficient access? No !

 Is it easy to be remembered? No !!

 What happens if number of students increase from 60 to 80?


It becomes even more difficult to be handled and remembered ! ! !
Variables you've seen so far
store single unit of data
Variable Types

Real

https://www.cemc.math.uwaterloo.ca/~slgraham/csgirls/Tutorial/Variables/variables.html
What if your application
demands a collection of data
to be processed?
Collection of data usually
comprises related information

 Good idea to store them together


 not only logical….
 but also efficient !!
Arrays
Arrays store list of related
information in contiguous
memory locations
https://www.freecodecamp.org/news/data-structures-101-arrays-a-visual-introduction-for-beginners-7f013bcc355a/
Each memory unit holds a unit of data –
called elements of array. These elements are
next to each other in memory location.
https://www.freecodecamp.org/news/data-structures-101-arrays-a-visual-introduction-for-beginners-7f013bcc355a/
Since you often need more than one of them,
the fetching process is extremely optimized
because your computer knows where they are!
https://www.freecodecamp.org/news/data-structures-101-arrays-a-visual-introduction-for-beginners-7f013bcc355a/
Arrays can store any datatypes (Integers,
Reals, Booleans and Strings). But once you
decide the data type, all the array elements
must be of that same type. You can't mix
different types of data!!
https://www.freecodecamp.org/news/data-structures-101-arrays-a-visual-introduction-for-beginners-7f013bcc355a/
Now the array variable can hold multiple
units of data. Typically the name also
includes a square bracket that contains the
size of the array. For example
myArray[5]
https://www.freecodecamp.org/news/data-structures-101-arrays-a-visual-introduction-for-beginners-7f013bcc355a/
You use index (plural indices) to access a
value in an array. This is a number that
refers to the relative location where the value
is stored or to be stored. Note zero-based-numbering
https://www.freecodecamp.org/news/data-structures-101-arrays-a-visual-introduction-for-beginners-7f013bcc355a/
Boundaries of an Array
• Arrays are by default zero-based,
which means the enumeration of the
elements starts from 0.

• The index of the first element is 0,


and that of the second element is 1,
and so on.

• In an array of N elements, the last Figure: Boundary of myArray is 4


element has the index N-1.
Assigning value to an Array
• We assign the value to the array elements directly by

myArray[0]=10
• Each element can be accessed through the name of the array and the
element’s index (consecutive number) placed in the brackets

Figure: Elements of myArray


Array element access
• We access the array elements directly using their indices

• Each element can be accessed through the name of the array and the
element’s index (consecutive number) placed in the brackets

• For Eg. myArray[2] produces the 3rd element, 30

Figure: Elements of myArray


Array element access
• As long as we keep track of the location of the array, we will
be able to access every item in the array.

• The location of the array is known as the base address or


the anchor, and is the memory address of the first item in
the array.

• myArray= address of first item i.e myArray[0] address

• From a computer's perspective, the name of an array


corresponds to the anchor of the array.

• If the name of the array is known, then the anchor is


known and all of the array items can be accessed by their
index.
The general syntax to refer/access an element
is given above.
https://www.freecodecamp.org/news/data-structures-101-arrays-a-visual-introduction-for-beginners-7f013bcc355a/
When you define the size of an array, all of
that space is reserved. If you do not fill the
array, that space is kept reserved and empty
until you do.

https://www.freecodecamp.org/news/data-structures-101-arrays-a-visual-introduction-for-beginners-7f013bcc355a/
1 Byte=8 bits
Array Memory representation
NAME OF THE ARRAY IS REGNO

REGNO variable is
FIRST ELEMENT OF ARRAY integer
MEMORY “it occupy 4bytes of
ALLOCATED TO memory for an
REGNO IS FROM SECOND ELEMENT OF ARRAY element of Array”
316 TO 328

THIRD ELEMENT OF ARRAY

FOURTH ELEMENT OF ARRAY


As long as we keep track of the location of the array, we will be able to access every item in the array.
The location of the array is known as the base address or the anchor, and is the memory address of the first item in the
array.
From a computer's perspective, the name of an array corresponds to the anchor of the array.
If the name of the array is known, then the anchor is known and all of the array items can be accessed by their index.
1. Given the array scores,
scores 79 87 94 82 67 98 87 81 74 91

• Can you find


 How many elements are there?
 What is the fifth element in the array?
 What is the result of mean = (scores[0]+scores[1])/2

2. Workout the averages of Quiz1 and student0 with the following


data? Quiz1 Quiz2
Student0 95 85
Student1 89 80
Answers
How many elements are there? 10
What is the fifth element in the array? 67
What is the result of
mean = (scores[0]+scores[1])/2;
(79+87)/2 = 83

Can you workout the average of Quiz1 and student0


with the following data??? 92,90
Number of instructions increases in
code/algorithm
 This is why arrays are
recommended when
you know beforehand
how many elements
you are going to store.

 This is also the reason


why often arrays and
for loops go hand-in-
hand (twins).
Assign and print all the elements in an array
• Input: Array size, Array values
• Output: Sum of all elements in an array

Algorithm:
Step 1: Start
Step 2: Declare n, i
Step 3: Read input n from user
Step 4: Declare an array of size n, myArray[n]
Step 5: Initialize i=0
Step 6: Repeat
Step 6.1: myArray[i]=i
Step 6.2: i=i+1
Step 6: Until i==n
Step 7: Assign i=0 and sum=0
Step 8: Repeat
Step 8.1: Display myArray[i]
Step 8.2: i=i+1
Step 9: Until i==n
Step 1o: Stop
Read and print all the elements in an array
• Input: Array size, Array values
• Output: Sum of all elements in an array

Algorithm:
Step 1: Start
Step 2: Declare n, i
Step 3: Read input n from user
Step 4: Declare an array of size n, myArray[n]
Step 5: Initialize i=0
Step 6: Repeat
Step 6.1: Read array value from user and assign it to myArray[i]
Step 6.2: i=i+1
Step 6: Until i==n
Step 7: Assign i=0 and sum=0
Step 8: Repeat
Step 8.1: Display myArray[i]
Step 8.2: i=i+1
Step 9: Until i==n
Step 1o: Stop
Summary
Data organisation
• list
• array
• Array representation
• Memory representation
• Assigning value to array using loop construct
• Operations on array
Find the sum of all the elements in an array?
• Input: Array size, Array values
• Output: Sum of all elements in an array

Algorithm:
Step 1: Start
Step 2: Declare n, i, j, sum
Step 3: Read input n from user
Step 4: Declare an array of size n, myArray[n]
Step 5: Initialize i=0
Step 6: Repeat
Step 6.1: Read array value from user and assign it to myArray[i]
Step 6.2: i=i+1
Step 6: Until i==n
Step 7: Assign j=0 and sum=0
Step 8: Repeat
Step 8.1: sum=sum + myArray[j]
Step 8.2: j=j+1
Step 9: Until j==n
Step 10: Display sum
Step 11: Stop
To find player with maximum age from the
array of players age.
Real World Examples
• Digital Signal Processing
• Filter Design
• Convolution
• Auto-correlation
• Spectral Estimation
• Speech & audio processing
• Communication Engineering
• Digital Modulation and demodulation [m-QAM]
• Digital communication sub systems
• Lempel Ziv, LDPC, TURBO Codes
• Decoders, Viterbi Decoding
Real World Examples
• Control Systems
• State Space Representation
• State Feedback Controller
• Circuit Analysis
• Solving Linear Circuit Equations
• Image Processing
• Edge detection and filtering
• RF design
• Antenna array design
• Beamforming Weights

You might also like