0% found this document useful (0 votes)
23 views31 pages

Chapter 2. Arrays and Structures

- Variables store values while pointers store addresses of memory locations. - Arrays group similar data types together and allow accessing elements via indices. Pointers can also access array elements. - Structures group different data types together and allow accessing fields via dot or arrow operators. - Multi-dimensional arrays in C use nested arrays. Row-major order maps elements to linear memory sequentially by row.

Uploaded by

Marcus Lee
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
23 views31 pages

Chapter 2. Arrays and Structures

- Variables store values while pointers store addresses of memory locations. - Arrays group similar data types together and allow accessing elements via indices. Pointers can also access array elements. - Structures group different data types together and allow accessing fields via dot or arrow operators. - Multi-dimensional arrays in C use nested arrays. Row-major order maps elements to linear memory sequentially by row.

Uploaded by

Marcus Lee
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 31

CSCI2100E

Arrays & Structures


Variables and Pointers (1)
 A variable is to declare a space
(box) to hold a value.
 int val, a;
 A pointer is to declare an
address of a space.
 int *ptr, *b;
 When using a variable/pointer,
 A variable keeps a value.
 A pointer keeps a value which is
the address.

Arrays and Structures 2-2


Variables and Pointers (2)
 Why do we need variables #include <stdio.h>
int main()
and pointers? { int val, a, x, y;
 We want to access values easily. int *ptr, *b;
val = 5; a = 9;
 We want to have flexibility to
ptr = &val; b = &a;
access values.
 Flexibility? Consider the x = val + a;
y = *ptr + *b;
following.
 x = val + a; return 0;
}
 y = *ptr + *b;
 Are x and y the same?

Arrays and Structures 2-3


Variables and Pointers (3)
 Two symbols: * and &. #include <stdio.h>
int main()
 Given a variable, say a, &a { int val, a, x, y;
means the address of the int *ptr, *b;
val = 5; a = 9;
variable. ptr = &val; b = &a;
 &variable  the address of
that variable x = val + a;
y = *ptr + *b;
 Given a pointer, say b, *b
means the value kept in the return 0;
}
address.
 *address  the content kept in that address.

Arrays and Structures 2-4


Variables and Pointers (4)
 Two symbols: * and &. #include <stdio.h>
 More on flexibility: to access int main()
{ int val, a, x, y;
the same content? int *ptr, *b;
 Example 1: val = 5; a = 9;
ptr = &val; b = &a;
 a = 9;
 x = a; a = a + 1; x = val + a;
y = *ptr + *b;
 What is the value in x?

 Example 2: return 0;
}
 a = 9;
 b = &a; a = a + 1; // What is the effect on *b?
 *b = 15; // What is the value kept in a?
Arrays and Structures 2-5
Array
 An array is used to group the same objects
together.
 Good things: If you know the index (where), you can find
the object (content) in one step.
 Bad things: If you want to insert an object in
a place between two objects in an
array, you have to move the objects
first.
 Array as an ADT
 Array plus operations
associate with it.

Arrays and Structures 2-6


Overview of Arrays in C
int list[8], *ptr[8];

 Let the size of the array be n. The first element and the last
element are indexed by 0 and n-1, respectively.
 Let the base address of an array to be a. The address of the i-th
element is located at a+(i-1) x sizeof(theTypeOfElement).
 Example of Usages:
ptr[0] = &list[0]; ptr[1] = &list[2]; list[3] = list[2];
ptr[0] = list; ptr[1] = list + 2; list[3] = *ptr[1];

Arrays and Structures 2-7


Arrays in C
 Create an array statically (when you write the program, and
when you know how many elements you need to access).
 int list[8];
 list[0] = 10;

 Create an array dynamically (when you run the program, and


when you don’t know how many elements you need to access).
 int *list;
 list = (int*)malloc(8 * sizeof(int));
 list[0] = 10;

 Create and initialize an array (when you write the program).


 int list[] = {10, 20, 30, 40, 50, 60, 70, 80};

 What is the ‘number of elements’?


• int size;
• size = sizeof(list) /sizeof(int);
Arrays and Structures 2-8
Pointers and Arrays
int *iptr; // to declare a pointer to int
char *cptr; // to declare a pointer to char
 A pointer has a type. It can point to an int, or a char, …..
 In general, a pointer can point to a sequence of ints, or chars, ….
 int n = 8;
 iptr = (int*)malloc(n * sizeof(int));
 Malloc (memory allocation) is to allocate a piece of memory. As a
procedure, malloc returns a type (char*).
 (int*) is used to match the type at both side.

Arrays and Structures 2-9


Pointers and Arrays
 Let the base address of an array to be a. The address of the
i-th element of an array is located at a+(i-1)x
sizeof(theTypeOfElement).
 So, a pointer pointing a memory can be considered as an
array.
 iptr[2] = 10;
 About the “+” for address.
 Iptr + 2  move two integers away from the base
address
 Cptr + 2  move two chars away from the base address

Arrays and Structures 2-10


Multi-Dimensional Arrays in C
 C uses the so-called array-of-arrays
representation to represent a
multidimensional array.
 A two-dimensional array
int x[8][5]

Arrays and Structures 2-11


Multi-Dimensional Arrays
 An alternative is to map all elements of a
multi-dimensional array into an order or linear
list.
 Let be a -dimension array where is the size of
the -th dimension, then the number of
elements is .
 For example, int x[8][5]
 Two ways:
 Row-Major-Order
 Column-Major-Order

Arrays and Structures 2-12


Row Major Order vs Column
Major Order

Arrays and Structures 2-13


Multi-Dimensional Arrays
 Let be a -dimension array where is the size of the -
th dimension, then the number of elements is .
 Row-Major-Order:
 Consider a 2-dimension array, with rows and each
row contains elements.
 Let be the address of , the address of is .
 Consider be a 3-dimenion array, Let be the
address of , the address of is .

Arrays and Structures 2-14


A 3-dimension Array Example A[3][3][3]
A[0][0][1]

A[1][0][2]

A[2][1][2]
The address of A[0][0][1] is a+0*3*3+0*3+1=a+1
The address of A[1][0][2] is a+1*3*3+0*3+2=a+11
The address of A[2][1][2] is a+2*3*3+1*3+2=a+23
Arrays and Structures 2-15
Overview of Structures in C
 Grouping data of different/same types.
 Declare as user-defined data types:
typedef struct { typedef struct { typedef struct {
int day; char name[16]; char name[16];
int month; int student_id; int student_id;
int year; float mark; float mark;
} date; char grade; char grade;
date dob; date *dob;
} student; } studentp;
 Some usages: (Note, there are “.” and “->”)
student s1, s2;
studentp p1, p2; Both “.” and “->” are to
date dob; access a data field defined
s1.dob.year = 1979;
strcpy(s1.name, "Mr.Right"); in a data structure.
p1.dob = &dob; “.” is to access a data field
p2.dob = &dob;
p1.dob->year = 1979; in a variable.
“->” is to access a data field
in a pointer.
Arrays and Structures 2-16
Self-Referential Structures
#define NULL 0
typedef struct _list {
int i;
struct _list *link;
} list;

int main()
{
list item1, item2, item3, *itemVar;
item1.i = 10; item2.i = 20; item3.i = 30;
item1.link = &item2; item2.link = &item3;
item3.link = NULL; itemVar = &item1;
while (itemVar != NULL) {
printf("%d\n", itemVar->i);
itemVar = itemVar->link;
}
return 0;
}
Arrays and Structures 2-17
Array as an ADT

 An array is a collection of data of the same type.


 An array is a set of pairs, (index, value), where each index
that is defined has a value associated with it.
 Other than array creation, 2 standard array operations are:
 retrieve a value
 store a value

Arrays and Structures 2-18


The Polynomial ADT
 A polynomial is where is the degree of the polynomial.

 A polynomial can be represented as a set of pairs of where is a


coefficient and is an exponent.
 E.g., is represented by {(3,2), (1,1), (0,3)}

 We need to define its data structure and its operations to


access the polynomial.

Arrays and Structures 2-19


The Polynomial ADT
 Let poly, poly1, poly2 be polynomials and coef be a
coefficient and expon be an exponent. The following
functions are defined.
 polynomial Zero(): create and initialize a polynomial.
 Boolean IsZero(poly): if (poly) return FALSE else return TRUE.
 coefficient Coef(poly, expon): return the coefficient of expon used
in poly if expon is found in poly. Otherwise return 0.
 exponent Lead_Exp(poly): return the largest exponent in poly.
 polynomial Attach(poly, coef, expon): if expon is found in poly then
return error. Otherwise, return the poly with the term (coef, expon)
inserted.
 polynomial Remove(poly, expon): if expon is found in poly return the
polynomial poly with the term whose exponent is the same as expon
deleted.
 polynomial Add(poly1, poly2): return the polynomial poly1+poly2.
 polynomial Mult(poly1, poly2): return the polynomial poly1poly2.
Arrays and Structures 2-20
Data Structures for Polynomial
ADT
 A polynomial is where is the degree of the polynomial.
 We need to have a data structure to maintain all the
information we need to process ANY polynomial.
 What are the data structures we can define?
 We show four different definitions in the following discussion.

Arrays and Structures 2-21


Data Structures for Polynomial
ADT (cont’d)
 A polynomial is
where is the degree of the polynomial.
 Data-Structure-1:
Coefficients are stored in order of decreasing order (static).

#define MAX_DEGREE 101 /* max degree + 1 */


typedef struct {
int degree;
float coef[MAX_DEGREE];
} polynomial;

degree: the degree of the polynomial ().


coef[i]: for (the coefficient of term) where .

Arrays and Structures 2-22


Data Structure 1 Example

i= 0 1 2 3 98 99 100

2 0 0 0 ... 0 0 1

i= 0 1 2 3 96 97 98 99 100

0 0 0 0 ... 1 9 3 0 1

Disadvantage: waste memory space if MAX_DEGREE.

Arrays and Structures 2-23


Data Structures for Polynomial
ADT (cont’d)
 A polynomial is
where is the degree of the polynomial.
 Data-Structure-2: a dynamic approach of Data-Structure-1.

typedef struct {
int degree;
float *coef;
} polynomial;

polynomial p1;
p1.degree = n;
p1.coef = (float*)malloc((p1.degree+1) *sizeof(float));

Arrays and Structures 2-24


Data Structure 2 Example

i= 0 1 2 3 98 99 100

2 0 0 0 ... 0 0 1

i= 0 1 2 3 4

1 9 3 0 1

Arrays and Structures 2-25


Data Structures for Polynomial
ADT (cont’d)
 Data-Structure-3:
typedef struct {
int expon;
float coef;
} polynomialbuffer;

typedef struct {
int start;
int finish;
} polynomial;
 A polynomialbuffer to keep many.

#define MAX_TERM 100


int avail = 0;
polynomialbuffer global[MAX_TERM];
polynomial a, b;
Arrays and Structures 2-26
Data Structure 3 Example

0 1 2 3 4 5 6
global 100 0 4 3 2 0 expon
2 1 1 9 3 1 coef

a.start b.start b.finish


a.finish avail

Arrays and Structures 2-27


Data Structures for Polynomial
ADT (cont’d)
 Data-Structure-4:
typedef struct {
int expon;
float coef;
} term;

typedef struct {
int number; /* the number of terms */
term *terms;
} polynomial;

polynomial p;
p.number = m;
p.terms = (term*)malloc(p.number * sizeof(term));

Arrays and Structures 2-28


Data Structure 4 Example

p.number 2
expon 100 0
p.terms
coef 2 1

p.number 4
expon 4 3 2 0
p.terms
coef 1 9 3 1

Arrays and Structures 2-29


Implementation of the 𝑎=2 𝑥 4+3 𝑥 2+𝑥+2
𝑏=9 𝑥 3+2 𝑥 2+2 𝑥
Polynomial Add Function 𝑑=2 𝑥 4+9𝑥 3+5 𝑥2+3 𝑥+2
#define COMPARE(x,y) (((x) < (y)) ? -1: ((x) == (y))? 0: 1)
/* d = a + b, where a, b, and d are polynomials */
d = Zero();
while (!(IsZero(a) && IsZero(b))) {
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));
break;
case 1: d = Attach(d, Coef(a, Lead_Exp(a)), Lead_Exp(a));
a = Remove(a, Lead_Exp(a));
}}
Arrays and Structures 2-30
Polynomial Add: Example
𝑎=2 𝑥 4+3 𝑥 2+𝑥+2
a.number 4
expon 4 2 1 0
a.terms
coef 2 3 1 2
𝑏=9 𝑥 3+2 𝑥2+2 𝑥
b.number 3
expon 3 2 1
b.terms
coef 9 2 2

𝑑=2𝑥 4+9𝑥 3+5 𝑥2+3 𝑥+2


d.number 5
expon 4 3 2 1 0
d.terms
coef 2 9 5 3 2
Arrays and Structures 2-31

You might also like