INFSCI 0015 Data Structures Lecture 12: Nested Loops - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

INFSCI 0015 Data Structures Lecture 12: Nested Loops

Description:

INFSCI 0015 Data Structures Lecture 12: Nested Loops – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 17
Provided by: peterbru
Category:

less

Transcript and Presenter's Notes

Title: INFSCI 0015 Data Structures Lecture 12: Nested Loops


1
INFSCI 0015 - Data StructuresLecture 12 Nested
Loops
  • Peter Brusilovsky
  • http//www2.sis.pitt.edu/peterb/0015-011/

2
Outline
  • ANSI C Function Prototypes
  • Nested Loops
  • Example 11.1 Star chart
  • Example 11.2 Longest line
  • Example 11.3 Simple pattern search

3
Example 12.1 Counting digits
  • include ltstdio.hgt
  • main ()
  • int c, i, j
  • int ndigs10
  • / initialize /
  • for(i 0 i lt 10 i) ndigsi 0
  • / count /
  • while ((c getchar()) ! EOF)
  • if (c gt '0' c lt '9')
  • ndigsc - '0'

4
Example 12.1 Star Chart
  • / reporting /
  • printf("\n \n")
  • for (i 0 i lt 10 i)
  • printf(" d ", i)
  • for(j1 j lt ndigsi j)
  • printf("")
  • printf("\n")
  • printf(" ------------------gt\n")

nested loop
5
Example 12.2 Longest line
  • / print longest input line /
  • main()
  • int len / current line length /
  • int max / maximum length seen so far /
  • char lineMAXLINE / current input line /
  • char longest MAXLINE /longest line saved
    here /
  • max 0
  • while ((len getline(line, MAXLINE)) gt 0)
  • if (len gt max)
  • max len
  • copy(longest, line)
  • if (max gt 0) / there was a line /
  • printf("s", longest)
  • return 0

6
Example 12.2 Longest line
  • / getline read a line into s, return length /
  • int getline(char s, int lim)
  • int c, i
  • for (i 0 i lt lim-1 (cgetchar())! EOF c
    ! '\n' i)
  • si c
  • if (c '\n')
  • si c
  • i
  • si '\0'
  • return i

7
Example 12.2 Two copyes
  • / copy copy 'from' into 'to' assume to is big
    enough /
  • void copy(char to, char from)
  • int i 0
  • while((toi from i) ! '\0')
  • i
  • void copy(char to, char from)
  • int i
  • for(i0 (toi from i) ! '\0 i)

8
Example 12.3 String Search
i
c
o
u
r
s
e
\0
j
o
u
r
\0
k
9
Example 12.3 String Search
i
c
o
u
r
s
e
\0
j
o
u
r
\0
k
10
Example 12.3 String Search
i
c
o
u
r
s
e
\0
j
o
u
r
\0
k
11
Example 12.3 String Search
i
c
o
u
r
s
e
\0
j
o
u
r
\0
k
12
Example 12.3 String Search
i
c
o
u
r
s
e
\0
j
o
u
r
\0
k
13
Example 12.3 String Search
i
c
o
u
r
s
e
\0
j
o
u
r
\0
k
14
Example 12.3 String Search
  • int getline(char line, int maxline)
  • int strindex(char source, char searchfor)
  • char pattern "ould"
  • / find all lines matching pattern /
  • main()
  • char lineMAXLINE / current input line /
  • int found 0
  • while (getline(line, MAXLINE) gt 0)
  • if (strindex(line, pattern) gt 0)
  • printf("s", line)
  • found
  • return found

15
Example 12.3 String Search
  • / strindex return index of t in s, -1 if none
    /
  • int strindex(char s, char t)
  • int i, j, k
  • for(i 0 si ! '\0' i)
  • for(j i, k 0 tk!'\0' sjtk
  • j, k)
  • if (k gt 0 tk '\0')
  • return i
  • return -1

16
Exercise
  • Modify example 12.3 using rindex function instead
    of index. Unlike strindex that returns index of
    the first pattern found in the target string from
    the left, rindex should return index of the first
    pattern found from the right
  • strindex abababbabbbabbabb
  • rindex abababbabbbabbabb
Write a Comment
User Comments (0)
About PowerShow.com