INFSCI 0015 Data Structures Lecture 15: 2dimensional Arrays - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

INFSCI 0015 Data Structures Lecture 15: 2dimensional Arrays

Description:

Example: Day of the Month. Arrays of pointers. Example: Text Reversing. Two-Dimensional Arrays ... i, leap /* 1 for leap year, 0 for non-leap */; leap = ((year ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 19
Provided by: peterbru
Category:

less

Transcript and Presenter's Notes

Title: INFSCI 0015 Data Structures Lecture 15: 2dimensional Arrays


1
INFSCI 0015 - Data StructuresLecture 15
2-dimensional Arrays
  • Peter Brusilovsky
  • http//www2.sis.pitt.edu/peterb/0015-011/

2
Outline
  • 2-dimensional Arrays
  • Example Naval Battle
  • Example Day of the Month
  • Arrays of pointers
  • Example Text Reversing

3
Two-Dimensional Arrays
  • Two-dimensional array is simply a table where
    each cell has two addresses - row and column.
  • The way to address an element of a 2-dimensional
    array a is arowcolumn
  • Rows and Columns are numbered from 0

a00
a01
a02
a04
a10
a11
a44
a40
int a55
4
Declaration and Access
  • Declaration with no initialization
  • int battlefield1010
  • Declaration with initialization
  • int battlefield55
  • 1, 0, 1, 0, 0,
  • 0, 0, 0, 0, 1,
  • 0, 0, 1, 0, 0,
  • 0, 0, 0, 0, 0,
  • 1, 0, 0, 0, 1,
  • Access
  • battlefield44 1

5
Example 15.1 Naval Battle (1)
  • include ltstdio.hgt
  • define FSIZE 5
  • define SHIP 1
  • define EMPTY 0
  • define DESTROYED 2
  • static int battlefieldFSIZEFSIZE
  • 1, 0, 1, 0, 0, / row A /
  • 0, 0, 0, 0, 1, / row B /
  • 0, 0, 1, 0, 0, / row C /
  • 0, 0, 0, 0, 0, / row D /
  • 1, 0, 0, 0, 1, / row E /

6
Example 15.1 Naval Battle (2)
  • main()
  • int col
  • char ch_row
  • printf("Enter your move like 5 A\n")
  • printf("Enter 0 X to exit\n")
  • / reads and process hits /
  • do
  • printf("You move ")
  • scanf("d c\n", col, ch_row)
  • while(react(ch_row, col) gt 0)
  • return 0

7
Example 15.1 Naval Battle (3)
  • int react(char r, int col)
  • int row
  • if (col 0)
  • printf("Good Bye!\n") return 0
  • row r - 'A'
  • if(row lt 0 row gt 4)
  • printf("Row c is not possible!\n", r)
  • else if (col lt1 col gt5)
  • printf("Column d is not possible!\n", col)
  • else if (battlefieldrowcol-1 SHIP)
  • printf("Hit!\n")
  • battlefieldrowcol-1 DESTROYED
  • else if (battlefieldrowcol-1
    DESTROYED)
  • printf("This ship has been already
    destroyed!\n")
  • else printf("Misss...\n")
  • return 1

8
Example 15.2 Day of the Year
101 of 2000?
Jan
Feb
Dec
Mar
31
31
31
29
101 - 31 70
31
31
31
29
70 - 29 41
31
31
31
29
41 - 31 10
31
31
31
29
101 April 10, 2000
9
Example 15.2 Day of the Year
  • static char daytab213
  • 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,
    31,
  • 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30,
    31,
  • void month_day(int year, int yearday, int
    pmonth, int pday)
  • int i, leap / 1 for leap year, 0 for non-leap
    /
  • leap ((year4 0) year100 ! 0)
    year400 0
  • for(i 1 yearday gt daytableapi i)
  • yearday - daytableapi
  • pmonth i
  • pday yearday

10
Example 15.2 Day of the Year
  • main()
  • int year, yday / input data /
  • int month, mday / output data /
  • do
  • printf("Enter Year and Day (yyyy dd) ")
  • scanf("d d", year, yday)
  • if(yday gt 0)
  • month_day(year, yday, month, mday)
  • printf("Day d of d is simply d/d/d\n",
  • yday, year, month, mday, year)
  • else
  • printf("Good Bye!\n")
  • while(yday gt 0)
  • return 0

11
Arrays of Pointers
a
b
r
a
\0
c
a
d
a
b
r
\0
c
o
u
r
s
e
\0
strings
c
o
u
r
s
e
s
\0
char strings4 / array of four pointers /
12
Memory Allocation malloc
p1 malloc(6)
112300
112301
112302
112303
112304
112305
112306
p1
112307
112308
112309
112310
p2
112311
112312
112313
112314
112300
13
Memory Allocation malloc
p2 malloc(8)
112300
112301
112302
112303
112304
112305
112306
p1
112307
112308
112309
112310
p2
112311
112312
112313
112314
112300
14
Example 15.3 Reverse Text
  • static char lineptrMAXLINES
  • main()
  • int nlines / counter of input lines read /
  • nlines readlines()
  • printbackwards(nlines)
  • void printbackwards(int n)
  • while(n gt 0)
  • printf("s", lineptrn-1)
  • --n

15
Example 15.3 Reverse Text
  • define MAXLEN 1000
  • int readlines()
  • int len, nlines
  • char p, line MAXLEN
  • nlines 0
  • while((len getline(line, MAXLEN)) gt 0)
  • p (char ) malloc(len1)
  • copy(p, line)
  • lineptrnlines p
  • nlines
  • return nlines

16
Initialization of Pointer Arrays
  • static char strings4
  • "abc", "def", "gh"
  • static char picture3
  • " ",
  • " ",
  • "X"

17
Example 15.4 More Battle
  • static char picture3 " ", " ", "X"
  • void printfield(char header)
  • int i, j
  • / print header /
  • printf("s\n\n ", header)
  • for(j 0 j lt FSIZE j)
  • printf(" d ", j1)
  • printf("\n")
  • / print field /
  • for(i 0 i lt FSIZE i)
  • printf("c ", i 'A')
  • for(j 0 j lt FSIZE j)
  • printf("s ", picture battlefieldij )
  • printf("\n")
  • printf("\n")

18
Two-Dimensional Arrays Again
  • Two-dimensional array is really an array of
    one-dimensional arrays
  • We can address a whole one-dimensional array as
    arow
  • A continuous segment of memory is allocated for a
    2-dimensional array

a0
a00
a01
a02
a04
a10
a11
a1
a44
a40
a4
Write a Comment
User Comments (0)
About PowerShow.com