Exploring Data Structures: Arrays, String Manipulation, and Scaling Techniques
150 likes | 281 Vues
This document recaps essential concepts in data manipulation using arrays, focusing on splitting names and numbers from a file and searching through them. The practical assignment involves reading a line-oriented file, extracting information into arrays, and implementing searching functions. It further explains scaling data to a specified range by calculating scaled values based on minimum and maximum inputs. The structured approach enables students to learn about string handling, data organization, and visualizing outputs through histograms.
Exploring Data Structures: Arrays, String Manipulation, and Scaling Techniques
E N D
Presentation Transcript
CISC 130 - Today’s Class • Recap • Problem 11 • Problem 12 R. Smith - University of St Thomas - Minnesota
Recap • Splitting with ‘Extract’ • Searching for strings in a string array • Pulling the Pieces Together R. Smith - University of St Thomas - Minnesota
A practical look at #11 • We read data from a file containing 2 columns • We split data from the 2 columns • Each goes into a separate array: numbers and names • Index values ‘match’ for the column • If names[5] = “Joe” then numbers[5] = his phone number • We search for names in ‘names’ array • then we print out the corresponding name and number R. Smith - University of St Thomas - Minnesota
Let’s create a sample file • Go to the assignment, copy/paste the data R. Smith - University of St Thomas - Minnesota
How the assignment works • Call 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 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 step R. Smith - University of St Thomas - Minnesota
Writing the extract() function • Local variables • We need 2 array indices • We need a variable for the ‘split’ index • First, find the split point • The end of the number • Next, copy out the name and number • Option: copy the number • Option: copy the name R. Smith - University of St Thomas - Minnesota
Searching • We write a loop to search the array • Compare each line with the line typed in • We can use a ‘full match’ from the library • We must write a ‘partial match’ R. Smith - University of St Thomas - Minnesota
Assignment 12: Scaling • Date Due: Next Monday • The problem: • Taking a set of data and scaling it to fit a particular range • A common lab data problem • What we’ll do • Read in a file of data to be scaled • Calculate scaled values • Print out the original data w/scaled horizontal histogram R. Smith - University of St Thomas - Minnesota
Assignment 12: The File • Line-oriented file again, like in A11 • Name/Number instead of Number/Name • Separated by tab character “\t” • Read “names” into one array • Read “numbers” into a separate array • Use a third integer array for scaled values R. Smith - University of St Thomas - Minnesota
How Scaling Works • Scan 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 ratio • Range of desired values / range of actual values • scale = 31.0 / (max – min) • A real number, not an int • Apply the ratio and round the result • Subtract ‘min’ before scaling • Use “rint()” function from math.h • Save scaled values in the scaled array R. Smith - University of St Thomas - Minnesota
Unscaled Values 200 1000 253 600 999 823 410 Scaled Values 0 31 2 16 31 24 8 Example Scaling R. Smith - University of St Thomas - Minnesota
The Histogram • Horizontal Format • Left side of display: name and unscaled value • Right side: histogram of scaled value R. Smith - University of St Thomas - Minnesota
What to Start On • Create a sample file • Read in names and numbers • Looking for the Tab • Can use a ‘string.h’ function • See 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 Integer • Use the function in the library R. Smith - University of St Thomas - Minnesota
Sample Input • Moose Mountain 986 • Mystery Mountain 650 • Eagle Mountain 650 • Ullr Mountain 350 • Welch Village 360 • Spirit Mountain 700 • Keystone 3128 • Steamboat Springs 3668 • Sunday River 2340 R. 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. R. Smith - University of St Thomas - Minnesota