Next: About this document ... Up: Arrays Previous: Review Questions

Exercises

  1. To familiarize yourself with using arrays write a program that declares two float arrays, say with 5 elements each, and carries out the following:
    1. Input some data from the user into the two arrays.
    2. Output the sum of the elements in each of the two arrays.
    3. Output the inner product of the two arrays - that is the sum of the products of corresponding elements A[0]*B[0] + A[1]*B[1]+ ....etc.
    4. Produce an estimate of how different the values in the two arrays are by evaluating the sum of squares of the differences between corresponding elements of the two arrays divided by the number of elements.
    Start by only entering and printing the values in the arrays to ensure you are capturing the data correctly. Then add each of the facilities above in turn.

  2. A popular method of displaying data is in a Histogram. A histogram counts how many items of data fall in each of n equally sized intervals and displays the results as a bar chart in which each bar is proportional in length to the number of data items falling in that interval.

    Write a program that generates n random integers in the range 0-99 (see exercise 3 of Lesson 16) and produces a Histogram from the data. Assume that we wish to count the number of numbers that lie in each of the intervals 0-9, 10-19, 20-29, ........., 90-99. This requires that we hold 10 counts, use an array to hold the 10 counts. While it would be possible to check which range a value x lies in by using if-else statements this would be pretty tedious. A much better way is to note that the value of x/10 returns the index of the count array element to increment. Having calculated the interval counts draw the Histogram by printing each bar of the Histogram as an appropriately sized line of X's across the screen as below

     0 -  9  16  XXXXXXXXXXXXXXXX
    10 - 19  13  XXXXXXXXXXXXX
    20 - 29  17  XXXXXXXXXXXXXXXXX
        etc.
    

  3. In question 1 above you should have written C++ statements to enter numbers into an array. Convert these statements into a general function for array input. Your function should indicate the number of elements to be entered and should signal an error situation if this is greater than the size of the array--think about the required parameters. Also write a function to output n elements of a given array five to a line.

    Write a driver program to test these functions and once you are satisfied they are working correctly write functions:

    1. To return the minimum element in the first n elements of an array.
    2. To return a count of the number of corresponding elements which differ in the first n elements of two arrays of the same size.
    3. Which searches the first n elements of an array for an element with a given value. If the value is found then the function should return true and also return the index of the element in the array. If not found then the function should return false.

    In these functions incorporate error testing for a number of elements greater than the array size.



Next: About this document ... Up: Arrays Previous: Review Questions