Chapter2 Arrays&Structures 2023-11-5
Chapter2 Arrays&Structures 2023-11-5
CHAPTER 2 1
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 2
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
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
Notations:
list2 - a pointer to list2[0]
(list2 + i) - a pointer to list2[i] (&list2[i])
*(list2 + i) - list2[i]
CHAPTER 2 5
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 Contents\n”);
for (i=0; i < rows; i++)
printf(“%8u%5d\n”, ptr+i, *(ptr+i));
printf(“\n”);
}
CHAPTER 2 6
call print1(&one[0], 5)
Address Contents
1228 0
1230 1
1232 2
1234 3
1236 4
*Figure 2.1: One- dimensional array addressing (p.53)
CHAPTER 2 7
Structures (records)
struct {
char name[10];
int age;
float salary;
} person;
strcpy(person.name, “james”);
person.age=10;
person.salary=35000;
CHAPTER 2 8
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;
End Polynomial
*Structure 2.2:Abstract data type Polynomial (p.61)
CHAPTER 2 13
Polynomial Addition
data structure 1: #define MAX_DEGREE 101
typedef struct {
int degree;
float coef[MAX_DEGREE];
} polynomial;
/* 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!=0) {
Attach (d, sum, Lead_Exp(a));
a = Remove(a , Lead_Exp(a));
b = Remove(b , Lead_Exp(b));
} CHAPTER 2 14
break;
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
CHAPTER 2 15
Data structure 2: use one global array to store all polynomials
A(X)=2X1000+1
B(X)=X4+10X3+3X2+1 *Figure 2.2: Array representation of two polynomials
(p.63)
starta finisha startb finishb avail
coef
2 1 1 10 3 1
exp
1000 0 4 3 2 0
0 1 2 3 4 5 6
specification representation
poly <start, finish>
A CHAPTER 2 <0,1> 16
B <2,5>
storage requirements: start, finish, 2*(finish-start+1)
nonparse: twice as much as (1)
when all the items are nonzero
CHAPTER 2 17
Add two polynomials: D = A + B
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;
CHAPTER 2 18
case 0: /* equal exponents */
coefficient = terms[starta].coef +
terms[startb].coef;
if (coefficient!=0)
attach (coefficient, terms[starta].expon);
starta++;
startb++;
break;
case 1: /* a expon > b expon */
attach(terms[starta].coef, terms[starta].expon);
starta++;
}
CHAPTER 2 19
/* 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;
}
Analysis: O(n+m)
where n (m) is the number of nonzeros in A(B).
*Program 2.5: Function to add two polynomial (p.64)
CHAPTER 2 20
void attach(float coefficient, int exponent)
{
/* add a new term to the polynomial */
if (avail >= MAX_TERMS) {
fprintf(stderr, “Too many terms in the polynomial\n”);
exit(1);
}
terms[avail].coef = coefficient;
terms[avail++].expon = exponent;
}
*Program 2.6:Function to add anew term (p.65)
CHAPTER 2 23
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.
CHAPTER 2 24
* Structure 2.3: Abstract data type Sparse-Matrix (p.68)
(1) Represented by a two-dimensional array.
Sparse matrix wastes space.
(2) Each element is characterized by <row, col, value>.
row col value row col value
# of rows (columns)
# of nonzero terms
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 transpose [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)
row, column in ascending order
*Figure 2.4:Sparse matrix and its transpose stored as triples (p.69)
CHAPTER 2 25
Sparse_matrix Create(max_row, max_col) ::=
* (P.69)
CHAPTER 2 26
Transpose a Matrix
(1) for each row i
take element <i, j, value> and store it
in element <j, i, value> of the transpose.
elements
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)
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 30
Fast Transpose a Matrix:
CHAPTER 2 32
for (i=1; i <= num_terms, i++) {
j = starting_pos[a[i].col]++;
elements 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