CMIS 141
Lab exercise
Array play
Modify the ArrayStuff program so it also determines the index of the
min value and the index of the max value in the array (i.e. where the
min and max are in the array).
Add code that displays the index(es) of the (duplicates) min and max
values. This should be a loop after the previous loop, so that the
min and max are known. Add to it the number of duplicates of the min
and max values.
Add code that counts the number of values less than the average.
(This isn't so useful because the data is random, so about half of the
values will be less than the average. Real data is often not uniformly
distributed and so the average [arithmetic mean] is not the middle
value [median]. Example: if we and Bill Gates are in this room, the
average person's wealth is about $5,000,000,000, but almost every one
of us has wealth less than this average.)
Add code that counts and sums the values less than a user-specified
number.
Modify that into code that counts and sums the values less than and
the values greater than a user-specified number.
Add code that doubles each value. Sum them and compare this new sum
with the prevous sum (the new one should be twice the previous sum).
If you're finished I'll think of more.
OK. I've thought of one.
Ask the user for a number and then search the array to see if that
number is in the array or not.
If the array is not in sorted order then you can't do much more than
start at the beginning and go through the array until either the item
is found (a successful search) or you get to the end of the array
(when the item is not in the array, an unsuccessful search). On
average, for a successful search about half the array elements are
looked at. For an unsuccessful search all the elements are examined.
In both cases that's a lot of work (think of an array with millions of
elements to be checked for the search value).
This is called sequential search.
If the array is sorted then a much quicker way to search exists called
binary search in which only a handful of elements needs to be looked
at before either finding the item or knowing it's not in the array.