Presentation is loading. Please wait.

Presentation is loading. Please wait.

7/6/2016 1R. Smith - University of St Thomas - Minnesota CISC 130: Today’s Class Collect PapersCollect Papers Review: Our ZeroesReview: Our Zeroes Review:

Similar presentations


Presentation on theme: "7/6/2016 1R. Smith - University of St Thomas - Minnesota CISC 130: Today’s Class Collect PapersCollect Papers Review: Our ZeroesReview: Our Zeroes Review:"— Presentation transcript:

1 7/6/2016 1R. Smith - University of St Thomas - Minnesota CISC 130: Today’s Class Collect PapersCollect Papers Review: Our ZeroesReview: Our Zeroes Review: Function DeclarationReview: Function Declaration Arrays and Side EffectsArrays and Side Effects –Example: counting digits typed

2 Recap Computer HardwareComputer Hardware Scanning for Words FunctionScanning for Words Function Working on Assignment 10Working on Assignment 10

3 Zeroes We have three zeroes:We have three zeroes:0‘0’ and ‘\0’ Two are ‘characters’ one is a literal numberTwo are ‘characters’ one is a literal number 0 == ‘\0’ && ‘0’ == 48 7/6/2016 3R. Smith - University of St Thomas - Minnesota

4 How to Declare a Function There is a ‘function description’There is a ‘function description’ –Function’s name, return type, arguments Use the description to create the functionUse the description to create the function Function Header: All on one line:Function Header: All on one line: –Function type –Function name –First open parenthesis For each argument:For each argument: –Argument type –Argument name –Array description, if it’s an array –Comma, if there’s another argument 7/6/2016 4R. Smith - University of St Thomas - Minnesota

5 Variables in a Function The ‘arguments’ are ‘fill in the blanks’The ‘arguments’ are ‘fill in the blanks’ –When we say sqrt(4), we want sqrt to calculate the answer –When we say printf(“Hello”), we want printf to print “Hello” We declare the argumentsWe declare the arguments –And they’re filled in when we call the function We declare local variables after the headerWe declare local variables after the header –Just like in main() –Note: main() is a function with ZERO arguments, like getint() 7/6/2016 5R. Smith - University of St Thomas - Minnesota

6 How do we count digits in text? A function with ‘side effects’A function with ‘side effects’ –We can change an array passed as an argument Let’s produce a digit counting functionLet’s produce a digit counting function –Input arguments: Character array (string) to count digits inCharacter array (string) to count digits in Array of integers, one per digit being countedArray of integers, one per digit being counted Length of the array of integersLength of the array of integers –Result type: void since answer is in array of integers The big picture: this is an exampleThe big picture: this is an example –This is NOT part of Assignment 10 –It will help you figure out Assignment 10 –How do they compare? ……. 7/6/2016 6R. Smith - University of St Thomas - Minnesota

7 Counting digits in text What parts are the same? what parts differ?What parts are the same? what parts differ? Both have roughly the same main()Both have roughly the same main() –Local variables: an array to count, maybe some loop indices –Step 1: initialize the array to count –Step 2: loop to read lines of text till a blank line is typed Inside the loop, read the line of textInside the loop, read the line of text Pass the line of text to the counting function (either one)Pass the line of text to the counting function (either one) –Step 3: call function to print out the results Short term: print array valuesShort term: print array values Long term: print a histogramLong term: print a histogram 7/6/2016 7R. Smith - University of St Thomas - Minnesota

8 Counting digits in text: the function Similar but simpler than word length countSimilar but simpler than word length count Big loop to step through chars in the stringBig loop to step through chars in the string –If the char is a digit (and ONLY if) Then increment the corresponding count in the arrayThen increment the corresponding count in the array –Go to the next char Returns nothing; answers are in the arrayReturns nothing; answers are in the array –Subsequent function calls add results from other lines Let’s write itLet’s write it 7/6/2016 8R. Smith - University of St Thomas - Minnesota

9 7/6/2016 9R. Smith - University of St Thomas - Minnesota How do we count word length? When we start a word, it’s 1 char longWhen we start a word, it’s 1 char long When we’re inside a word,When we’re inside a word, –we add another char to the length When we end a word,When we end a word, –we don’t add another char –we save the result Input = White Space Input = other character State = IN Next State = OUT Next State = IN State = OUT Next State = OUT Next State = IN

10 7/6/2016 10R. Smith - University of St Thomas - Minnesota How do we count word length? When we start a word, it’s 1 char longWhen we start a word, it’s 1 char long When we’re inside a word,When we’re inside a word, –we add another char to the length When we end a word,When we end a word, –we don’t add another char –we save the result Input = White Space Input = other character State = IN Next State = OUT Print the count Next State = IN Increment count State = OUT Next State = OUT do nothing Next State = IN count = 1

11 7/6/2016 11R. Smith - University of St Thomas - Minnesota

12 Sample “drawing” function Draw right triangles with nested for loopsDraw right triangles with nested for loops 7/6/2016 12R. Smith - University of St Thomas - Minnesota

13 7/6/2016 13R. Smith - University of St Thomas - Minnesota Creative Commons License This work is licensed under the Creative Commons Attribution-Share Alike 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by- sa/3.0/us/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.


Download ppt "7/6/2016 1R. Smith - University of St Thomas - Minnesota CISC 130: Today’s Class Collect PapersCollect Papers Review: Our ZeroesReview: Our Zeroes Review:"

Similar presentations


Ads by Google