Presentation is loading. Please wait.

Presentation is loading. Please wait.

1/31/2016 1R. Smith - University of St Thomas - Minnesota CISC 130 - Today’s Class RecapRecap Problem 11Problem 11 Problem 12Problem 12.

Similar presentations


Presentation on theme: "1/31/2016 1R. Smith - University of St Thomas - Minnesota CISC 130 - Today’s Class RecapRecap Problem 11Problem 11 Problem 12Problem 12."— Presentation transcript:

1 1/31/2016 1R. Smith - University of St Thomas - Minnesota CISC 130 - Today’s Class RecapRecap Problem 11Problem 11 Problem 12Problem 12

2 Recap Splitting with ‘Extract’Splitting with ‘Extract’ Searching for strings in a string arraySearching for strings in a string array Pulling the Pieces TogetherPulling the Pieces Together 1/31/2016 2R. Smith - University of St Thomas - Minnesota

3 A practical look at #11 We read data from a file containing 2 columnsWe read data from a file containing 2 columns We split data from the 2 columnsWe split data from the 2 columns –Each goes into a separate array: numbers and names Index values ‘match’ for the columnIndex values ‘match’ for the column –If names[5] = “Joe” then numbers[5] = his phone number We search for names in ‘names’ arrayWe search for names in ‘names’ array –then we print out the corresponding name and number 1/31/2016 3R. Smith - University of St Thomas - Minnesota

4 Let’s create a sample file Go to the assignment, copy/paste the dataGo to the assignment, copy/paste the data 1/31/2016 4R. Smith - University of St Thomas - Minnesota

5 1/31/2016 5R. Smith - University of St Thomas - Minnesota How the assignment works Call a function to read the number/name strings into 2 separate arraysCall a function to read the number/name strings into 2 separate arrays –One keeps the name strings –One keeps the number strings –Name[i] is the person whose number is in number[i] Do a loop till a blank line is enteredDo a loop till a blank line is entered –Read a line from input –Look it up in the ‘names’ array; retrieve the index –If it’s a valid index, print out the name and number Create a file of numbers/names for next stepCreate a file of numbers/names for next step

6 1/31/2016 6R. Smith - University of St Thomas - Minnesota Writing the extract() function Local variablesLocal variables –We need 2 array indices –We need a variable for the ‘split’ index First, find the split pointFirst, find the split point –The end of the number Next, copy out the name and numberNext, copy out the name and number –Option: copy the number –Option: copy the name

7 Searching We write a loop to search the arrayWe write a loop to search the array –Compare each line with the line typed in We can use a ‘full match’ from the libraryWe can use a ‘full match’ from the library We must write a ‘partial match’We must write a ‘partial match’ 1/31/2016 7R. Smith - University of St Thomas - Minnesota

8 1/31/2016 8R. Smith - University of St Thomas - Minnesota Assignment 12: Scaling Date Due: Next MondayDate Due: Next Monday The problem:The problem: –Taking a set of data and scaling it to fit a particular range –A common lab data problem What we’ll doWhat we’ll do –Read in a file of data to be scaled –Calculate scaled values –Print out the original data w/scaled horizontal histogram

9 1/31/2016 9R. Smith - University of St Thomas - Minnesota Assignment 12: The File Line-oriented file again, like in A11Line-oriented file again, like in A11 Name/Number instead of Number/NameName/Number instead of Number/Name Separated by tab character “\t”Separated by tab character “\t” Read “names” into one arrayRead “names” into one array Read “numbers” into a separate arrayRead “numbers” into a separate array Use a third integer array for scaled valuesUse a third integer array for scaled values

10 1/31/2016 10R. Smith - University of St Thomas - Minnesota How Scaling Works Scan the raw data in the arrayScan the raw data in the array –Find the minimum and maximum values –Minimum maps to 0 in scaled values –Maximum maps to 31 in scaled values Calculate the scaling ratioCalculate the scaling ratio –Range of desired values / range of actual values scale = 31.0 / (max – min)scale = 31.0 / (max – min) –A real number, not an int Apply the ratio and round the resultApply the ratio and round the result –Subtract ‘min’ before scaling –Use “rint()” function from math.h –Save scaled values in the scaled array

11 1/31/2016 11R. Smith - University of St Thomas - Minnesota Example Scaling Unscaled ValuesUnscaled Values2001000253600999823410 Scaled Values 0 31 2 16 31 24 8

12 1/31/2016 12R. Smith - University of St Thomas - Minnesota The Histogram Horizontal FormatHorizontal Format Left side of display: name and unscaled valueLeft side of display: name and unscaled value Right side: histogram of scaled valueRight side: histogram of scaled value

13 1/31/2016 13R. Smith - University of St Thomas - Minnesota What to Start On Create a sample fileCreate a sample file –Read in names and numbers Looking for the TabLooking for the Tab –Can use a ‘string.h’ function See pp. 249-50 in the bookSee pp. 249-50 in the book –strspn() – search against a ‘set’ of chars –strcspn() – opposite of strspn() –strchr() – points to first occurrence of a char Converting Text to IntegerConverting Text to Integer –Use the function in the library

14 1/31/2016 14R. Smith - University of St Thomas - Minnesota Sample Input Moose Mountain986Moose Mountain986 Mystery Mountain650Mystery Mountain650 Eagle Mountain650Eagle Mountain650 Ullr Mountain350Ullr Mountain350 Welch Village360Welch Village360 Spirit Mountain700Spirit Mountain700 Keystone3128Keystone3128 Steamboat Springs3668Steamboat Springs3668 Sunday River2340Sunday River2340

15 1/31/2016 15R. 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 "1/31/2016 1R. Smith - University of St Thomas - Minnesota CISC 130 - Today’s Class RecapRecap Problem 11Problem 11 Problem 12Problem 12."

Similar presentations


Ads by Google