SlideShare a Scribd company logo
Slot 18-19-20: Contiguous Storage
Arrays/Simple Data Structure/Contiguous
Storage/Searching Sorting
Objectives
• How to manage a group of data?
• Store
• Input
• Output
• Search
• Sort
Contents
• Introduction to
contiguous storage
• Arrays
• One-dimensional Arrays
• Declaration
• Memory Allocation
• Initialization
• Accessing elements
• Traversing
• 1-D Arrays are
parameters of
functions
• Searching
• Sorting
• 2-D Arrays
3
Contiguous Storage
• Commonly, a group of the same meaning elements are considered.
• They are stored in a contiguous block of memory.
• Ex: Group of 10 int numbers ➔ 40 bytes block is needed.
• Data are considered can be a group of some items which belong to some
different data types → Contiguous memory block is partitioned into some
parts which have different size, one part for an item.
• Data structure: A structure of data stored.
• Array is the simplest data structure which contains some items which
belong to the same data type.
• Common used operations on a group: Add, Search, Remove, Update, Sort
4
Contiguous Storage
Arrays
• Array: A group
of elements
which belong
to the same
data type.
• Each element
is identified by
it’s position
(index).
• Dimension: Direction that is used to perform an action on array.
• Number of dimensions: Number of indexes are used to specify
an element.
• Common arrays: 1-D and 2-D arrays.
• Name of an array: An array has it’s name.
5 4 8 15 90 27 34 21 152 80
a
(array)
a[3]
element
0 1 2 3 4 5 6 7 8 9
index
a[i] is an integer
m
row
column
m[1][3]
5
Contiguous Storage
One Dimensional (1-D)Arrays
• 1-D array: a collection of items (elements, terms) which
belong to the same data type and are stored contiguously in
memory.
→Each element is identified by a unique index of it’s position
in the array (an integer from 0).
5 4 8 15 90 27 34 21 152 80
a
(array)
a[3]
element
0 1 2 3 4 5 6 7 8 9
index
a[i] is an integer
6
Contiguous Storage
1-D Arrays: Declaration
• If the array is stored in the stack segment → Use a STATIC array
→ The compiler will determine the array’s storage at compile-
time.
• If the array is stored in the heap → Use a pointer (DYNAMIC
array) → The array’s storage will be allocated in the heap at run-
time through memory allocating functions (malloc, calloc,
realloc)
DataType ArrayName[NumberOfElements] ;
Examples:
int a1[5];
char s[12];
double a3[100];
How compilers can determine the
memory size of an array?
NumberOfElements *
sizeof(dataType)
➔ int a1[5] → 5 *sizeof(int) = 5*4 =
20 bytes
float *a;
a = (float*)calloc (10, sizeof(float)); /* allocate a block of 10 float numbers */
7
Contiguous Storage
1-D Arrays: Memory Allocation 20
4202496
code
4199056
20
bytes
a1:
2293584
80
bytes
407249
6
Data segment
Code segment
Stack segment
Heap
4072496
a2:2293580
The name of the array is the
address of the first element.
8
Contiguous Storage
1-D Arrays:
Initialization & Accessing Elements
Initialize an array:
DataType a[] = {val1, val2, … };
How to access the ith element of the
array a?
• a is the address of the first element.
Based on operation on pointers:
→ a+i : address of the ith element,
another way: &a[i]
→*(a+i): value of the ith element,
→ popular way: a[i]
9
Contiguous Storage
Compiler will
automatically count
number of initial values
to determine the size
of array memory
The size of array
memory is pre-
defined.
Compiler will fill 0 to
elements which are
not initialized.
int a[5];
Elements contain
un-predictable
values because they
are local variables.
TEST IT !!!! 10
Contiguous Storage
1-D Arrays: Init. & Accessing…
1-D Arrays: Traversing
• A way to visit each element of an array
• Suppose that the 1-D array, named a, containingn elements.
• Forward traversal:
int i;
for (i=0; i<n; i++)
{ [if (condition)] Accessa[i];
}
• Backward traversal:
int i;
for (i=n-1; i >=0; i--)
{ [if (condition)] Access a[i];
}
Contiguous Storage 11
Array Function Parameter: Demo 1.
• For a parameter of an array type, the value
of the parameter contains a reference to an
array; this reference is passed to the
method. Any changes to the array that
occur inside the method body will affect the
original array that was passed as the
argument.
• Input an array of n integers: void input (int*
a, int n)
• Input elements of an array of integers
which it’s number of element is stored at
the pointer pn: void input (int a[], int*pn)
• Output an array of n double numbers:
• void output (double a[], int n)
• Calculate the sum of an array of n integers:
int sum (int *a, int n)
12
Contiguous Storage
Array Function Parameter: Demo 1.
• Develop a C-program that will:
• Accept values to an integer array that may contain 100 elements.
• Print out the it’s maximum value.
• Print out it’s elements.
• Print out it’s even values.
• Nouns:
– Constant: MAXN=100
–Static array of integers
→ int a[MAXN]
–Real number of
elements → int n
–Maximum value
→ int maxVal.
• Verbs:
-Begin
-Input n (one value)
-Input a, n (function)
- maxVal = get maximum value in a, n
(function)
- Print out maxVal (one value)
- Print out a, n (function)
- Print even values in a, n (function)
- End
13
Contiguous Storage
Array Function Parameter: Demo 1.
14
Contiguous Storage
Array Function Parameter: Demo 1.
15
Contiguous Storage
When using a dynamic array?
• Comments
• If you allocate an array having 100 elements but 6 elements are
used then memory is wasted.
• If If you allocate an array having 100 elements but 101
elements are used then there is a lack of memory.
• Solution: Use a dynamic array.
16
Contiguous Storage
Array Function Parameter: Demo 1.
17
Contiguous Storage
Array Function Parameter: Demo 1.
18
Contiguous Storage
Array Function Parameter: Demo 2.
• Develop a C-program that will:
-Accept values to an integer array that may contains 100
elements. The input will terminate when user enters the
value of zero.
- Print out the it’s maximum value.
- Print out it’s elements.
- Print out it’s even values.
The difference between this problem with the previous one is the input
operation can terminate abruptly when 0 is accepted.
➔Memory block of the array needs to be allocated in excess
➔The function for input values of the array must be modified for this case and
the number of elements is updated after each valid value is accepted.
19
Contiguous Storage
Array Function Parameter: Demo 2.
20
Contiguous Storage
Array Function Parameter: Demo 2.
0
3
n=0 → 1
x=3
0 1 2
3 5 2
n=3→ 4
x=7
21
Contiguous Storage
1-D Arrays: Searching
• A search algorithm finds the record of interest
using the key array
• Return value: The positional index at which the
interest value is found.
• Two common search algorithms are
• Linear search
• find whether a given number is present in
an array and if it is present then at what
location it occurs.
• binary search
• Search a sorted array by repeatedly
dividing the search interval in half.
• If the value of the search key is less than
the item in the middle of the interval,
narrow the interval to the lower half.
Otherwise narrow it to the upper half.
• Repeatedly check until the value is found
or the interval is empty.
22
Contiguous Storage
1-D Arrays: Searching
23
Contiguous Storage
1-D Arrays: Searching…
• Linear search: Find the position of the value x in the array a
having n elements.
5 9 2 7 6 5 2 5
i=0 1 2 3 4
• Search the value of 6 in the array a having 8 items
5 9 2 7 6 5 2 5
i=0 1 2 3 4 5 6 7
• Search the value of 12 in the array a having 8 items
-1
There may be
n comparisons
performed.
24
Contiguous Storage
1-D Arrays: Linear Searching…
25
Contiguous Storage
1-D Arrays: Binary Searching…
Binary search
- Condition for
application: Values
in the array were
sorted.
return c (7)
i>j → return -1
26
Contiguous Storage
1-D Arrays: Binary Searching…
No. of elements considered No. of comparisons
n= 2m 1
2m-1 1
2m-2 1
… …
20 1
Sum m+1 = log2(n) +1
Evaluation:
27
Contiguous Storage
1-D Arrays: Sorting
• Sorting: Changing positions of elements in an array so that
values are in a order based on a pre-defined order relation.
• Default order relation in set of numbers: Value order
• Default order relation in a set of characters/ strings:
Dictionary order
• Only two sorting algorithms are introduced here.
• Selection Sort
• Bubble Sort
28
Contiguous Storage
1-D Arrays: Selection Sort
• Find the minimum value in the list
• Swap it with the value in the first position
• Repeat the steps above for remainder of the list
29
Contiguous Storage
1-D Arrays: Selection Sort
30
Contiguous Storage
1-D Arrays: Bubble Sort
•It works by
repeatedly
stepping through
the list to be
sorted,
comparing two
items at a time
and swapping
them if they are
in the wrong
order.
•The pass
through the list is
repeated until no
swaps are
needed, which
means the list is
sorted
31
Contiguous Storage
1-D Arrays: Bubble Sort…
32
Contiguous Storage
1-D Arrays: A Sample
• Develop a C-program that helps user managing an 1-D array of
integers (maximum of 100 elements) using the following simple
menu:
• 1- Add a value
• 2- Search a value
• 3- Remove the first existence of a value
• 4- Remove all existences of a value
• 5- Print out the array
• 6- Print out the array in ascending order (positions of elements
are preserved)
• 7- Print out the array in descending order (positions of elements
are preserved)
• Others- Quit
33
Contiguous Storage
1-D Arrays: A Sample…
• In this program, user can freely add or remove one or more elements to/
from the array. So, an extra memory allocation is needed (100 items).
• Data:
Array of integers → int a[100], n
searched/added/removed number → int value
• Functions:
• int menu() → Get user choice
• int isFull(int *a, int n) - Testing whether an array is full or not
• int isEmpty(int *a, int n) - Testing whether an array is empty or not
• void add(int x, int*a, int*pn) → adding an element to the array will
increase number of elements
• int search(int x, int *a, int n) → return a position found in the array
• int removeOne (int pos, int*a, int*pn) → Removing a value at the
position pos will decrease number of elements → return 1:
successfully, 0: fail
• int remove All(int x, int*a, int*pn) → Removing a value will decrease
number of elements → return 1: successfully, 0: fail
• void printAsc(int*a, int n) – printing array, elements are preserved
• void printDesc(int*a, int n) – printing array, elements are preserved
• void print(int*a, int n) 34
Contiguous Storage
1-D Arrays: A Sample…
After values 0 , 2, 8, 9, 7, 3, 2, 4, 2 are
added. Use menu 5 to view them.
Search option Remove one option
35
Contiguous Storage
1-D Arrays: A Sample…
Remove all option
Print out in ascending order
(elements are preserved)
Print out in descending order
(elements are preserved)
36
Contiguous Storage
1-D Arrays: A Sample…
37
Contiguous Storage
1-D Arrays: A Sample…
0 1 2 3 4 5 6 7 8
0 2 9 7 3 2 4 2 2
9 7 3 2 4 2 2
index
38
Contiguous Storage
1-D Arrays: A Sample…
39
Contiguous Storage
1-D Arrays: A Sample…
3
4
2
1
5
4223516
2293496
2293488
2293492
2293504
2293500
2293488
2293492
2293496
2293500
2293504
adds
4223516 2293488
2293492
2293496
2293500
2293504
Values of pointers after
sorting
40
Contiguous Storage
1-D Arrays: A Sample…
41
Contiguous Storage
1-D Arrays: A Sample…
42
Contiguous Storage
1-D Arrays: A Sample…
43
Contiguous Storage
1-D Arrays: A Sample…
44
Contiguous Storage
Two-Dimensional Arrays
• A group of elements which belong the same data type and
they are divided into some rows and some column (it is
called as matrix also).
• Each element is identified by two indexes (index of row,
index of column).
m
row
column
m[1][3]
Traversing a matrix:
for ( i =0; i<row; i++)
{ for ( j=0; j< column; j++)
[if (condition)] Access m[i][j];
}
The next slide
will
demonstrate
how static
and dynamic
2-D arrays are
stored.
45
Contiguous Storage
2-D Arrays: Memory Structure
4078616
2293488
2293492
2293496
2293500
2293504
2293508
2293512
2293520
2293524
2293528
2293532
2293516
2293596
4078824
4078784
4078744
4078720
2293600
p
m1
m2
4078752
4078760
4078768
4078784
4078792
4078800
4078808
4078832
4078840
4078848
4078824
4078744
4078720
4078616
H
E
A
P
S
T
A
C
K
46
Contiguous Storage
Static 2-D Arrays Demo.
Accept a matrix maximum 20x20.
Print out maximum value in it, print out the matrix.
Keep in your mind the way to
specify a matrix as a parameter
of a function ( the number of
column must be pre-defined.).
47
Contiguous Storage
Static 2-D Arrays Demo.
48
Contiguous Storage
Summary
• Array is the simplest data structure for a group of elements which
belong to the same data type.
• Each element in an array is identified by one or more index
beginning from 0.
• Number of dimensions: Number of indexes are used to identify an
element.
• Static arrays → Stack segment
Type a[MAXN];
Type m[MAXROW][MAXCOL];
• Dynamic array: Use pointer and allocate memory using functions
double *a = (double*)calloc(n, sizeof(double));
int** m = (int**) calloc(row, sizeof(int*));
for (i=0; i<row; i++) m[i]= (int*)calloc(col, sizeof(int));
49
Contiguous Storage
Summary
• Accessing elements in an array:
1-D Array (a) 2-D Array (m)
Address Value Address Value
&a[index] a[index] &m[i][j] m[i][j]
a+index *(a+index)
Compiler determines the address of an element:
a + index*sizeof(DataType) m + (i*NumCol + j)*sizeof(DataType)
• Common operations on
arrays:
• Add an element
• Search an element
• Remove an element
• Input
• Output
• Sort
• Base of algorithms on
arrays: Traversing
50
Contiguous Storage
Exercise- Do yourself
• Develop a C-program that helps user managing an 1-D array
of real numbers(maximum of 100 elements) using the
following simple menu:
• 1- Add a value
• 2- Search a value
• 3- Print out the array
• 4- Print out values in a range (minVal<=value<=maxVal,
minVal and maxVal are inputted)
• 5- Print out the array in ascending order (positions of
elements are preserved)
• Others- Quit
51
Contiguous Storage

More Related Content

Similar to 0-Slot18-19-20-ContiguousStorage.pdf

Lesson 11 one dimensional array
Lesson 11 one dimensional arrayLesson 11 one dimensional array
Lesson 11 one dimensional array
MLG College of Learning, Inc
 
Acm aleppo cpc training seventh session
Acm aleppo cpc training seventh sessionAcm aleppo cpc training seventh session
Acm aleppo cpc training seventh session
Ahmad Bashar Eter
 
4.ArraysInC.pdf
4.ArraysInC.pdf4.ArraysInC.pdf
4.ArraysInC.pdf
FarHanWasif1
 
2 Arrays & Strings.pptx
2 Arrays & Strings.pptx2 Arrays & Strings.pptx
2 Arrays & Strings.pptx
aarockiaabinsAPIICSE
 
unit 2.pptx
unit 2.pptxunit 2.pptx
unit 2.pptx
researchgrad82
 
STACK.pptx
STACK.pptxSTACK.pptx
STACK.pptx
Dr.Shweta
 
Arrays in C.pptx
Arrays in C.pptxArrays in C.pptx
Arrays in C.pptx
HarsimranKaur362773
 
Arrays Basics
Arrays BasicsArrays Basics
Arrays Basics
Nikhil Pandit
 
Unit viii searching and hashing
Unit   viii searching and hashing Unit   viii searching and hashing
Unit viii searching and hashing
Tribhuvan University
 
DS Unit 1.pptx
DS Unit 1.pptxDS Unit 1.pptx
DS Unit 1.pptx
chin463670
 
Queue
QueueQueue
Unit 4
Unit 4Unit 4
Unit 2 linear data structures
Unit 2   linear data structuresUnit 2   linear data structures
Unit 2 linear data structures
Senthil Murugan
 
Arrays In C++
Arrays In C++Arrays In C++
Arrays In C++
Awais Alam
 
Arrays.pptx
 Arrays.pptx Arrays.pptx
Arrays.pptx
PankajKumar497975
 
An Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: ArraysAn Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: Arrays
Martin Chapman
 
STRINGS IN JAVA
STRINGS IN JAVASTRINGS IN JAVA
Week2-stacks-queues.pptx
Week2-stacks-queues.pptxWeek2-stacks-queues.pptx
Week2-stacks-queues.pptx
VandanaBharti21
 
Array
ArrayArray
Array
PRN USM
 
21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx
reddy19841
 

Similar to 0-Slot18-19-20-ContiguousStorage.pdf (20)

Lesson 11 one dimensional array
Lesson 11 one dimensional arrayLesson 11 one dimensional array
Lesson 11 one dimensional array
 
Acm aleppo cpc training seventh session
Acm aleppo cpc training seventh sessionAcm aleppo cpc training seventh session
Acm aleppo cpc training seventh session
 
4.ArraysInC.pdf
4.ArraysInC.pdf4.ArraysInC.pdf
4.ArraysInC.pdf
 
2 Arrays & Strings.pptx
2 Arrays & Strings.pptx2 Arrays & Strings.pptx
2 Arrays & Strings.pptx
 
unit 2.pptx
unit 2.pptxunit 2.pptx
unit 2.pptx
 
STACK.pptx
STACK.pptxSTACK.pptx
STACK.pptx
 
Arrays in C.pptx
Arrays in C.pptxArrays in C.pptx
Arrays in C.pptx
 
Arrays Basics
Arrays BasicsArrays Basics
Arrays Basics
 
Unit viii searching and hashing
Unit   viii searching and hashing Unit   viii searching and hashing
Unit viii searching and hashing
 
DS Unit 1.pptx
DS Unit 1.pptxDS Unit 1.pptx
DS Unit 1.pptx
 
Queue
QueueQueue
Queue
 
Unit 4
Unit 4Unit 4
Unit 4
 
Unit 2 linear data structures
Unit 2   linear data structuresUnit 2   linear data structures
Unit 2 linear data structures
 
Arrays In C++
Arrays In C++Arrays In C++
Arrays In C++
 
Arrays.pptx
 Arrays.pptx Arrays.pptx
Arrays.pptx
 
An Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: ArraysAn Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: Arrays
 
STRINGS IN JAVA
STRINGS IN JAVASTRINGS IN JAVA
STRINGS IN JAVA
 
Week2-stacks-queues.pptx
Week2-stacks-queues.pptxWeek2-stacks-queues.pptx
Week2-stacks-queues.pptx
 
Array
ArrayArray
Array
 
21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx
 

More from ssusere19c741

0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf
ssusere19c741
 
0-Slot14-15-16-Libraries.pdf
0-Slot14-15-16-Libraries.pdf0-Slot14-15-16-Libraries.pdf
0-Slot14-15-16-Libraries.pdf
ssusere19c741
 
0-Slot13-Programming-With-Menu.pdf
0-Slot13-Programming-With-Menu.pdf0-Slot13-Programming-With-Menu.pdf
0-Slot13-Programming-With-Menu.pdf
ssusere19c741
 
0-Slot11-12-Pointers.pdf
0-Slot11-12-Pointers.pdf0-Slot11-12-Pointers.pdf
0-Slot11-12-Pointers.pdf
ssusere19c741
 
0-Slot08-09-10-Module-Functions.pdf
0-Slot08-09-10-Module-Functions.pdf0-Slot08-09-10-Module-Functions.pdf
0-Slot08-09-10-Module-Functions.pdf
ssusere19c741
 
0-Slot05-06-07-Basic-Logics.pdf
0-Slot05-06-07-Basic-Logics.pdf0-Slot05-06-07-Basic-Logics.pdf
0-Slot05-06-07-Basic-Logics.pdf
ssusere19c741
 
0-Slot02-Introduction-to-PFC.pdf
0-Slot02-Introduction-to-PFC.pdf0-Slot02-Introduction-to-PFC.pdf
0-Slot02-Introduction-to-PFC.pdf
ssusere19c741
 
Intro-InstallingTool-FirstProgram
Intro-InstallingTool-FirstProgramIntro-InstallingTool-FirstProgram
Intro-InstallingTool-FirstProgram
ssusere19c741
 
Background Tasks with Worker Service
Background Tasks with Worker ServiceBackground Tasks with Worker Service
Background Tasks with Worker Service
ssusere19c741
 
Real-Time Communication
Real-Time CommunicationReal-Time Communication
Real-Time Communication
ssusere19c741
 
Building Websites Using ASP.NET Core Razor Pages
Building Websites Using ASP.NET Core Razor PagesBuilding Websites Using ASP.NET Core Razor Pages
Building Websites Using ASP.NET Core Razor Pages
ssusere19c741
 
Dependency Injection in .NET
Dependency Injection in .NETDependency Injection in .NET
Dependency Injection in .NET
ssusere19c741
 
Asynchronous and Parallel Programming in .NET
Asynchronous and Parallel Programming in .NETAsynchronous and Parallel Programming in .NET
Asynchronous and Parallel Programming in .NET
ssusere19c741
 
Networking Programming
Networking ProgrammingNetworking Programming
Networking Programming
ssusere19c741
 
Working with XML and JSON Serializing
Working with XML and JSON SerializingWorking with XML and JSON Serializing
Working with XML and JSON Serializing
ssusere19c741
 
Building Windows Presentation Foundation (WPF) Application
Building Windows Presentation Foundation (WPF) ApplicationBuilding Windows Presentation Foundation (WPF) Application
Building Windows Presentation Foundation (WPF) Application
ssusere19c741
 
Course Introduction
Course IntroductionCourse Introduction
Course Introduction
ssusere19c741
 
Building Windows Presentation Foundation (WPF) Application
Building Windows Presentation Foundation (WPF) ApplicationBuilding Windows Presentation Foundation (WPF) Application
Building Windows Presentation Foundation (WPF) Application
ssusere19c741
 
Course Introduction
Course IntroductionCourse Introduction
Course Introduction
ssusere19c741
 

More from ssusere19c741 (19)

0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf
 
0-Slot14-15-16-Libraries.pdf
0-Slot14-15-16-Libraries.pdf0-Slot14-15-16-Libraries.pdf
0-Slot14-15-16-Libraries.pdf
 
0-Slot13-Programming-With-Menu.pdf
0-Slot13-Programming-With-Menu.pdf0-Slot13-Programming-With-Menu.pdf
0-Slot13-Programming-With-Menu.pdf
 
0-Slot11-12-Pointers.pdf
0-Slot11-12-Pointers.pdf0-Slot11-12-Pointers.pdf
0-Slot11-12-Pointers.pdf
 
0-Slot08-09-10-Module-Functions.pdf
0-Slot08-09-10-Module-Functions.pdf0-Slot08-09-10-Module-Functions.pdf
0-Slot08-09-10-Module-Functions.pdf
 
0-Slot05-06-07-Basic-Logics.pdf
0-Slot05-06-07-Basic-Logics.pdf0-Slot05-06-07-Basic-Logics.pdf
0-Slot05-06-07-Basic-Logics.pdf
 
0-Slot02-Introduction-to-PFC.pdf
0-Slot02-Introduction-to-PFC.pdf0-Slot02-Introduction-to-PFC.pdf
0-Slot02-Introduction-to-PFC.pdf
 
Intro-InstallingTool-FirstProgram
Intro-InstallingTool-FirstProgramIntro-InstallingTool-FirstProgram
Intro-InstallingTool-FirstProgram
 
Background Tasks with Worker Service
Background Tasks with Worker ServiceBackground Tasks with Worker Service
Background Tasks with Worker Service
 
Real-Time Communication
Real-Time CommunicationReal-Time Communication
Real-Time Communication
 
Building Websites Using ASP.NET Core Razor Pages
Building Websites Using ASP.NET Core Razor PagesBuilding Websites Using ASP.NET Core Razor Pages
Building Websites Using ASP.NET Core Razor Pages
 
Dependency Injection in .NET
Dependency Injection in .NETDependency Injection in .NET
Dependency Injection in .NET
 
Asynchronous and Parallel Programming in .NET
Asynchronous and Parallel Programming in .NETAsynchronous and Parallel Programming in .NET
Asynchronous and Parallel Programming in .NET
 
Networking Programming
Networking ProgrammingNetworking Programming
Networking Programming
 
Working with XML and JSON Serializing
Working with XML and JSON SerializingWorking with XML and JSON Serializing
Working with XML and JSON Serializing
 
Building Windows Presentation Foundation (WPF) Application
Building Windows Presentation Foundation (WPF) ApplicationBuilding Windows Presentation Foundation (WPF) Application
Building Windows Presentation Foundation (WPF) Application
 
Course Introduction
Course IntroductionCourse Introduction
Course Introduction
 
Building Windows Presentation Foundation (WPF) Application
Building Windows Presentation Foundation (WPF) ApplicationBuilding Windows Presentation Foundation (WPF) Application
Building Windows Presentation Foundation (WPF) Application
 
Course Introduction
Course IntroductionCourse Introduction
Course Introduction
 

Recently uploaded

How MJ Global Leads the Packaging Industry.pdf
How MJ Global Leads the Packaging Industry.pdfHow MJ Global Leads the Packaging Industry.pdf
How MJ Global Leads the Packaging Industry.pdf
MJ Global
 
Business storytelling: key ingredients to a story
Business storytelling: key ingredients to a storyBusiness storytelling: key ingredients to a story
Business storytelling: key ingredients to a story
Alexandra Fulford
 
Call 8867766396 Satta Matka Dpboss Matka Guessing Satta batta Matka 420 Satta...
Call 8867766396 Satta Matka Dpboss Matka Guessing Satta batta Matka 420 Satta...Call 8867766396 Satta Matka Dpboss Matka Guessing Satta batta Matka 420 Satta...
Call 8867766396 Satta Matka Dpboss Matka Guessing Satta batta Matka 420 Satta...
bosssp10
 
一比一原版新西兰奥塔哥大学毕业证(otago毕业证)如何办理
一比一原版新西兰奥塔哥大学毕业证(otago毕业证)如何办理一比一原版新西兰奥塔哥大学毕业证(otago毕业证)如何办理
一比一原版新西兰奥塔哥大学毕业证(otago毕业证)如何办理
taqyea
 
Creative Web Design Company in Singapore
Creative Web Design Company in SingaporeCreative Web Design Company in Singapore
Creative Web Design Company in Singapore
techboxsqauremedia
 
Anny Serafina Love - Letter of Recommendation by Kellen Harkins, MS.
Anny Serafina Love - Letter of Recommendation by Kellen Harkins, MS.Anny Serafina Love - Letter of Recommendation by Kellen Harkins, MS.
Anny Serafina Love - Letter of Recommendation by Kellen Harkins, MS.
AnnySerafinaLove
 
buy old yahoo accounts buy yahoo accounts
buy old yahoo accounts buy yahoo accountsbuy old yahoo accounts buy yahoo accounts
buy old yahoo accounts buy yahoo accounts
Susan Laney
 
Top mailing list providers in the USA.pptx
Top mailing list providers in the USA.pptxTop mailing list providers in the USA.pptx
Top mailing list providers in the USA.pptx
JeremyPeirce1
 
Unveiling the Dynamic Personalities, Key Dates, and Horoscope Insights: Gemin...
Unveiling the Dynamic Personalities, Key Dates, and Horoscope Insights: Gemin...Unveiling the Dynamic Personalities, Key Dates, and Horoscope Insights: Gemin...
Unveiling the Dynamic Personalities, Key Dates, and Horoscope Insights: Gemin...
my Pandit
 
Satta Matka Dpboss Matka Guessing Kalyan Chart Indian Matka Kalyan panel Chart
Satta Matka Dpboss Matka Guessing Kalyan Chart Indian Matka Kalyan panel ChartSatta Matka Dpboss Matka Guessing Kalyan Chart Indian Matka Kalyan panel Chart
Satta Matka Dpboss Matka Guessing Kalyan Chart Indian Matka Kalyan panel Chart
➒➌➎➏➑➐➋➑➐➐Dpboss Matka Guessing Satta Matka Kalyan Chart Indian Matka
 
Authentically Social by Corey Perlman - EO Puerto Rico
Authentically Social by Corey Perlman - EO Puerto RicoAuthentically Social by Corey Perlman - EO Puerto Rico
Authentically Social by Corey Perlman - EO Puerto Rico
Corey Perlman, Social Media Speaker and Consultant
 
Hamster Kombat' Telegram Game Surpasses 100 Million Players—Token Release Sch...
Hamster Kombat' Telegram Game Surpasses 100 Million Players—Token Release Sch...Hamster Kombat' Telegram Game Surpasses 100 Million Players—Token Release Sch...
Hamster Kombat' Telegram Game Surpasses 100 Million Players—Token Release Sch...
SOFTTECHHUB
 
Best Forex Brokers Comparison in INDIA 2024
Best Forex Brokers Comparison in INDIA 2024Best Forex Brokers Comparison in INDIA 2024
Best Forex Brokers Comparison in INDIA 2024
Top Forex Brokers Review
 
Part 2 Deep Dive: Navigating the 2024 Slowdown
Part 2 Deep Dive: Navigating the 2024 SlowdownPart 2 Deep Dive: Navigating the 2024 Slowdown
Part 2 Deep Dive: Navigating the 2024 Slowdown
jeffkluth1
 
The Evolution and Impact of OTT Platforms: A Deep Dive into the Future of Ent...
The Evolution and Impact of OTT Platforms: A Deep Dive into the Future of Ent...The Evolution and Impact of OTT Platforms: A Deep Dive into the Future of Ent...
The Evolution and Impact of OTT Platforms: A Deep Dive into the Future of Ent...
ABHILASH DUTTA
 
Tata Group Dials Taiwan for Its Chipmaking Ambition in Gujarat’s Dholera
Tata Group Dials Taiwan for Its Chipmaking Ambition in Gujarat’s DholeraTata Group Dials Taiwan for Its Chipmaking Ambition in Gujarat’s Dholera
Tata Group Dials Taiwan for Its Chipmaking Ambition in Gujarat’s Dholera
Avirahi City Dholera
 
BeMetals Investor Presentation_June 1, 2024.pdf
BeMetals Investor Presentation_June 1, 2024.pdfBeMetals Investor Presentation_June 1, 2024.pdf
BeMetals Investor Presentation_June 1, 2024.pdf
DerekIwanaka1
 
Mastering B2B Payments Webinar from BlueSnap
Mastering B2B Payments Webinar from BlueSnapMastering B2B Payments Webinar from BlueSnap
Mastering B2B Payments Webinar from BlueSnap
Norma Mushkat Gaffin
 
ikea_woodgreen_petscharity_dog-alogue_digital.pdf
ikea_woodgreen_petscharity_dog-alogue_digital.pdfikea_woodgreen_petscharity_dog-alogue_digital.pdf
ikea_woodgreen_petscharity_dog-alogue_digital.pdf
agatadrynko
 
ModelingMarketingStrategiesMKS.CollumbiaUniversitypdf
ModelingMarketingStrategiesMKS.CollumbiaUniversitypdfModelingMarketingStrategiesMKS.CollumbiaUniversitypdf
ModelingMarketingStrategiesMKS.CollumbiaUniversitypdf
fisherameliaisabella
 

Recently uploaded (20)

How MJ Global Leads the Packaging Industry.pdf
How MJ Global Leads the Packaging Industry.pdfHow MJ Global Leads the Packaging Industry.pdf
How MJ Global Leads the Packaging Industry.pdf
 
Business storytelling: key ingredients to a story
Business storytelling: key ingredients to a storyBusiness storytelling: key ingredients to a story
Business storytelling: key ingredients to a story
 
Call 8867766396 Satta Matka Dpboss Matka Guessing Satta batta Matka 420 Satta...
Call 8867766396 Satta Matka Dpboss Matka Guessing Satta batta Matka 420 Satta...Call 8867766396 Satta Matka Dpboss Matka Guessing Satta batta Matka 420 Satta...
Call 8867766396 Satta Matka Dpboss Matka Guessing Satta batta Matka 420 Satta...
 
一比一原版新西兰奥塔哥大学毕业证(otago毕业证)如何办理
一比一原版新西兰奥塔哥大学毕业证(otago毕业证)如何办理一比一原版新西兰奥塔哥大学毕业证(otago毕业证)如何办理
一比一原版新西兰奥塔哥大学毕业证(otago毕业证)如何办理
 
Creative Web Design Company in Singapore
Creative Web Design Company in SingaporeCreative Web Design Company in Singapore
Creative Web Design Company in Singapore
 
Anny Serafina Love - Letter of Recommendation by Kellen Harkins, MS.
Anny Serafina Love - Letter of Recommendation by Kellen Harkins, MS.Anny Serafina Love - Letter of Recommendation by Kellen Harkins, MS.
Anny Serafina Love - Letter of Recommendation by Kellen Harkins, MS.
 
buy old yahoo accounts buy yahoo accounts
buy old yahoo accounts buy yahoo accountsbuy old yahoo accounts buy yahoo accounts
buy old yahoo accounts buy yahoo accounts
 
Top mailing list providers in the USA.pptx
Top mailing list providers in the USA.pptxTop mailing list providers in the USA.pptx
Top mailing list providers in the USA.pptx
 
Unveiling the Dynamic Personalities, Key Dates, and Horoscope Insights: Gemin...
Unveiling the Dynamic Personalities, Key Dates, and Horoscope Insights: Gemin...Unveiling the Dynamic Personalities, Key Dates, and Horoscope Insights: Gemin...
Unveiling the Dynamic Personalities, Key Dates, and Horoscope Insights: Gemin...
 
Satta Matka Dpboss Matka Guessing Kalyan Chart Indian Matka Kalyan panel Chart
Satta Matka Dpboss Matka Guessing Kalyan Chart Indian Matka Kalyan panel ChartSatta Matka Dpboss Matka Guessing Kalyan Chart Indian Matka Kalyan panel Chart
Satta Matka Dpboss Matka Guessing Kalyan Chart Indian Matka Kalyan panel Chart
 
Authentically Social by Corey Perlman - EO Puerto Rico
Authentically Social by Corey Perlman - EO Puerto RicoAuthentically Social by Corey Perlman - EO Puerto Rico
Authentically Social by Corey Perlman - EO Puerto Rico
 
Hamster Kombat' Telegram Game Surpasses 100 Million Players—Token Release Sch...
Hamster Kombat' Telegram Game Surpasses 100 Million Players—Token Release Sch...Hamster Kombat' Telegram Game Surpasses 100 Million Players—Token Release Sch...
Hamster Kombat' Telegram Game Surpasses 100 Million Players—Token Release Sch...
 
Best Forex Brokers Comparison in INDIA 2024
Best Forex Brokers Comparison in INDIA 2024Best Forex Brokers Comparison in INDIA 2024
Best Forex Brokers Comparison in INDIA 2024
 
Part 2 Deep Dive: Navigating the 2024 Slowdown
Part 2 Deep Dive: Navigating the 2024 SlowdownPart 2 Deep Dive: Navigating the 2024 Slowdown
Part 2 Deep Dive: Navigating the 2024 Slowdown
 
The Evolution and Impact of OTT Platforms: A Deep Dive into the Future of Ent...
The Evolution and Impact of OTT Platforms: A Deep Dive into the Future of Ent...The Evolution and Impact of OTT Platforms: A Deep Dive into the Future of Ent...
The Evolution and Impact of OTT Platforms: A Deep Dive into the Future of Ent...
 
Tata Group Dials Taiwan for Its Chipmaking Ambition in Gujarat’s Dholera
Tata Group Dials Taiwan for Its Chipmaking Ambition in Gujarat’s DholeraTata Group Dials Taiwan for Its Chipmaking Ambition in Gujarat’s Dholera
Tata Group Dials Taiwan for Its Chipmaking Ambition in Gujarat’s Dholera
 
BeMetals Investor Presentation_June 1, 2024.pdf
BeMetals Investor Presentation_June 1, 2024.pdfBeMetals Investor Presentation_June 1, 2024.pdf
BeMetals Investor Presentation_June 1, 2024.pdf
 
Mastering B2B Payments Webinar from BlueSnap
Mastering B2B Payments Webinar from BlueSnapMastering B2B Payments Webinar from BlueSnap
Mastering B2B Payments Webinar from BlueSnap
 
ikea_woodgreen_petscharity_dog-alogue_digital.pdf
ikea_woodgreen_petscharity_dog-alogue_digital.pdfikea_woodgreen_petscharity_dog-alogue_digital.pdf
ikea_woodgreen_petscharity_dog-alogue_digital.pdf
 
ModelingMarketingStrategiesMKS.CollumbiaUniversitypdf
ModelingMarketingStrategiesMKS.CollumbiaUniversitypdfModelingMarketingStrategiesMKS.CollumbiaUniversitypdf
ModelingMarketingStrategiesMKS.CollumbiaUniversitypdf
 

0-Slot18-19-20-ContiguousStorage.pdf

  • 1. Slot 18-19-20: Contiguous Storage Arrays/Simple Data Structure/Contiguous Storage/Searching Sorting
  • 2. Objectives • How to manage a group of data? • Store • Input • Output • Search • Sort
  • 3. Contents • Introduction to contiguous storage • Arrays • One-dimensional Arrays • Declaration • Memory Allocation • Initialization • Accessing elements • Traversing • 1-D Arrays are parameters of functions • Searching • Sorting • 2-D Arrays 3
  • 4. Contiguous Storage • Commonly, a group of the same meaning elements are considered. • They are stored in a contiguous block of memory. • Ex: Group of 10 int numbers ➔ 40 bytes block is needed. • Data are considered can be a group of some items which belong to some different data types → Contiguous memory block is partitioned into some parts which have different size, one part for an item. • Data structure: A structure of data stored. • Array is the simplest data structure which contains some items which belong to the same data type. • Common used operations on a group: Add, Search, Remove, Update, Sort 4 Contiguous Storage
  • 5. Arrays • Array: A group of elements which belong to the same data type. • Each element is identified by it’s position (index). • Dimension: Direction that is used to perform an action on array. • Number of dimensions: Number of indexes are used to specify an element. • Common arrays: 1-D and 2-D arrays. • Name of an array: An array has it’s name. 5 4 8 15 90 27 34 21 152 80 a (array) a[3] element 0 1 2 3 4 5 6 7 8 9 index a[i] is an integer m row column m[1][3] 5 Contiguous Storage
  • 6. One Dimensional (1-D)Arrays • 1-D array: a collection of items (elements, terms) which belong to the same data type and are stored contiguously in memory. →Each element is identified by a unique index of it’s position in the array (an integer from 0). 5 4 8 15 90 27 34 21 152 80 a (array) a[3] element 0 1 2 3 4 5 6 7 8 9 index a[i] is an integer 6 Contiguous Storage
  • 7. 1-D Arrays: Declaration • If the array is stored in the stack segment → Use a STATIC array → The compiler will determine the array’s storage at compile- time. • If the array is stored in the heap → Use a pointer (DYNAMIC array) → The array’s storage will be allocated in the heap at run- time through memory allocating functions (malloc, calloc, realloc) DataType ArrayName[NumberOfElements] ; Examples: int a1[5]; char s[12]; double a3[100]; How compilers can determine the memory size of an array? NumberOfElements * sizeof(dataType) ➔ int a1[5] → 5 *sizeof(int) = 5*4 = 20 bytes float *a; a = (float*)calloc (10, sizeof(float)); /* allocate a block of 10 float numbers */ 7 Contiguous Storage
  • 8. 1-D Arrays: Memory Allocation 20 4202496 code 4199056 20 bytes a1: 2293584 80 bytes 407249 6 Data segment Code segment Stack segment Heap 4072496 a2:2293580 The name of the array is the address of the first element. 8 Contiguous Storage
  • 9. 1-D Arrays: Initialization & Accessing Elements Initialize an array: DataType a[] = {val1, val2, … }; How to access the ith element of the array a? • a is the address of the first element. Based on operation on pointers: → a+i : address of the ith element, another way: &a[i] →*(a+i): value of the ith element, → popular way: a[i] 9 Contiguous Storage
  • 10. Compiler will automatically count number of initial values to determine the size of array memory The size of array memory is pre- defined. Compiler will fill 0 to elements which are not initialized. int a[5]; Elements contain un-predictable values because they are local variables. TEST IT !!!! 10 Contiguous Storage 1-D Arrays: Init. & Accessing…
  • 11. 1-D Arrays: Traversing • A way to visit each element of an array • Suppose that the 1-D array, named a, containingn elements. • Forward traversal: int i; for (i=0; i<n; i++) { [if (condition)] Accessa[i]; } • Backward traversal: int i; for (i=n-1; i >=0; i--) { [if (condition)] Access a[i]; } Contiguous Storage 11
  • 12. Array Function Parameter: Demo 1. • For a parameter of an array type, the value of the parameter contains a reference to an array; this reference is passed to the method. Any changes to the array that occur inside the method body will affect the original array that was passed as the argument. • Input an array of n integers: void input (int* a, int n) • Input elements of an array of integers which it’s number of element is stored at the pointer pn: void input (int a[], int*pn) • Output an array of n double numbers: • void output (double a[], int n) • Calculate the sum of an array of n integers: int sum (int *a, int n) 12 Contiguous Storage
  • 13. Array Function Parameter: Demo 1. • Develop a C-program that will: • Accept values to an integer array that may contain 100 elements. • Print out the it’s maximum value. • Print out it’s elements. • Print out it’s even values. • Nouns: – Constant: MAXN=100 –Static array of integers → int a[MAXN] –Real number of elements → int n –Maximum value → int maxVal. • Verbs: -Begin -Input n (one value) -Input a, n (function) - maxVal = get maximum value in a, n (function) - Print out maxVal (one value) - Print out a, n (function) - Print even values in a, n (function) - End 13 Contiguous Storage
  • 14. Array Function Parameter: Demo 1. 14 Contiguous Storage
  • 15. Array Function Parameter: Demo 1. 15 Contiguous Storage
  • 16. When using a dynamic array? • Comments • If you allocate an array having 100 elements but 6 elements are used then memory is wasted. • If If you allocate an array having 100 elements but 101 elements are used then there is a lack of memory. • Solution: Use a dynamic array. 16 Contiguous Storage
  • 17. Array Function Parameter: Demo 1. 17 Contiguous Storage
  • 18. Array Function Parameter: Demo 1. 18 Contiguous Storage
  • 19. Array Function Parameter: Demo 2. • Develop a C-program that will: -Accept values to an integer array that may contains 100 elements. The input will terminate when user enters the value of zero. - Print out the it’s maximum value. - Print out it’s elements. - Print out it’s even values. The difference between this problem with the previous one is the input operation can terminate abruptly when 0 is accepted. ➔Memory block of the array needs to be allocated in excess ➔The function for input values of the array must be modified for this case and the number of elements is updated after each valid value is accepted. 19 Contiguous Storage
  • 20. Array Function Parameter: Demo 2. 20 Contiguous Storage
  • 21. Array Function Parameter: Demo 2. 0 3 n=0 → 1 x=3 0 1 2 3 5 2 n=3→ 4 x=7 21 Contiguous Storage
  • 22. 1-D Arrays: Searching • A search algorithm finds the record of interest using the key array • Return value: The positional index at which the interest value is found. • Two common search algorithms are • Linear search • find whether a given number is present in an array and if it is present then at what location it occurs. • binary search • Search a sorted array by repeatedly dividing the search interval in half. • If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half. Otherwise narrow it to the upper half. • Repeatedly check until the value is found or the interval is empty. 22 Contiguous Storage
  • 24. 1-D Arrays: Searching… • Linear search: Find the position of the value x in the array a having n elements. 5 9 2 7 6 5 2 5 i=0 1 2 3 4 • Search the value of 6 in the array a having 8 items 5 9 2 7 6 5 2 5 i=0 1 2 3 4 5 6 7 • Search the value of 12 in the array a having 8 items -1 There may be n comparisons performed. 24 Contiguous Storage
  • 25. 1-D Arrays: Linear Searching… 25 Contiguous Storage
  • 26. 1-D Arrays: Binary Searching… Binary search - Condition for application: Values in the array were sorted. return c (7) i>j → return -1 26 Contiguous Storage
  • 27. 1-D Arrays: Binary Searching… No. of elements considered No. of comparisons n= 2m 1 2m-1 1 2m-2 1 … … 20 1 Sum m+1 = log2(n) +1 Evaluation: 27 Contiguous Storage
  • 28. 1-D Arrays: Sorting • Sorting: Changing positions of elements in an array so that values are in a order based on a pre-defined order relation. • Default order relation in set of numbers: Value order • Default order relation in a set of characters/ strings: Dictionary order • Only two sorting algorithms are introduced here. • Selection Sort • Bubble Sort 28 Contiguous Storage
  • 29. 1-D Arrays: Selection Sort • Find the minimum value in the list • Swap it with the value in the first position • Repeat the steps above for remainder of the list 29 Contiguous Storage
  • 30. 1-D Arrays: Selection Sort 30 Contiguous Storage
  • 31. 1-D Arrays: Bubble Sort •It works by repeatedly stepping through the list to be sorted, comparing two items at a time and swapping them if they are in the wrong order. •The pass through the list is repeated until no swaps are needed, which means the list is sorted 31 Contiguous Storage
  • 32. 1-D Arrays: Bubble Sort… 32 Contiguous Storage
  • 33. 1-D Arrays: A Sample • Develop a C-program that helps user managing an 1-D array of integers (maximum of 100 elements) using the following simple menu: • 1- Add a value • 2- Search a value • 3- Remove the first existence of a value • 4- Remove all existences of a value • 5- Print out the array • 6- Print out the array in ascending order (positions of elements are preserved) • 7- Print out the array in descending order (positions of elements are preserved) • Others- Quit 33 Contiguous Storage
  • 34. 1-D Arrays: A Sample… • In this program, user can freely add or remove one or more elements to/ from the array. So, an extra memory allocation is needed (100 items). • Data: Array of integers → int a[100], n searched/added/removed number → int value • Functions: • int menu() → Get user choice • int isFull(int *a, int n) - Testing whether an array is full or not • int isEmpty(int *a, int n) - Testing whether an array is empty or not • void add(int x, int*a, int*pn) → adding an element to the array will increase number of elements • int search(int x, int *a, int n) → return a position found in the array • int removeOne (int pos, int*a, int*pn) → Removing a value at the position pos will decrease number of elements → return 1: successfully, 0: fail • int remove All(int x, int*a, int*pn) → Removing a value will decrease number of elements → return 1: successfully, 0: fail • void printAsc(int*a, int n) – printing array, elements are preserved • void printDesc(int*a, int n) – printing array, elements are preserved • void print(int*a, int n) 34 Contiguous Storage
  • 35. 1-D Arrays: A Sample… After values 0 , 2, 8, 9, 7, 3, 2, 4, 2 are added. Use menu 5 to view them. Search option Remove one option 35 Contiguous Storage
  • 36. 1-D Arrays: A Sample… Remove all option Print out in ascending order (elements are preserved) Print out in descending order (elements are preserved) 36 Contiguous Storage
  • 37. 1-D Arrays: A Sample… 37 Contiguous Storage
  • 38. 1-D Arrays: A Sample… 0 1 2 3 4 5 6 7 8 0 2 9 7 3 2 4 2 2 9 7 3 2 4 2 2 index 38 Contiguous Storage
  • 39. 1-D Arrays: A Sample… 39 Contiguous Storage
  • 40. 1-D Arrays: A Sample… 3 4 2 1 5 4223516 2293496 2293488 2293492 2293504 2293500 2293488 2293492 2293496 2293500 2293504 adds 4223516 2293488 2293492 2293496 2293500 2293504 Values of pointers after sorting 40 Contiguous Storage
  • 41. 1-D Arrays: A Sample… 41 Contiguous Storage
  • 42. 1-D Arrays: A Sample… 42 Contiguous Storage
  • 43. 1-D Arrays: A Sample… 43 Contiguous Storage
  • 44. 1-D Arrays: A Sample… 44 Contiguous Storage
  • 45. Two-Dimensional Arrays • A group of elements which belong the same data type and they are divided into some rows and some column (it is called as matrix also). • Each element is identified by two indexes (index of row, index of column). m row column m[1][3] Traversing a matrix: for ( i =0; i<row; i++) { for ( j=0; j< column; j++) [if (condition)] Access m[i][j]; } The next slide will demonstrate how static and dynamic 2-D arrays are stored. 45 Contiguous Storage
  • 46. 2-D Arrays: Memory Structure 4078616 2293488 2293492 2293496 2293500 2293504 2293508 2293512 2293520 2293524 2293528 2293532 2293516 2293596 4078824 4078784 4078744 4078720 2293600 p m1 m2 4078752 4078760 4078768 4078784 4078792 4078800 4078808 4078832 4078840 4078848 4078824 4078744 4078720 4078616 H E A P S T A C K 46 Contiguous Storage
  • 47. Static 2-D Arrays Demo. Accept a matrix maximum 20x20. Print out maximum value in it, print out the matrix. Keep in your mind the way to specify a matrix as a parameter of a function ( the number of column must be pre-defined.). 47 Contiguous Storage
  • 48. Static 2-D Arrays Demo. 48 Contiguous Storage
  • 49. Summary • Array is the simplest data structure for a group of elements which belong to the same data type. • Each element in an array is identified by one or more index beginning from 0. • Number of dimensions: Number of indexes are used to identify an element. • Static arrays → Stack segment Type a[MAXN]; Type m[MAXROW][MAXCOL]; • Dynamic array: Use pointer and allocate memory using functions double *a = (double*)calloc(n, sizeof(double)); int** m = (int**) calloc(row, sizeof(int*)); for (i=0; i<row; i++) m[i]= (int*)calloc(col, sizeof(int)); 49 Contiguous Storage
  • 50. Summary • Accessing elements in an array: 1-D Array (a) 2-D Array (m) Address Value Address Value &a[index] a[index] &m[i][j] m[i][j] a+index *(a+index) Compiler determines the address of an element: a + index*sizeof(DataType) m + (i*NumCol + j)*sizeof(DataType) • Common operations on arrays: • Add an element • Search an element • Remove an element • Input • Output • Sort • Base of algorithms on arrays: Traversing 50 Contiguous Storage
  • 51. Exercise- Do yourself • Develop a C-program that helps user managing an 1-D array of real numbers(maximum of 100 elements) using the following simple menu: • 1- Add a value • 2- Search a value • 3- Print out the array • 4- Print out values in a range (minVal<=value<=maxVal, minVal and maxVal are inputted) • 5- Print out the array in ascending order (positions of elements are preserved) • Others- Quit 51 Contiguous Storage