Lecture 12 - Data Organization - Lists & Arrays
Lecture 12 - Data Organization - Lists & Arrays
• Algorithm
• Sequence
• Selection
• If
• If-else
• If-elseif ladder
• Nested if
• Loop
• While
• Do-while
• For
19CSE100 Problem Solving
and Algorithmic Thinking
Data
Organization
Lists, Arrays
Organizing data
• Humans hate disorder
Rea
l
https://www.cemc.math.uwaterloo.ca/~slgraham/csgirls/Tutorial/Variables/
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!!
Lists
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
https://www.freecodecamp.org/news/data-structures-101-arrays-a-visual-introduction-for-beginners-
Array Properties
•Arrays have three important properties.
• Arrays represent a group of related data.
(e.g. -- temperature for the last five days, or stock prices for the last 30
days.)
• All data within a single array must share the same data type.
• The size of an array is fixed once it is created.
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
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
Step 8: Repeat
Step 8.1: Display myArray[i]
Step 8.2: i=i+1
Step 9: Until i==n
Step 1o: Stop
Note: n-1 in the for loop
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
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 insert an element into an array, all
elements to the right side of the insertion
site should be moved one index to the
right. Only exception is the insertion at
the end (if space is available)
https://www.freecodecamp.org/news/data-structures-101-arrays-a-visual-introduction-for-beginners-
What if you must insert an element into
an array that is full?
https://www.freecodecamp.org/news/data-structures-101-arrays-a-visual-introduction-for-beginners-
What if you must insert an element into
an array that is full? You need to create a
larger array and manually copy all the
elements into the new array. A very
expensive operation!!
https://www.freecodecamp.org/news/data-structures-101-arrays-a-visual-introduction-for-beginners-
Like this. But this a very expensive
operation in terms of time!!
https://www.freecodecamp.org/news/data-structures-101-arrays-a-visual-introduction-for-beginners-
In case of deletion, you can't just delete
an element and leave the space empty.
Remember elements must be stored in
contiguous memory location to maintain
the efficiency of (random) access
https://www.freecodecamp.org/news/data-structures-101-arrays-a-visual-introduction-for-beginners-
In that case, what do you think can be
done?
https://www.freecodecamp.org/news/data-structures-101-arrays-a-visual-introduction-for-beginners-
Move all the elements to the right of the
element to be deleted by one index to the
left
https://www.freecodecamp.org/news/data-structures-101-arrays-a-visual-introduction-for-beginners-
Move all the elements to the right of the
element to be deleted by one index to the
left
https://www.freecodecamp.org/news/data-structures-101-arrays-a-visual-introduction-for-beginners-
When do you think deletion is very
efficient?
https://www.freecodecamp.org/news/data-structures-101-arrays-a-visual-introduction-for-beginners-
Multi Dimensional Arrays
• An array can be declared in multiple dimensions
https://www.freecodecamp.org/news/data-structures-101-arrays-a-visual-introduction-for-beginners-
How to find an element in a 1D array?
https://www.freecodecamp.org/news/data-structures-101-arrays-a-visual-introduction-for-beginners-
How to find an element in a 1D array?
https://www.freecodecamp.org/news/data-structures-101-arrays-a-visual-introduction-for-beginners-
How to find an element in a 1D array?
https://www.freecodecamp.org/news/data-structures-101-arrays-a-visual-introduction-for-beginners-
https://www.freecodecamp.org/news/data-structures-101-arrays-a-visual-introduction-for-beginners-
https://www.freecodecamp.org/news/data-structures-101-arrays-a-visual-introduction-for-beginners-
https://www.freecodecamp.org/news/data-structures-101-arrays-a-visual-introduction-for-beginners-
https://www.freecodecamp.org/news/data-structures-101-arrays-a-visual-introduction-for-beginners-
Thank you