SlideShare a Scribd company logo
1 of 44
CHAPTER 2 1
CHAPTER 2
ARRAYS AND STRUCTURES
All the programs in this file are selected from
Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed
“Fundamentals of Data Structures in C”,
Computer Science Press, 1992.
CHAPTER 2 2
Arrays
Array: a set of index and value
data structure
For each index, there is a value associated with
that index.
representation (possible)
implemented by using consecutive memory.
CHAPTER 2 3
objects: A set of pairs <index, value> where for each value of index
there is a value from the set item. Index is a finite ordered set of one or
more dimensions, for example, {0, … , n-1} for one dimension,
{(0,0),(0,1),(0,2),(1,0),(1,1),(1,2),(2,0),(2,1),(2,2)} for two dimensions,
etc.
Functions:
for all A ∈ Array, i ∈ index, x ∈ item, j, size ∈ integer
Array Create(j, list) ::= return an array of j dimensions where list is a
j-tuple whose ith element is the size of the
ith dimension. Items are undefined.
Item Retrieve(A, i) ::= if (i ∈ index) return the item associated with
index value i in array A
else return error
Array Store(A, i, x) ::= if (i in index)
return an array that is identical to array
A except the new pair <i, x> has been
inserted else return error
end array
CHAPTER 2 4
Arrays in C
int list[5], *plist[5];
list[5]: five integers
list[0], list[1], list[2], list[3], list[4]
*plist[5]: five pointers to integers
plist[0], plist[1], plist[2], plist[3], plist[4]
implementation of 1-D array
list[0] base address = α
list[1] α + sizeof(int)
list[2] α + 2*sizeof(int)
list[3] α + 3*sizeof(int)
list[4] α + 4*size(int)
CHAPTER 2 5
Arrays in C (Continued)
Compare int *list1 and int list2[5] in C.
Same: list1 and list2 are pointers.
Difference: list2 reserves five locations.
Notations:
list2 - a pointer to list2[0]
(list2 + i) - a pointer to list2[i] (&list2[i])
*(list2 + i) - list2[i]
CHAPTER 2 6
Example: 1-dimension array addressing
int one[] = {0, 1, 2, 3, 4};
Goal: print out address and value
void print1(int *ptr, int rows)
{
/* print out a one-dimensional array using a pointer */
int i;
printf(“Address Contentsn”);
for (i=0; i < rows; i++)
printf(“%8u%5dn”, ptr+i, *(ptr+i));
printf(“n”);
}
CHAPTER 2 7
Address Contents
1228 0
1230 1
1232 2
1234 3
1236 4
*Figure 2.1: One- dimensional array addressing (p.53)
call print1(&one[0], 5)
CHAPTER 2 8
Structures (records)
struct {
char name[10];
int age;
float salary;
} person;
strcpy(person.name, “james”);
person.age=10;
person.salary=35000;
CHAPTER 2 9
Create structure data type
typedef struct human_being {
char name[10];
int age;
float salary;
};
or
typedef struct {
char name[10];
int age;
float salary
} human_being;
human_being person1, person2;
CHAPTER 2 10
Unions
Similar to struct, but only one field is active.
Example: Add fields for male and female.
typedef struct sex_type {
enum tag_field {female, male} sex;
union {
int children;
int beard;
} u;
};
typedef struct human_being {
char name[10];
int age;
float salary;
date dob;
sex_type sex_info;
}
human_being person1, person2;
person1.sex_info.sex=male;
person1.sex_info.u.beard=FALSE;
CHAPTER 2 11
Self-Referential Structures
One or more of its components is a pointer to itself.
typedef struct list {
char data;
list *link;
}
list item1, item2, item3;
item1.data=‘a’;
item2.data=‘b’;
item3.data=‘c’;
item1.link=item2.link=item3.link=NULL;
Construct a list with three nodes
item1.link=&item2;
item2.link=&item3;
malloc: obtain a node
a b c
CHAPTER 2 12
Ordered List Examples
(MONDAY, TUEDSAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAYY,
SUNDAY)
(2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen,
King,
Ace)
(1941, 1942, 1943, 1944, 1945)
(a1, a2, a3, …, an-1, an)
ordered (linear) list: (item1, item2, item3, …, itemn)
CHAPTER 2 13
Operations on Ordered List
Find the length, n , of the list.
Read the items from left to right (or right to left).
Retrieve the i’th element.
Store a new value into the i’th position.
Insert a new element at the position i , causing
elements numbered i, i+1, …, n to become numbered
i+1, i+2, …, n+1
Delete the element at position i , causing elements
numbered i+1, …, n to become numbered i, i+1, …,
n-1 array (sequential mapping)? (1)~(4) O (5)~(6) X
CHAPTER 2 14
Structure Polynomial is
objects: ; a set of ordered pairs of
<ei,ai> where ai in Coefficients and ei in Exponents, ei are integers >= 0
functions:
for all poly, poly1, poly2  Polynomial, coef Coefficients, expon
Exponents
Polynomial Zero( ) ::= return the polynomial,
p(x) = 0
Boolean IsZero(poly) ::= if (poly) return FALSE
else return TRUE
Coefficient Coef(poly, expon) ::= if (expon  poly) return its
coefficient else return Zero
Exponent Lead_Exp(poly) ::= return the largest exponent in
poly
Polynomial Attach(poly,coef, expon) ::= if (expon  poly) return error
else return the polynomial poly
with the term <coef, expon>
inserted
ne
n
e
xaxaxp ++= ...)( 1
1
Polynomials A(X)=3X20
+2X5
+4, B(X)=X4
+10X3
+3X2
+1
CHAPTER 2 15
Polynomial Remove(poly, expon) ::= if (expon  poly) return the
polynomial poly with the
term whose exponent is
expon deleted
else return error
Polynomial SingleMult(poly, coef, expon) ::= return the polynomial
poly • coef • xexpon
Polynomial Add(poly1, poly2) ::= return the polynomial
poly1 +poly2
Polynomial Mult(poly1, poly2) ::= return the polynomial
poly1 • poly2
*Structure 2.2:Abstract data type Polynomial (p.61)
End Polynomial
CHAPTER 2 16
/* d =a + b, where a, b, and d are polynomials */
d = Zero( )
while (! IsZero(a) && ! IsZero(b)) do {
switch COMPARE (Lead_Exp(a), Lead_Exp(b)) {
case -1: d =
Attach(d, Coef (b, Lead_Exp(b)), Lead_Exp(b));
b = Remove(b, Lead_Exp(b));
break;
case 0: sum = Coef (a, Lead_Exp (a)) + Coef ( b, Lead_Exp(b));
if (sum) {
Attach (d, sum, Lead_Exp(a));
a = Remove(a , Lead_Exp(a));
b = Remove(b , Lead_Exp(b));
}
break;
Polynomial Addition
#define MAX_DEGREE 101
typedef struct {
int degree;
float coef[MAX_DEGREE];
} polynomial;
data structure 1:
CHAPTER 2 17
case 1: d =
Attach(d, Coef (a, Lead_Exp(a)), Lead_Exp(a));
a = Remove(a, Lead_Exp(a));
}
}
insert any remaining terms of a or b into d
*Program 2.4 :Initial version of padd function(p.62)
advantage: easy implementation
disadvantage: waste space when sparse
CHAPTER 2 18
Data structure 2: use one global array to store all polynomials
A(X)=2X1000
+1
B(X)=X4
+10X3
+3X2
+1
2 1 1 10 3 1
1000 0 4 3 2 0
coef
exp
starta finisha startb finishb avail
0 1 2 3 4 5 6
*Figure 2.2: Array representation of two polynomials
(p.63)
specification representation
poly <start, finish>
A <0,1>
B <2,5>
CHAPTER 2 19
MAX_TERMS 100 /* size of terms array */
typedef struct {
float coef;
int expon;
} polynomial;
polynomial terms[MAX_TERMS];
int avail = 0;
*(p.62)
storage requirements: start, finish, 2*(finish-start+1)
nonparse: twice as much as (1)
when all the items are nonzero
CHAPTER 2 20
void padd (int starta, int finisha, int startb, int finishb,
int * startd, int *finishd)
{
/* add A(x) and B(x) to obtain D(x) */
float coefficient;
*startd = avail;
while (starta <= finisha && startb <= finishb)
switch (COMPARE(terms[starta].expon,
terms[startb].expon)) {
case -1: /* a expon < b expon */
attach(terms[startb].coef, terms[startb].expon);
startb++
break;
Add two polynomials: D = A + B
CHAPTER 2 21
case 0: /* equal exponents */
coefficient = terms[starta].coef +
terms[startb].coef;
if (coefficient)
attach (coefficient, terms[starta].expon);
starta++;
startb++;
break;
case 1: /* a expon > b expon */
attach(terms[starta].coef, terms[starta].expon);
starta++;
}
CHAPTER 2 22
/* add in remaining terms of A(x) */
for( ; starta <= finisha; starta++)
attach(terms[starta].coef, terms[starta].expon);
/* add in remaining terms of B(x) */
for( ; startb <= finishb; startb++)
attach(terms[startb].coef, terms[startb].expon);
*finishd =avail -1;
}
*Program 2.5: Function to add two polynomial (p.64)
Analysis: O(n+m)
where n (m) is the number of nonzeros in A(B).
CHAPTER 2 23
void attach(float coefficient, int exponent)
{
/* add a new term to the polynomial */
if (avail >= MAX_TERMS) {
fprintf(stderr, “Too many terms in the polynomialn”);
exit(1);
}
terms[avail].coef = coefficient;
terms[avail++].expon = exponent;
}
*Program 2.6:Function to add anew term (p.65)
Problem: Compaction is required
when polynomials that are no longer needed.
(data movement takes time.)
CHAPTER 2 24




















−
−
0002800
0000091
000000
006000
0003110
150220015
col1 col2 col3 col4 col5 col6
row0
row1
row2
row3
row4
row5
(a) (b)
*Figure 2.3:Two matrices
8/36
6*65*3
15/15
Sparse Matrix
sparse matrix
data structure?
CHAPTER 2 25
Structure Sparse_Matrix is
objects: a set of triples, <row, column, value>, where row
and column are integers and form a unique combination, and
value comes from the set item.
functions:
for all a, b ∈ Sparse_Matrix, x  item, i, j, max_col,
max_row  index
Sparse_Marix Create(max_row, max_col) ::=
return a Sparse_matrix that can hold up to
max_items = max _row  max_col and
whose maximum row size is max_row and
whose maximum column size is max_col.
SPARSE MATRIX ABSTRACT DATA TYPE
CHAPTER 2 26
Sparse_Matrix Transpose(a) ::=
return the matrix produced by interchanging
the row and column value of every triple.
Sparse_Matrix Add(a, b) ::=
if the dimensions of a and b are the same
return the matrix produced by adding
corresponding items, namely those with
identical row and column values.
else return error
Sparse_Matrix Multiply(a, b) ::=
if number of columns in a equals number of
rows in b
return the matrix d produced by multiplying
a by b according to the formula: d [i] [j] =
(a[i][k]•b[k][j]) where d (i, j) is the (i,j)th
element
else return error.
* Structure 2.3: Abstract data type Sparse-Matrix (p.68)
CHAPTER 2 27
row col value row col value
a[0] 6 6 8 b[0] 6 6 8
[1] 0 0 15 [1] 0 0 15
[2] 0 3 22 [2] 0 4 91
[3] 0 5 -15 [3] 1 1 11
[4] 1 1 11 [4] 2 1 3
[5] 1 2 3 [5] 2 5 28
[6] 2 3 -6 [6] 3 0 22
[7] 4 0 91 [7] 3 2 -6
[8] 5 2 28 [8] 5 0 -15
(a) (b)
*Figure 2.4:Sparse matrix and its transpose stored as triples (p.69)
(1) Represented by a two-dimensional array.
Sparse matrix wastes space.
(2) Each element is characterized by <row, col, value>.
row, column in ascending order
# of rows (columns)
# of nonzero terms
transpose
CHAPTER 2 28
Sparse_matrix Create(max_row, max_col) ::=
#define MAX_TERMS 101 /* maximum number of terms +1*/
typedef struct {
int col;
int row;
int value;
} term;
term a[MAX_TERMS]
* (P.69)
# of rows (columns)
# of nonzero terms
CHAPTER 2 29
Transpose a Matrix
(1) for each row i
take element <i, j, value> and store it
in element <j, i, value> of the transpose.
difficulty: where to put <j, i, value>
(0, 0, 15) ====> (0, 0, 15)
(0, 3, 22) ====> (3, 0, 22)
(0, 5, -15) ====> (5, 0, -15)
(1, 1, 11) ====> (1, 1, 11)
Move elements down very often.
(2) For all elements in column j,
place element <i, j, value> in element <j, i, value>
CHAPTER 2 30
void transpose (term a[], term b[])
/* b is set to the transpose of a */
{
int n, i, j, currentb;
n = a[0].value; /* total number of elements */
b[0].row = a[0].col; /* rows in b = columns in a */
b[0].col = a[0].row; /*columns in b = rows in a */
b[0].value = n;
if (n > 0) { /*non zero matrix */
currentb = 1;
for (i = 0; i < a[0].col; i++)
/* transpose by columns in a */
for( j = 1; j <= n; j++)
/* find elements from the current column */
if (a[j].col == i) {
/* element is in current column, add it to b */
CHAPTER 2 31
b[currentb].row = a[j].col;
b[currentb].col = a[j].row;
b[currentb].value = a[j].value;
currentb++
}
}
}
* Program 2.7: Transpose of a sparse matrix (p.71)
elements
columns
Scan the array “columns” times.
The array has “elements” elements.
==> O(columns*elements)
CHAPTER 2 32
Discussion: compared with 2-D array representation
O(columns*elements) vs. O(columns*rows)
elements --> columns * rows when nonsparse
O(columns*columns*rows)
Problem: Scan the array “columns” times.
Solution:
Determine the number of elements in each column of
the original matrix.
==>
Determine the starting positions of each row in the
transpose matrix.
CHAPTER 2 33
[0] [1] [2] [3] [4] [5]
row_terms = 2 1 2 2 0 1
starting_pos = 1 3 4 6 8 8
a[0] 6 6 8
a[1] 0 0 15
a[2] 0 3 22
a[3] 0 5 -15
a[4] 1 1 11
a[5] 1 2 3
a[6] 2 3 -6
a[7] 4 0 91
a[8] 5 2 28
CHAPTER 2 34
void fast_transpose(term a[ ], term b[ ])
{
/* the transpose of a is placed in b */
int row_terms[MAX_COL], starting_pos[MAX_COL];
int i, j, num_cols = a[0].col, num_terms = a[0].value;
b[0].row = num_cols; b[0].col = a[0].row;
b[0].value = num_terms;
if (num_terms > 0){ /*nonzero matrix*/
for (i = 0; i < num_cols; i++)
row_terms[i] = 0;
for (i = 1; i <= num_terms; i++)
row_term [a[i].col]++
starting_pos[0] = 1;
for (i =1; i < num_cols; i++)
starting_pos[i]=starting_pos[i-1] +row_terms [i-1];
columns
elements
columns
CHAPTER 2 35
for (i=1; i <= num_terms, i++) {
j = starting_pos[a[i].col]++;
b[j].row = a[i].col;
b[j].col = a[i].row;
b[j].value = a[i].value;
}
}
}
*Program 2.8:Fast transpose of a sparse matrix
elements
Compared with 2-D array representation
O(columns+elements) vs. O(columns*rows)
elements --> columns * rows
O(columns+elements) --> O(columns*rows)
Cost: Additional row_terms and starting_pos arrays are required.
Let the two arrays row_terms and starting_pos be shared.
CHAPTER 2 36










=




















111
111
111
000
000
111
001
001
001
*Figure 2.5:Multiplication of two sparse matrices (p.73)
Sparse Matrix Multiplication
Definition: [D]m*p=[A]m*n* [B]n*p
Procedure: Fix a row of A and find all elements in column j
of B for j=0, 1, …, p-1.
Alternative 1. Scan all of B to find all elements in j.
Alternative 2. Compute the transpose of B.
(Put all column elements consecutively)
CHAPTER 2 37
A = 1 0 2 B = 3 0 2
-1 4 6 -1 0 0
0 0 5
2 3 5 3 3 4
0 0 1 0 0 3
0 2 2 0 2 2
1 0 -1 1 0 -1
1 1 4 2 2 5
1 2 6
BT
= 3 -1 0 3 3 4
0 0 0 0 0 3
2 0 5 0 1 -1
2 0 2
2 2 5
(0,0)
(0,2)
(1,0)
(1,2)
An Example
CHAPTER 2 38
General Case
dij=ai0*b0j+ai1*b1j+…+ai(n-1)*b(n-1)j
a 本來依 i 成群,經轉置後, b 也依 j 成群。
a Sa d Sd
b Sb e Se
c Sc f Sf
g Sg
最多可以產生 ad, ae, af, ag,
bd, be, bf, bg,
cd, ce, cf, cg 等 entries 。
CHAPTER 2 39
void mmult (term a[ ], term b[ ], term d[ ] )
/* multiply two sparse matrices */
{
int i, j, column, totalb = b[].value, totald = 0;
int rows_a = a[0].row, cols_a = a[0].col,
totala = a[0].value; int cols_b = b[0].col,
int row_begin = 1, row = a[1].row, sum =0;
int new_b[MAX_TERMS][3];
if (cols_a != b[0].row){
fprintf (stderr, “Incompatible matricesn”);
exit (1);
}
CHAPTER 2 40
fast_transpose(b, new_b);
/* set boundary condition */
a[totala+1].row = rows_a;
new_b[totalb+1].row = cols_b;
new_b[totalb+1].col = 0;
for (i = 1; i <= totala; ) {
column = new_b[1].row;
for (j = 1; j <= totalb+1;) {
/* mutiply row of a by column of b */
if (a[i].row != row) {
storesum(d, &totald, row, column, &sum);
i = row_begin;
for (; new_b[j].row == column; j++)
;
column =new_b[j].row
}
cols_b + totalb
at most rows_a times
CHAPTER 2 41
else switch (COMPARE (a[i].col, new_b[j].col)) {
case -1: /* go to next term in a */
i++; break;
case 0: /* add terms, go to next term in a and b */
sum += (a[i++].value * new_b[j++].value);
break;
case 1: /* advance to next term in b*/
j++
}
} /* end of for j <= totalb+1 */
for (; a[i].row == row; i++)
;
row_begin = i; row = a[i].row;
} /* end of for i <=totala */
d[0].row = rows_a;
d[0].col = cols_b; d[0].value = totald;
} *Praogram 2.9: Sparse matrix multiplication (p.75)
CHAPTER 2 42
Analyzing the algorithm
cols_b * termsrow1 + totalb +
cols_b * termsrow2 + totalb +
… +
cols_b * termsrowp + totalb
= cols_b * (termsrow1 + termsrow2 + … + termsrowp) +
rows_a * totalb
= cols_b * totala + row_a * totalb
O(cols_b * totala + rows_a * totalb)
CHAPTER 2 43
for (i =0; i < rows_a; i++)
for (j=0; j < cols_b; j++) {
sum =0;
for (k=0; k < cols_a; k++)
sum += (a[i][k] *b[k][j]);
d[i][j] =sum;
}
Compared with matrix multiplication using array
O(rows_a * cols_a * cols_b) vs.
O(cols_b * total_a + rows_a * total_b)
optimal case: total_a < rows_a * cols_a
total_b < cols_a * cols_b
worse case: total_a --> rows_a * cols_a, or
total_b --> cols_a * cols_b
CHAPTER 2 44
void storesum(term d[ ], int *totald, int row, int column,
int *sum)
{
/* if *sum != 0, then it along with its row and column
position is stored as the *totald+1 entry in d */
if (*sum)
if (*totald < MAX_TERMS) {
d[++*totald].row = row;
d[*totald].col = column;
d[*totald].value = *sum;
}
else {
fprintf(stderr, ”Numbers of terms in product
exceed %dn”, MAX_TERMS);
exit(1);
}
}
Program 2.10: storsum function

More Related Content

What's hot

Multidimensional array in C
Multidimensional array in CMultidimensional array in C
Multidimensional array in CSmit Parikh
 
Introduction to NumPy for Machine Learning Programmers
Introduction to NumPy for Machine Learning ProgrammersIntroduction to NumPy for Machine Learning Programmers
Introduction to NumPy for Machine Learning ProgrammersKimikazu Kato
 
Python3 cheatsheet
Python3 cheatsheetPython3 cheatsheet
Python3 cheatsheetGil Cohen
 
Data structure array
Data structure  arrayData structure  array
Data structure arrayMajidHamidAli
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to ArraysTareq Hasan
 
Arrays in java language
Arrays in java languageArrays in java language
Arrays in java languageHareem Naz
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional arrayRajendran
 
Python 2.5 reference card (2009)
Python 2.5 reference card (2009)Python 2.5 reference card (2009)
Python 2.5 reference card (2009)gekiaruj
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : ArraysGagan Deep
 
Cheat sheet python3
Cheat sheet python3Cheat sheet python3
Cheat sheet python3sxw2k
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayimtiazalijoono
 
Leet Code May Coding Challenge - DataStructure and Algorithm Problems
Leet Code May Coding Challenge - DataStructure and Algorithm ProblemsLeet Code May Coding Challenge - DataStructure and Algorithm Problems
Leet Code May Coding Challenge - DataStructure and Algorithm ProblemsSunil Yadav
 
Pandas pythonfordatascience
Pandas pythonfordatasciencePandas pythonfordatascience
Pandas pythonfordatascienceNishant Upadhyay
 
LeetCode April Coding Challenge
LeetCode April Coding ChallengeLeetCode April Coding Challenge
LeetCode April Coding ChallengeSunil Yadav
 

What's hot (20)

Multidimensional array in C
Multidimensional array in CMultidimensional array in C
Multidimensional array in C
 
Introduction to NumPy for Machine Learning Programmers
Introduction to NumPy for Machine Learning ProgrammersIntroduction to NumPy for Machine Learning Programmers
Introduction to NumPy for Machine Learning Programmers
 
Python3 cheatsheet
Python3 cheatsheetPython3 cheatsheet
Python3 cheatsheet
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
Data structure array
Data structure  arrayData structure  array
Data structure array
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
 
Arrays in java language
Arrays in java languageArrays in java language
Arrays in java language
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
 
Java arrays
Java    arraysJava    arrays
Java arrays
 
2D Array
2D Array 2D Array
2D Array
 
Array lecture
Array lectureArray lecture
Array lecture
 
Python 2.5 reference card (2009)
Python 2.5 reference card (2009)Python 2.5 reference card (2009)
Python 2.5 reference card (2009)
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : Arrays
 
2- Dimensional Arrays
2- Dimensional Arrays2- Dimensional Arrays
2- Dimensional Arrays
 
Cheat sheet python3
Cheat sheet python3Cheat sheet python3
Cheat sheet python3
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
 
Leet Code May Coding Challenge - DataStructure and Algorithm Problems
Leet Code May Coding Challenge - DataStructure and Algorithm ProblemsLeet Code May Coding Challenge - DataStructure and Algorithm Problems
Leet Code May Coding Challenge - DataStructure and Algorithm Problems
 
Pandas pythonfordatascience
Pandas pythonfordatasciencePandas pythonfordatascience
Pandas pythonfordatascience
 
Numpy python cheat_sheet
Numpy python cheat_sheetNumpy python cheat_sheet
Numpy python cheat_sheet
 
LeetCode April Coding Challenge
LeetCode April Coding ChallengeLeetCode April Coding Challenge
LeetCode April Coding Challenge
 

Viewers also liked

D85 digital magazine april 2015
D85 digital magazine   april 2015D85 digital magazine   april 2015
D85 digital magazine april 2015Saleem Khanani
 
TADRESS FARSI THROUGH IQBAL VOLUME 2
TADRESS FARSI THROUGH IQBAL VOLUME 2TADRESS FARSI THROUGH IQBAL VOLUME 2
TADRESS FARSI THROUGH IQBAL VOLUME 2Saleem Khanani
 
Power prospecting keynote
Power prospecting keynotePower prospecting keynote
Power prospecting keynoteBo Barron
 
Presentación1
Presentación1Presentación1
Presentación1N3GRI70
 
D85 Digital Magazine May 2015
D85 Digital Magazine May 2015D85 Digital Magazine May 2015
D85 Digital Magazine May 2015Saleem Khanani
 
D85 Digital Magazine, May 2014. An Anthology of Poetry by Dowites
D85 Digital Magazine, May 2014. An Anthology of Poetry by DowitesD85 Digital Magazine, May 2014. An Anthology of Poetry by Dowites
D85 Digital Magazine, May 2014. An Anthology of Poetry by DowitesSaleem Khanani
 
Landscape Architecture Portfolio Barbara Pyda 2013
Landscape Architecture Portfolio Barbara Pyda 2013Landscape Architecture Portfolio Barbara Pyda 2013
Landscape Architecture Portfolio Barbara Pyda 2013Barbara Pyda
 
33269 lfi0512 e_1023_port_tepito_rz copy
33269 lfi0512 e_1023_port_tepito_rz copy33269 lfi0512 e_1023_port_tepito_rz copy
33269 lfi0512 e_1023_port_tepito_rz copyAdriana Zehbrauskas
 
D85 Digital Magazine September 2015
D85 Digital Magazine September 2015D85 Digital Magazine September 2015
D85 Digital Magazine September 2015Saleem Khanani
 
D85 digital magazine July - August 2015
D85 digital magazine July - August 2015D85 digital magazine July - August 2015
D85 digital magazine July - August 2015Saleem Khanani
 
Barbara Pyda Landscape Architecture portfolio
Barbara Pyda Landscape Architecture portfolioBarbara Pyda Landscape Architecture portfolio
Barbara Pyda Landscape Architecture portfolioBarbara Pyda
 
Greedyalgorithm
Greedyalgorithm Greedyalgorithm
Greedyalgorithm Diksha Lad
 
2012 Tfa Infographic Advisors Bpm
2012 Tfa Infographic Advisors Bpm2012 Tfa Infographic Advisors Bpm
2012 Tfa Infographic Advisors Bpmericchau1105
 
Landscape architecture portfolio barbara pyda
Landscape architecture portfolio   barbara pydaLandscape architecture portfolio   barbara pyda
Landscape architecture portfolio barbara pydaBarbara Pyda
 
Fallsem2015 16 cp4194-13-oct-2015_rm01_graphs
Fallsem2015 16 cp4194-13-oct-2015_rm01_graphsFallsem2015 16 cp4194-13-oct-2015_rm01_graphs
Fallsem2015 16 cp4194-13-oct-2015_rm01_graphsSnehilKeshari
 

Viewers also liked (17)

D85 digital magazine april 2015
D85 digital magazine   april 2015D85 digital magazine   april 2015
D85 digital magazine april 2015
 
TADRESS FARSI THROUGH IQBAL VOLUME 2
TADRESS FARSI THROUGH IQBAL VOLUME 2TADRESS FARSI THROUGH IQBAL VOLUME 2
TADRESS FARSI THROUGH IQBAL VOLUME 2
 
Power prospecting keynote
Power prospecting keynotePower prospecting keynote
Power prospecting keynote
 
Presentación1
Presentación1Presentación1
Presentación1
 
D85 Digital Magazine May 2015
D85 Digital Magazine May 2015D85 Digital Magazine May 2015
D85 Digital Magazine May 2015
 
D85 Digital Magazine, May 2014. An Anthology of Poetry by Dowites
D85 Digital Magazine, May 2014. An Anthology of Poetry by DowitesD85 Digital Magazine, May 2014. An Anthology of Poetry by Dowites
D85 Digital Magazine, May 2014. An Anthology of Poetry by Dowites
 
Landscape Architecture Portfolio Barbara Pyda 2013
Landscape Architecture Portfolio Barbara Pyda 2013Landscape Architecture Portfolio Barbara Pyda 2013
Landscape Architecture Portfolio Barbara Pyda 2013
 
33269 lfi0512 e_1023_port_tepito_rz copy
33269 lfi0512 e_1023_port_tepito_rz copy33269 lfi0512 e_1023_port_tepito_rz copy
33269 lfi0512 e_1023_port_tepito_rz copy
 
D85 Digital Magazine September 2015
D85 Digital Magazine September 2015D85 Digital Magazine September 2015
D85 Digital Magazine September 2015
 
D85 digital magazine July - August 2015
D85 digital magazine July - August 2015D85 digital magazine July - August 2015
D85 digital magazine July - August 2015
 
Barbara Pyda Landscape Architecture portfolio
Barbara Pyda Landscape Architecture portfolioBarbara Pyda Landscape Architecture portfolio
Barbara Pyda Landscape Architecture portfolio
 
Tda8947 j
Tda8947 jTda8947 j
Tda8947 j
 
Greedyalgorithm
Greedyalgorithm Greedyalgorithm
Greedyalgorithm
 
(floyd's algm)
(floyd's algm)(floyd's algm)
(floyd's algm)
 
2012 Tfa Infographic Advisors Bpm
2012 Tfa Infographic Advisors Bpm2012 Tfa Infographic Advisors Bpm
2012 Tfa Infographic Advisors Bpm
 
Landscape architecture portfolio barbara pyda
Landscape architecture portfolio   barbara pydaLandscape architecture portfolio   barbara pyda
Landscape architecture portfolio barbara pyda
 
Fallsem2015 16 cp4194-13-oct-2015_rm01_graphs
Fallsem2015 16 cp4194-13-oct-2015_rm01_graphsFallsem2015 16 cp4194-13-oct-2015_rm01_graphs
Fallsem2015 16 cp4194-13-oct-2015_rm01_graphs
 

Similar to Chapter2

Data structures KTU chapter2.PPT
Data structures KTU chapter2.PPTData structures KTU chapter2.PPT
Data structures KTU chapter2.PPTAlbin562191
 
Arrays and structures
Arrays and structuresArrays and structures
Arrays and structuresMohd Arif
 
Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysMaulen Bale
 
Chap 2 Arrays and Structures.ppt
Chap 2  Arrays and Structures.pptChap 2  Arrays and Structures.ppt
Chap 2 Arrays and Structures.pptshashankbhadouria4
 
Chap 2 Arrays and Structures.pptx
Chap 2  Arrays and Structures.pptxChap 2  Arrays and Structures.pptx
Chap 2 Arrays and Structures.pptxshashankbhadouria4
 
A quick introduction to R
A quick introduction to RA quick introduction to R
A quick introduction to RAngshuman Saha
 
Using-Python-Libraries.9485146.powerpoint.pptx
Using-Python-Libraries.9485146.powerpoint.pptxUsing-Python-Libraries.9485146.powerpoint.pptx
Using-Python-Libraries.9485146.powerpoint.pptxUadAccount
 
B61301007 matlab documentation
B61301007 matlab documentationB61301007 matlab documentation
B61301007 matlab documentationManchireddy Reddy
 
MO 2020 DS Applications of Linked List 1 AB.ppt
MO 2020 DS Applications of Linked List 1 AB.pptMO 2020 DS Applications of Linked List 1 AB.ppt
MO 2020 DS Applications of Linked List 1 AB.pptshashankbhadouria4
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updatedvrgokila
 

Similar to Chapter2 (20)

Data structures KTU chapter2.PPT
Data structures KTU chapter2.PPTData structures KTU chapter2.PPT
Data structures KTU chapter2.PPT
 
Arrays and structures
Arrays and structuresArrays and structures
Arrays and structures
 
Array
ArrayArray
Array
 
Arrays
ArraysArrays
Arrays
 
Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson Arrays
 
Chap 2 Arrays and Structures.ppt
Chap 2  Arrays and Structures.pptChap 2  Arrays and Structures.ppt
Chap 2 Arrays and Structures.ppt
 
Chap 2 Arrays and Structures.pptx
Chap 2  Arrays and Structures.pptxChap 2  Arrays and Structures.pptx
Chap 2 Arrays and Structures.pptx
 
A quick introduction to R
A quick introduction to RA quick introduction to R
A quick introduction to R
 
Arrays
ArraysArrays
Arrays
 
Using-Python-Libraries.9485146.powerpoint.pptx
Using-Python-Libraries.9485146.powerpoint.pptxUsing-Python-Libraries.9485146.powerpoint.pptx
Using-Python-Libraries.9485146.powerpoint.pptx
 
B61301007 matlab documentation
B61301007 matlab documentationB61301007 matlab documentation
B61301007 matlab documentation
 
MO 2020 DS Applications of Linked List 1 AB.ppt
MO 2020 DS Applications of Linked List 1 AB.pptMO 2020 DS Applications of Linked List 1 AB.ppt
MO 2020 DS Applications of Linked List 1 AB.ppt
 
Fp201 unit4
Fp201 unit4Fp201 unit4
Fp201 unit4
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
 
Op ps
Op psOp ps
Op ps
 
6_Array.pptx
6_Array.pptx6_Array.pptx
6_Array.pptx
 
Spark workshop
Spark workshopSpark workshop
Spark workshop
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 
C# p9
C# p9C# p9
C# p9
 

Recently uploaded

dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptSonatrach
 
Call Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceCall Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceSapana Sha
 
B2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxB2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxStephen266013
 
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptxthyngster
 
办理(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一
办理(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一办理(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一
办理(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一F La
 
20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdfHuman37
 
Predicting Salary Using Data Science: A Comprehensive Analysis.pdf
Predicting Salary Using Data Science: A Comprehensive Analysis.pdfPredicting Salary Using Data Science: A Comprehensive Analysis.pdf
Predicting Salary Using Data Science: A Comprehensive Analysis.pdfBoston Institute of Analytics
 
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAmazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAbdelrhman abooda
 
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degreeyuu sss
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Jack DiGiovanna
 
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)jennyeacort
 
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样vhwb25kk
 
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...limedy534
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsappssapnasaifi408
 
科罗拉多大学波尔得分校毕业证学位证成绩单-可办理
科罗拉多大学波尔得分校毕业证学位证成绩单-可办理科罗拉多大学波尔得分校毕业证学位证成绩单-可办理
科罗拉多大学波尔得分校毕业证学位证成绩单-可办理e4aez8ss
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfLars Albertsson
 
RABBIT: A CLI tool for identifying bots based on their GitHub events.
RABBIT: A CLI tool for identifying bots based on their GitHub events.RABBIT: A CLI tool for identifying bots based on their GitHub events.
RABBIT: A CLI tool for identifying bots based on their GitHub events.natarajan8993
 
INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDINTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDRafezzaman
 
DBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfDBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfJohn Sterrett
 

Recently uploaded (20)

dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
 
Call Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceCall Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts Service
 
B2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxB2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docx
 
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
 
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
 
办理(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一
办理(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一办理(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一
办理(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一
 
20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf
 
Predicting Salary Using Data Science: A Comprehensive Analysis.pdf
Predicting Salary Using Data Science: A Comprehensive Analysis.pdfPredicting Salary Using Data Science: A Comprehensive Analysis.pdf
Predicting Salary Using Data Science: A Comprehensive Analysis.pdf
 
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAmazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
 
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
 
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
 
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
 
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
 
科罗拉多大学波尔得分校毕业证学位证成绩单-可办理
科罗拉多大学波尔得分校毕业证学位证成绩单-可办理科罗拉多大学波尔得分校毕业证学位证成绩单-可办理
科罗拉多大学波尔得分校毕业证学位证成绩单-可办理
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdf
 
RABBIT: A CLI tool for identifying bots based on their GitHub events.
RABBIT: A CLI tool for identifying bots based on their GitHub events.RABBIT: A CLI tool for identifying bots based on their GitHub events.
RABBIT: A CLI tool for identifying bots based on their GitHub events.
 
INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDINTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
 
DBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfDBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdf
 

Chapter2

  • 1. CHAPTER 2 1 CHAPTER 2 ARRAYS AND STRUCTURES All the programs in this file are selected from Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed “Fundamentals of Data Structures in C”, Computer Science Press, 1992.
  • 2. CHAPTER 2 2 Arrays Array: a set of index and value data structure For each index, there is a value associated with that index. representation (possible) implemented by using consecutive memory.
  • 3. CHAPTER 2 3 objects: A set of pairs <index, value> where for each value of index there is a value from the set item. Index is a finite ordered set of one or more dimensions, for example, {0, … , n-1} for one dimension, {(0,0),(0,1),(0,2),(1,0),(1,1),(1,2),(2,0),(2,1),(2,2)} for two dimensions, etc. Functions: for all A ∈ Array, i ∈ index, x ∈ item, j, size ∈ integer Array Create(j, list) ::= return an array of j dimensions where list is a j-tuple whose ith element is the size of the ith dimension. Items are undefined. Item Retrieve(A, i) ::= if (i ∈ index) return the item associated with index value i in array A else return error Array Store(A, i, x) ::= if (i in index) return an array that is identical to array A except the new pair <i, x> has been inserted else return error end array
  • 4. CHAPTER 2 4 Arrays in C int list[5], *plist[5]; list[5]: five integers list[0], list[1], list[2], list[3], list[4] *plist[5]: five pointers to integers plist[0], plist[1], plist[2], plist[3], plist[4] implementation of 1-D array list[0] base address = α list[1] α + sizeof(int) list[2] α + 2*sizeof(int) list[3] α + 3*sizeof(int) list[4] α + 4*size(int)
  • 5. CHAPTER 2 5 Arrays in C (Continued) Compare int *list1 and int list2[5] in C. Same: list1 and list2 are pointers. Difference: list2 reserves five locations. Notations: list2 - a pointer to list2[0] (list2 + i) - a pointer to list2[i] (&list2[i]) *(list2 + i) - list2[i]
  • 6. CHAPTER 2 6 Example: 1-dimension array addressing int one[] = {0, 1, 2, 3, 4}; Goal: print out address and value void print1(int *ptr, int rows) { /* print out a one-dimensional array using a pointer */ int i; printf(“Address Contentsn”); for (i=0; i < rows; i++) printf(“%8u%5dn”, ptr+i, *(ptr+i)); printf(“n”); }
  • 7. CHAPTER 2 7 Address Contents 1228 0 1230 1 1232 2 1234 3 1236 4 *Figure 2.1: One- dimensional array addressing (p.53) call print1(&one[0], 5)
  • 8. CHAPTER 2 8 Structures (records) struct { char name[10]; int age; float salary; } person; strcpy(person.name, “james”); person.age=10; person.salary=35000;
  • 9. CHAPTER 2 9 Create structure data type typedef struct human_being { char name[10]; int age; float salary; }; or typedef struct { char name[10]; int age; float salary } human_being; human_being person1, person2;
  • 10. CHAPTER 2 10 Unions Similar to struct, but only one field is active. Example: Add fields for male and female. typedef struct sex_type { enum tag_field {female, male} sex; union { int children; int beard; } u; }; typedef struct human_being { char name[10]; int age; float salary; date dob; sex_type sex_info; } human_being person1, person2; person1.sex_info.sex=male; person1.sex_info.u.beard=FALSE;
  • 11. CHAPTER 2 11 Self-Referential Structures One or more of its components is a pointer to itself. typedef struct list { char data; list *link; } list item1, item2, item3; item1.data=‘a’; item2.data=‘b’; item3.data=‘c’; item1.link=item2.link=item3.link=NULL; Construct a list with three nodes item1.link=&item2; item2.link=&item3; malloc: obtain a node a b c
  • 12. CHAPTER 2 12 Ordered List Examples (MONDAY, TUEDSAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAYY, SUNDAY) (2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace) (1941, 1942, 1943, 1944, 1945) (a1, a2, a3, …, an-1, an) ordered (linear) list: (item1, item2, item3, …, itemn)
  • 13. CHAPTER 2 13 Operations on Ordered List Find the length, n , of the list. Read the items from left to right (or right to left). Retrieve the i’th element. Store a new value into the i’th position. Insert a new element at the position i , causing elements numbered i, i+1, …, n to become numbered i+1, i+2, …, n+1 Delete the element at position i , causing elements numbered i+1, …, n to become numbered i, i+1, …, n-1 array (sequential mapping)? (1)~(4) O (5)~(6) X
  • 14. CHAPTER 2 14 Structure Polynomial is objects: ; a set of ordered pairs of <ei,ai> where ai in Coefficients and ei in Exponents, ei are integers >= 0 functions: for all poly, poly1, poly2  Polynomial, coef Coefficients, expon Exponents Polynomial Zero( ) ::= return the polynomial, p(x) = 0 Boolean IsZero(poly) ::= if (poly) return FALSE else return TRUE Coefficient Coef(poly, expon) ::= if (expon  poly) return its coefficient else return Zero Exponent Lead_Exp(poly) ::= return the largest exponent in poly Polynomial Attach(poly,coef, expon) ::= if (expon  poly) return error else return the polynomial poly with the term <coef, expon> inserted ne n e xaxaxp ++= ...)( 1 1 Polynomials A(X)=3X20 +2X5 +4, B(X)=X4 +10X3 +3X2 +1
  • 15. CHAPTER 2 15 Polynomial Remove(poly, expon) ::= if (expon  poly) return the polynomial poly with the term whose exponent is expon deleted else return error Polynomial SingleMult(poly, coef, expon) ::= return the polynomial poly • coef • xexpon Polynomial Add(poly1, poly2) ::= return the polynomial poly1 +poly2 Polynomial Mult(poly1, poly2) ::= return the polynomial poly1 • poly2 *Structure 2.2:Abstract data type Polynomial (p.61) End Polynomial
  • 16. CHAPTER 2 16 /* d =a + b, where a, b, and d are polynomials */ d = Zero( ) while (! IsZero(a) && ! IsZero(b)) do { switch COMPARE (Lead_Exp(a), Lead_Exp(b)) { case -1: d = Attach(d, Coef (b, Lead_Exp(b)), Lead_Exp(b)); b = Remove(b, Lead_Exp(b)); break; case 0: sum = Coef (a, Lead_Exp (a)) + Coef ( b, Lead_Exp(b)); if (sum) { Attach (d, sum, Lead_Exp(a)); a = Remove(a , Lead_Exp(a)); b = Remove(b , Lead_Exp(b)); } break; Polynomial Addition #define MAX_DEGREE 101 typedef struct { int degree; float coef[MAX_DEGREE]; } polynomial; data structure 1:
  • 17. CHAPTER 2 17 case 1: d = Attach(d, Coef (a, Lead_Exp(a)), Lead_Exp(a)); a = Remove(a, Lead_Exp(a)); } } insert any remaining terms of a or b into d *Program 2.4 :Initial version of padd function(p.62) advantage: easy implementation disadvantage: waste space when sparse
  • 18. CHAPTER 2 18 Data structure 2: use one global array to store all polynomials A(X)=2X1000 +1 B(X)=X4 +10X3 +3X2 +1 2 1 1 10 3 1 1000 0 4 3 2 0 coef exp starta finisha startb finishb avail 0 1 2 3 4 5 6 *Figure 2.2: Array representation of two polynomials (p.63) specification representation poly <start, finish> A <0,1> B <2,5>
  • 19. CHAPTER 2 19 MAX_TERMS 100 /* size of terms array */ typedef struct { float coef; int expon; } polynomial; polynomial terms[MAX_TERMS]; int avail = 0; *(p.62) storage requirements: start, finish, 2*(finish-start+1) nonparse: twice as much as (1) when all the items are nonzero
  • 20. CHAPTER 2 20 void padd (int starta, int finisha, int startb, int finishb, int * startd, int *finishd) { /* add A(x) and B(x) to obtain D(x) */ float coefficient; *startd = avail; while (starta <= finisha && startb <= finishb) switch (COMPARE(terms[starta].expon, terms[startb].expon)) { case -1: /* a expon < b expon */ attach(terms[startb].coef, terms[startb].expon); startb++ break; Add two polynomials: D = A + B
  • 21. CHAPTER 2 21 case 0: /* equal exponents */ coefficient = terms[starta].coef + terms[startb].coef; if (coefficient) attach (coefficient, terms[starta].expon); starta++; startb++; break; case 1: /* a expon > b expon */ attach(terms[starta].coef, terms[starta].expon); starta++; }
  • 22. CHAPTER 2 22 /* add in remaining terms of A(x) */ for( ; starta <= finisha; starta++) attach(terms[starta].coef, terms[starta].expon); /* add in remaining terms of B(x) */ for( ; startb <= finishb; startb++) attach(terms[startb].coef, terms[startb].expon); *finishd =avail -1; } *Program 2.5: Function to add two polynomial (p.64) Analysis: O(n+m) where n (m) is the number of nonzeros in A(B).
  • 23. CHAPTER 2 23 void attach(float coefficient, int exponent) { /* add a new term to the polynomial */ if (avail >= MAX_TERMS) { fprintf(stderr, “Too many terms in the polynomialn”); exit(1); } terms[avail].coef = coefficient; terms[avail++].expon = exponent; } *Program 2.6:Function to add anew term (p.65) Problem: Compaction is required when polynomials that are no longer needed. (data movement takes time.)
  • 24. CHAPTER 2 24                     − − 0002800 0000091 000000 006000 0003110 150220015 col1 col2 col3 col4 col5 col6 row0 row1 row2 row3 row4 row5 (a) (b) *Figure 2.3:Two matrices 8/36 6*65*3 15/15 Sparse Matrix sparse matrix data structure?
  • 25. CHAPTER 2 25 Structure Sparse_Matrix is objects: a set of triples, <row, column, value>, where row and column are integers and form a unique combination, and value comes from the set item. functions: for all a, b ∈ Sparse_Matrix, x  item, i, j, max_col, max_row  index Sparse_Marix Create(max_row, max_col) ::= return a Sparse_matrix that can hold up to max_items = max _row  max_col and whose maximum row size is max_row and whose maximum column size is max_col. SPARSE MATRIX ABSTRACT DATA TYPE
  • 26. CHAPTER 2 26 Sparse_Matrix Transpose(a) ::= return the matrix produced by interchanging the row and column value of every triple. Sparse_Matrix Add(a, b) ::= if the dimensions of a and b are the same return the matrix produced by adding corresponding items, namely those with identical row and column values. else return error Sparse_Matrix Multiply(a, b) ::= if number of columns in a equals number of rows in b return the matrix d produced by multiplying a by b according to the formula: d [i] [j] = (a[i][k]•b[k][j]) where d (i, j) is the (i,j)th element else return error. * Structure 2.3: Abstract data type Sparse-Matrix (p.68)
  • 27. CHAPTER 2 27 row col value row col value a[0] 6 6 8 b[0] 6 6 8 [1] 0 0 15 [1] 0 0 15 [2] 0 3 22 [2] 0 4 91 [3] 0 5 -15 [3] 1 1 11 [4] 1 1 11 [4] 2 1 3 [5] 1 2 3 [5] 2 5 28 [6] 2 3 -6 [6] 3 0 22 [7] 4 0 91 [7] 3 2 -6 [8] 5 2 28 [8] 5 0 -15 (a) (b) *Figure 2.4:Sparse matrix and its transpose stored as triples (p.69) (1) Represented by a two-dimensional array. Sparse matrix wastes space. (2) Each element is characterized by <row, col, value>. row, column in ascending order # of rows (columns) # of nonzero terms transpose
  • 28. CHAPTER 2 28 Sparse_matrix Create(max_row, max_col) ::= #define MAX_TERMS 101 /* maximum number of terms +1*/ typedef struct { int col; int row; int value; } term; term a[MAX_TERMS] * (P.69) # of rows (columns) # of nonzero terms
  • 29. CHAPTER 2 29 Transpose a Matrix (1) for each row i take element <i, j, value> and store it in element <j, i, value> of the transpose. difficulty: where to put <j, i, value> (0, 0, 15) ====> (0, 0, 15) (0, 3, 22) ====> (3, 0, 22) (0, 5, -15) ====> (5, 0, -15) (1, 1, 11) ====> (1, 1, 11) Move elements down very often. (2) For all elements in column j, place element <i, j, value> in element <j, i, value>
  • 30. CHAPTER 2 30 void transpose (term a[], term b[]) /* b is set to the transpose of a */ { int n, i, j, currentb; n = a[0].value; /* total number of elements */ b[0].row = a[0].col; /* rows in b = columns in a */ b[0].col = a[0].row; /*columns in b = rows in a */ b[0].value = n; if (n > 0) { /*non zero matrix */ currentb = 1; for (i = 0; i < a[0].col; i++) /* transpose by columns in a */ for( j = 1; j <= n; j++) /* find elements from the current column */ if (a[j].col == i) { /* element is in current column, add it to b */
  • 31. CHAPTER 2 31 b[currentb].row = a[j].col; b[currentb].col = a[j].row; b[currentb].value = a[j].value; currentb++ } } } * Program 2.7: Transpose of a sparse matrix (p.71) elements columns Scan the array “columns” times. The array has “elements” elements. ==> O(columns*elements)
  • 32. CHAPTER 2 32 Discussion: compared with 2-D array representation O(columns*elements) vs. O(columns*rows) elements --> columns * rows when nonsparse O(columns*columns*rows) Problem: Scan the array “columns” times. Solution: Determine the number of elements in each column of the original matrix. ==> Determine the starting positions of each row in the transpose matrix.
  • 33. CHAPTER 2 33 [0] [1] [2] [3] [4] [5] row_terms = 2 1 2 2 0 1 starting_pos = 1 3 4 6 8 8 a[0] 6 6 8 a[1] 0 0 15 a[2] 0 3 22 a[3] 0 5 -15 a[4] 1 1 11 a[5] 1 2 3 a[6] 2 3 -6 a[7] 4 0 91 a[8] 5 2 28
  • 34. CHAPTER 2 34 void fast_transpose(term a[ ], term b[ ]) { /* the transpose of a is placed in b */ int row_terms[MAX_COL], starting_pos[MAX_COL]; int i, j, num_cols = a[0].col, num_terms = a[0].value; b[0].row = num_cols; b[0].col = a[0].row; b[0].value = num_terms; if (num_terms > 0){ /*nonzero matrix*/ for (i = 0; i < num_cols; i++) row_terms[i] = 0; for (i = 1; i <= num_terms; i++) row_term [a[i].col]++ starting_pos[0] = 1; for (i =1; i < num_cols; i++) starting_pos[i]=starting_pos[i-1] +row_terms [i-1]; columns elements columns
  • 35. CHAPTER 2 35 for (i=1; i <= num_terms, i++) { j = starting_pos[a[i].col]++; b[j].row = a[i].col; b[j].col = a[i].row; b[j].value = a[i].value; } } } *Program 2.8:Fast transpose of a sparse matrix elements Compared with 2-D array representation O(columns+elements) vs. O(columns*rows) elements --> columns * rows O(columns+elements) --> O(columns*rows) Cost: Additional row_terms and starting_pos arrays are required. Let the two arrays row_terms and starting_pos be shared.
  • 36. CHAPTER 2 36           =                     111 111 111 000 000 111 001 001 001 *Figure 2.5:Multiplication of two sparse matrices (p.73) Sparse Matrix Multiplication Definition: [D]m*p=[A]m*n* [B]n*p Procedure: Fix a row of A and find all elements in column j of B for j=0, 1, …, p-1. Alternative 1. Scan all of B to find all elements in j. Alternative 2. Compute the transpose of B. (Put all column elements consecutively)
  • 37. CHAPTER 2 37 A = 1 0 2 B = 3 0 2 -1 4 6 -1 0 0 0 0 5 2 3 5 3 3 4 0 0 1 0 0 3 0 2 2 0 2 2 1 0 -1 1 0 -1 1 1 4 2 2 5 1 2 6 BT = 3 -1 0 3 3 4 0 0 0 0 0 3 2 0 5 0 1 -1 2 0 2 2 2 5 (0,0) (0,2) (1,0) (1,2) An Example
  • 38. CHAPTER 2 38 General Case dij=ai0*b0j+ai1*b1j+…+ai(n-1)*b(n-1)j a 本來依 i 成群,經轉置後, b 也依 j 成群。 a Sa d Sd b Sb e Se c Sc f Sf g Sg 最多可以產生 ad, ae, af, ag, bd, be, bf, bg, cd, ce, cf, cg 等 entries 。
  • 39. CHAPTER 2 39 void mmult (term a[ ], term b[ ], term d[ ] ) /* multiply two sparse matrices */ { int i, j, column, totalb = b[].value, totald = 0; int rows_a = a[0].row, cols_a = a[0].col, totala = a[0].value; int cols_b = b[0].col, int row_begin = 1, row = a[1].row, sum =0; int new_b[MAX_TERMS][3]; if (cols_a != b[0].row){ fprintf (stderr, “Incompatible matricesn”); exit (1); }
  • 40. CHAPTER 2 40 fast_transpose(b, new_b); /* set boundary condition */ a[totala+1].row = rows_a; new_b[totalb+1].row = cols_b; new_b[totalb+1].col = 0; for (i = 1; i <= totala; ) { column = new_b[1].row; for (j = 1; j <= totalb+1;) { /* mutiply row of a by column of b */ if (a[i].row != row) { storesum(d, &totald, row, column, &sum); i = row_begin; for (; new_b[j].row == column; j++) ; column =new_b[j].row } cols_b + totalb at most rows_a times
  • 41. CHAPTER 2 41 else switch (COMPARE (a[i].col, new_b[j].col)) { case -1: /* go to next term in a */ i++; break; case 0: /* add terms, go to next term in a and b */ sum += (a[i++].value * new_b[j++].value); break; case 1: /* advance to next term in b*/ j++ } } /* end of for j <= totalb+1 */ for (; a[i].row == row; i++) ; row_begin = i; row = a[i].row; } /* end of for i <=totala */ d[0].row = rows_a; d[0].col = cols_b; d[0].value = totald; } *Praogram 2.9: Sparse matrix multiplication (p.75)
  • 42. CHAPTER 2 42 Analyzing the algorithm cols_b * termsrow1 + totalb + cols_b * termsrow2 + totalb + … + cols_b * termsrowp + totalb = cols_b * (termsrow1 + termsrow2 + … + termsrowp) + rows_a * totalb = cols_b * totala + row_a * totalb O(cols_b * totala + rows_a * totalb)
  • 43. CHAPTER 2 43 for (i =0; i < rows_a; i++) for (j=0; j < cols_b; j++) { sum =0; for (k=0; k < cols_a; k++) sum += (a[i][k] *b[k][j]); d[i][j] =sum; } Compared with matrix multiplication using array O(rows_a * cols_a * cols_b) vs. O(cols_b * total_a + rows_a * total_b) optimal case: total_a < rows_a * cols_a total_b < cols_a * cols_b worse case: total_a --> rows_a * cols_a, or total_b --> cols_a * cols_b
  • 44. CHAPTER 2 44 void storesum(term d[ ], int *totald, int row, int column, int *sum) { /* if *sum != 0, then it along with its row and column position is stored as the *totald+1 entry in d */ if (*sum) if (*totald < MAX_TERMS) { d[++*totald].row = row; d[*totald].col = column; d[*totald].value = *sum; } else { fprintf(stderr, ”Numbers of terms in product exceed %dn”, MAX_TERMS); exit(1); } } Program 2.10: storsum function