Arrays%20of%20Arrays: - PowerPoint PPT Presentation

About This Presentation
Title:

Arrays%20of%20Arrays:

Description:

{ m','o','n','d','a','y',''}, { t','u','e','s','d','a','y','' ... big sky country. what is printed. Arrays and Pointers. 21. Self-Test: ... – PowerPoint PPT presentation

Number of Views:65
Avg rating:3.0/5.0
Slides: 27
Provided by: robert1776
Category:
Tags: 20arrays | 20of | arrays | big | tires

less

Transcript and Presenter's Notes

Title: Arrays%20of%20Arrays:


1
Arrays of Arrays
  • We can have arrays of any type, even arrays whose
    elements are themselves arrays.
  • With two bracket pairs, we obtain a
    two-dimensional array. To obtain arrays of higher
    dimension, we simply continue to add brackets.
  • int a100 // an one-dimensional array
  • int b27 // a two-dimensional array
  • int c543 // a three-dimensional array

2
Size of a Multi-dimensional Array
  • A k-dimensional array has a size for each of its
    k dimensions. Let si represent the size of the
    arrays ith dimension, such as
  • int as1s2sk
  • then the declaration of the array a allocates
    space for s1 ? s2 ? ? sk elements.

3
Example
  • int a35

Col 1 Col 2 Col 3 Col 4 Col 5
Row 1 a00 a01 a02 a03 a04
Row 2 a10 a11 a12 a13 a14
Row 3 a20 a21 a22 a23 a24
4
  • define ROWS 3
  • define COLS 4
  • int main()
  • int aROWSCOLS, i, j, sum 0
  • for (i 0 i lt ROWS i) / fill the
    array /
  • for (j 0 j lt COLS j)
  • aij i j
  • for (i 0 i lt ROWS i) / print the
    array /
  • for (j 0 j lt COLS j)
  • printf(add d , i, j,
    aij)
  • printf(\n)
  • for (i 0 i lt ROWS i) / sum the array
    /
  • for (j 0 j lt COLS j)
  • sum aij
  • printf(\nsum d\n, sum)

5
Relationship between pointers and arrays
  • int a35

Expressions equivalent to aij
(ai j)
((a i))j
((a i) j)
(a00 5i j)
6
Storage Mapping Function
  • For any array, the mapping between pointer
    values and array indices is called the storage
    mapping function. For example
  • int aROWSCOLS
  • aij ? (a00 iCOLS j)
  • Note the array name a by itself is equivalent
    to a0, a pointer to an array of COLS ints. The
    base address of the array is a00, not a.

7
Multi-dimensional Arrays as Formal Parameters
  • In a function definition, a multi-dimensional
    array argument must be specified by all sizes
    except the first dimension.
  • int sum(int a5)
  • int i, j, sum 0
  • for (i 0 i lt ROWS i)
  • for (j 0 j lt 5 j)
  • sum aij
  • return sum
  • Note the following parameter declarations are
    equivalent
  • int a5 int (a)5 int a35

8
Initialization
  • The following three initializations are
    equivalent.
  • int a23 1, 2, 3, 4, 5, 6
  • int a23 1, 2, 3, 4, 5, 6
  • int a3 1, 2, 3, 4, 5, 6
  • If there are no inner braces, then each array
    elements is initialized in turn (row-wise).
  • If there are fewer values than elements in the
    array, then the remaining elements are
    initialized to zero.
  • If the first bracket pair is empty, then the
    compiler takes the size from the number of inner
    brace pairs.
  • All sizes except the first must be given
    explicitly.

9
Another Example
  • int a223
  • 1, 1, 0, 2, 0, 0,
  • 3, 0, 0, 4, 4, 0
  • / or equivalently /
  • int a23
  • 1, 1, 2,
  • 3, 4, 4

10
Dynamic Allocation
  • Suppose we want to build a student name list.
    The program must read its input from keyboard (by
    calling getline function) and store the input
    into some kind of table. What king of table?
  • If we use an array of pointers, how to allocate
    space for each entry?

11
Arrays of Pointers
  • 2-D arrays contain the same number of elements
    in each row. For example
  • char days10
  • m,o,n,d,a,y,\0,
  • t,u,e,s,d,a,y,\0,
  • Space is wasted in the rows containing shorter
    strings. Can we build an array whose rows can
    vary in length?

12
Ragged Array
  • A ragged array is an array of pointers where
    each entry in the array is a pointer to a string
    (arbitrary length). For example
  • char days monday, tuesday,
    wednesday, thursday, friday, saturday,
    sunday
  • The compiler allocates an array containing 7
    elements and assigns each element a pointer to
    the corresponding string.

13
m o n d a y \0
t u e s d a y \0
w e d n e s d a y \0
t h u r s d a y \0
f r i d a y \0
s a t u r d a y \0
s u n d a y \0
2-D Array
Array of pointers







m o n d a y \0
t u e s d a y \0

s u n d a y \0
14
Accessing Elements
  • Elements in a ragged array can be access in
    either pointer-based access or array-based
    access. For example,
  • char days monday, tuesday,
    wednesday, thursday, friday, saturday,
    sunday
  • Suppose we want to access the third char of
    wednesday, the following are equivalent
  • days22
  • (days2 2)
  • ((days 2) 2)
  • days is the base-address of the array of
    pointers
  • daysk is the pointer to the kth string

15
Self-Test
  • char days monday, tuesday,
    wednesday, thursday, friday, saturday,
    sunday

Expression
(days2 4)
days35
days4
(days5)
days66
days
Value
e
d
friday
s
\0
m
16
Traversing an Array of pointers
  • include ltstdio.hgt
  • / array-based (index) /
  • void printStrs1(char s, int n)
  • int i
  • for (i 0 i lt n i)
  • printf(s\n, si)
  • / pointer-based /
  • void printStrs2(char sp, int n)
  • char ep sp n
  • for ( sp lt ep sp)
  • printf(s\n, sp)

17
  • include ltstdio.hgt
  • include ltstring.hgt
  • include ltstddef.hgt
  • include ltstdlib.hgt
  • define MAXLEN 80
  • define MAXNAMES 100
  • static char newStr(char str) // a static
    function will be handled as an inline function
  • char newstr malloc(strlen(str) 1)
  • if (newstr ! NULL) strcpy(newstr, str)
  • return newstr
  • int main()
  • int k
  • char nameTableMAXNAMES, nameMAXLEN 1,
    sp
  • for (k 0 getline(name, MAXLEN) ! -1 k lt
    MAXAMES k)
  • if ((sp newStr(name)) ! NULL)
  • nameTablek sp
  • else
  • printf(Memory run out\n)

18
Command-line Arguments
  • When we run a program, we often provide some
    initial values the program will work with. For
    Example
  • myprogram filename 222
  • Two arguments, argc and argv, can be used with
    the main() to communicate with the operating
    system, where argc is the number of arguments on
    the command-line, and argv is an array of
    pointers to strings containing the various
    arguments.
  • For the above example
  • argc 3
  • argv0 myprogram
  • argv1 filename
  • argv2 222

19
Passing Arguments to main()
  • include ltstdio.hgt
  • int main(int argc, char argv)
  • int k
  • printf(argc d\n, argc)
  • for (k 0 k lt argc k)
  • printf(argvd s\n, k, argvk)
  • mytest o try this
  • argc 4
  • argv0 mytest
  • argv1 -o
  • argv2 try
  • argv3 this

20
Self-Test
  • char s1 beautiful big sky country,
  • s2 how now brown cow

Expression
strlen(s1)
strlen(s2 8)
strcmp(s1, s2)
Value
25
9
negative int
Statement
printf(s, s1 10)
strcpy(s1 10, s2 8)
strcat(s1, s!)
printf(s, s1)
what is printed
big sky country


beautiful brown cows!
21
Self-Test
  • char p23 abc, defg, hi,
  • jklmno, pqrstuvw, xyz

Expression Equivalent expression
p p000
p1
(p1 2)
((p 1) 1)7
(((p 1) 1))7
(p12 2)
Value
a
j
x
error
w
z
22
A Union and Structure Example
  • typedef enum AUTO, BOAT, PLANE, SHIP VTYPE
  • typedef struct / structure for an automobile
    /
  • int tires
  • int fenders
  • int doors
  • AUTOTYPE
  • typedef struct / structure for a boat or ship
    /
  • int displacement
  • char length
  • BOATTYPE

23
  • typedef struct
  • char engines
  • int wingspan
  • PLANETYPE
  • typedef struct
  • VTYPE vehicle / what type of vehicle? /
  • int weight / gross weight of vehicle /
  • union / type-dependent data /
  • AUTOTYPE car / part 1 of the union /
  • BOATTYPE boat / part 2 of the union /
  • PLANETYPE airplane / part 3 of the union
    /
  • BOATTYPE ship / part 4 of the union /
  • vehicle_type
  • int value / value of vehicle in dollars /
  • char owner32 / owners name /
  • VEHICLE

24
  • void print_auto(VEHICLE pv)
  • printf(Auto weight d\n, pv-gtweight)
  • printf( of tires d\n, of fenders d\n,
    of doors d\n, pv-gtvehicle_type.car.tires,
  • pv-gtvehicle_type.car.fenders,
  • pv-gtvehicle_type.car.doors)
  • printf(Cost d\n, pv-gtvalue)
  • printf(Owned by s\n, pv-gtowner) / owners
    name /
  • void print_plane(VEHICLE pv)
  • void print_boat(VEHICLE pv)
  • void print_ship(VEHICLE pv)

25
  • void print_vehicle(VEHICLE pv)
  • switch (pv-gtvehicle)
  • case AUTO print_auto(pv)
  • break
  • case PLANE print_plane(pv)
  • break
  • case BOAT print_boat(pv)
  • break
  • case SHIP print_ship(pv)
  • break
  • default printf(Unknown vehicle type\n)

26
  • void main()
  • VEHICLE v2, piper_cub
  • v0.vehicle AUTO
  • v0.weight 2742 / with a full gas tank /
  • v0.vehicle_type.car.tires 5 / including
    the spare / v0. v0.vehicle_type.car.doors
    2
  • / other initialization of v0 /
  • v1.vehicle BOAT
  • v1.value 3742 / trailer not included /
    v1.vehicle_type.boat.length 20
  • / other initialization of v1 /
  • piper_cub (vehicle) malloc(sizeof(VEHICLE))
  • piper_cub-gtvehicle PLANE
  • piper_cub-gtvehicle_type.airplane.wingspan 27
  • / other initialization of piper_cub /
  • print_vehicle(v)
  • print_vehicle(v1)
  • print_vehicle(piper_cub) / piper_cub is a
    pointer to VEHICLE /
Write a Comment
User Comments (0)
About PowerShow.com