partially filled arrays · string terms[6] = {“loop”, “function”, “array”,...

6
1 13 Partially Filled Arrays 1 Partially Filled Arrays 1E3 Topic 13 13 Partially Filled Arrays 2 Objectives This topic should allow students to use arrays to handle problems whose size is unknown in advance. This topic is not covered in the textbook.

Upload: others

Post on 15-Jul-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Partially Filled Arrays · string terms[6] = {“loop”, “function”, “array”, “variable”, “program”, “double”}!! We’ll count each one in turn, since we have

1

13 Partially Filled Arrays 1

Partially Filled Arrays!

1E3 !Topic 13!

13 Partially Filled Arrays 2

Objectives!

n  This topic should allow students to use arrays to handle problems whose size is unknown in advance.!

n  This topic is not covered in the textbook.!

Page 2: Partially Filled Arrays · string terms[6] = {“loop”, “function”, “array”, “variable”, “program”, “double”}!! We’ll count each one in turn, since we have

2

13 Partially Filled Arrays 3

Problem!

n  Up till now we’ve used arrays to deal with problems of known size!n  5 grades!n  Reverse 10 words!n  12 months of rainfall!

n  Often we want to use arrays but we don’t know in advance how many students, employees, words,… there will be.!

13 Partially Filled Arrays 4

Solution!

n  Declare an array big enough to handle the maximum possible problem size.!n  E.g. DECLARED_SIZE!

n  Keep track of how much of the array you fill up.!n  E.g. call it array_size, or num_grades!

n  Use the latter number in for loops that process the array.!

Page 3: Partially Filled Arrays · string terms[6] = {“loop”, “function”, “array”, “variable”, “program”, “double”}!! We’ll count each one in turn, since we have

3

13 Partially Filled Arrays 5

Example!n  Write a program to square a sequence of

numbers, terminated by a 0.!n  Use a function readtosentinel to read in the

sequence.!n  readtosentinel takes in the array, the declared size

of the array and a parameter count.!n  readtosentinel updates the count variable so it

must be a call-by-reference parameter to readtosentinel!

n Use count to control subsequent loops.!

n  See squarearbseq.cpp!

13 Partially Filled Arrays 6

readtosentinel function!

Page 4: Partially Filled Arrays · string terms[6] = {“loop”, “function”, “array”, “variable”, “program”, “double”}!! We’ll count each one in turn, since we have

4

13 Partially Filled Arrays 7

readtosentinel!n  Called like"readtosentinel (numbers, MAX_SIZE, count, 0); n  The 0 is the sentinel value – our function will handle

any sentinel of type double.!n  readtosentinel must watch for too many values

being entered.!n  Note the pattern!

n  read x; while (x ok) { process x;… read next x; …}

n  We’ve seen this pattern many times before.!

13 Partially Filled Arrays 8

Summary!

n  To handle arrays of unknown size!n  Declare a large enough array for all cases!n  Use a function like readtosentinel to read in

values and set the actual number of values read in.!n You would need a variant to read strings or ints!

n  Don’t read too many values in!!n  Use the actual size of the array in subsequent

loops / array functions.!

Page 5: Partially Filled Arrays · string terms[6] = {“loop”, “function”, “array”, “variable”, “program”, “double”}!! We’ll count each one in turn, since we have

5

Exercise: indexing text!n  Read text, terminated by “END”, into an

array of words.!n  Assume no punctuation, just words.!

n  Then count the occurrences of specified terms in the text.!n  string terms[6] = {“loop”, “function”, “array”,

“variable”, “program”, “double”}!n  We’ll count each one in turn, since we have

permanent access to all the text.!n  Identify the most common of those terms.!

13 Partially Filled Arrays 9

13 Partially Filled Arrays 10

Advanced indexing exercise!n  Extend the previous idea!n  Instead of having a specified list of terms

to count, extract every unique word from the input and count all of them.!

n  Write a function to pass through the text array and add newly found words to the terms array.!n  This function will generate an array of terms

whose size will be set by the function.!n  Then the previous program will work

with that terms array and it’s size.!

Page 6: Partially Filled Arrays · string terms[6] = {“loop”, “function”, “array”, “variable”, “program”, “double”}!! We’ll count each one in turn, since we have

6

13 Partially Filled Arrays 11

Another exercise!n  Write a function between to extract from an

array of numbers, those which are between two values. The extracted values should be put into an array.!

n  void between(double n[], int asize, double b[], int& bsize);

n  Embed your function in a main program that tests it. !