In this stage, you'll improve your functions related to
NullableInts, making use of references and const. You'll also
create add conversion functions between NullableInts and other
formats. Finally, you'll use your new functions to write a program
that constructs a NullableInts based on command line arguments.
See the overall project description here.
- tweaked the rough sanity check in test 15 that looks for delete
- use references
- use const
- parse strings
- manipulate double pointers
- write programs that accept command line input
Overload your previous Average function that previously accepted a
pointer to a NullableInts; it should now be possible to call
Average with a reference to a NullableInts as well. The
computation should be the equivalent either way (consider having one
function call the other to avoid copying code).
Add a safety check to the Average function that accepts a pointer to
check whether it is nullptr -- if it is, return an error (!ok)
rather than segfault.
Both your Average functions should accept const inputs.
Write a StrsToNullableInts function with this signature:
NullableInts* StrsToNullableInts(std::vector<std::string> inputs);Each entry in inputs will either be "null" or a string that can be
converted to an integer (e.g., something like "5" or "-368"). You
don't need to support other cases.
There should be an entry in nums (of NullableInts) corresponding
to every entry in inputs. If you encounter a "null", it doesn't
matter what int you place in nums, but a valid bit for that
position should indicate that the value is to be ignored.
Write a NullableIntsToArray function that accepts a reference to a
NullableInts (first arg) and a double pointer to int (second arg).
Callers will pass an address of an int pointer to this function. You
will allocate an int array and make the caller's pointer point to the
first element of the array.
The size of the array should equal the number of valid entries in the
NullableIntsToArray. This size should also be the return value of
the function.
Note that an int array does not support a notion of null or missing
values, so some indexes will shift over when you populate the array.
For example, if you NullableInts has something like [3,null,4], your
resulting int array will look like this: [3, 4]. Observe that 4 is at
index 2 in the original structure but at index 1 in the resulting
array.
Write a p3.cpp program that uses the new functionality in your library.
It should accept command line arguments that are either ints or
"null". Use StrsToNullableInts to convert these args to a
NullableInts, call Average on it, then print the result.
For example, ./p3 2 3 should print 2.5. ./p3 2 null 3 would also
print 2.5 since Average ignores missing values. Something like
./p3 hello may crash (you don't need to handle that scenario now).
If Average returns an error status (!ok), you should just print
"failed" -- you can expect that for runs like ./p3 or ./p3 null null.