Using the Integer Extractor
Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.
1) uniform_arithmetic
Now that you have written a function that extracts integers and stores them in an output array, let's develop a simple application for it. We are going to use the int_extractor function to compute very simple char array representing basic uniform arithmetic expressions where all integers are combined by the same operator (e.g. "1+2+4+8", "10-1-2").
Write a function uniform_arithmetic which takes in 3 arguments and returns a computed result:
char* data_array: An input char array that contains a string of integers separated by one of the following delimiters:'+','-','*','/'.char operator_: The char used as the delimiter in chopping up the input string and as the operator used to compute the expressionint num_ints: The number of integers in the input string, used to create anintarray big enough to hold the extractedints
You will be able to call the int_extractor function to extract the integers into an output array you create and then compute all the values in that array. uniform_arithmetic should be able to either add, subtract, multiply, or divide depending on the delimiter/operator passed in. Recall that the function signature of int_extractor is:
void int_extractor(char* data_array, int* output_values, char delimiter);
Hint: what should your running total be assigned initially? Does it differ depending on the operator?
2) measure_average
Another application that may be useful is extracting ints that represent measurements taken at different times and averaging them into a result. For example, say you take the temperature (in fahrenheit) of your room every 4 hour for 24 hours. A possible string to come out of this would be "my measurements: 69, 72, 73, 75, 72, 71".
What we want to do is take these integers and compute a simple average (in this case the result would be 72), but as it stands int_extractor can only deal with strings that contains only integers and a single delimiter seperating each. We will have to write a function measure_average that takes in a string of the format " any key can go here : 1, 0, 2" and figure out where to start parsing in order to extract our integers. Think about what string format our int_extractor takes in as an argument to be able to parse and extract.
measure_average will take in 2 arguments and return a float:
char* measurements: a string of the format mentioned above where a:will always separate a key or phrase from the measurement integer values, which are always separated by commas.int num_measurements: the number of integer measurements in the string to be used to determine the output length of theint_extractoroutput array.