Programming Exercises

I l @ ve RuBoard

Programming Exercises

  1. Modify the rain program in Listing 10.12 so that it does the calculations using pointers instead of subscripts. (You still have to declare and initialize the array.)

  2. Write a program that initializes an array and then copies the contents of the array into two other arrays. (All three arrays should be declared in the main program.) To make the first copy, use a function with array notation. To make the second copy, use a function with pointer notation and pointer incrementing. Have each function take as arguments the name of the source array, the name of the target array, and the number of elements to be copied .

  3. Write a function that returns the largest value stored in an array. Test the function in a simple program.

  4. Write a function that returns the index of the largest value stored in an array. Test the function in a simple program.

  5. Write a function that returns the difference between the largest and smallest elements of an array. Test the function in a simple program.

  6. Write a program that initializes a two-dimensional array and uses one of the copy functions from Exercise 2 to copy it to a second two-dimensional array. (Because a two-dimensional array is an array of arrays, a one-dimensional copy function can be used with each subarray.)

  7. Use a copy function from Exercise 2 to copy the third through fifth elements of a seven-element array into a three-element array. The function itself need not be altered ; just choose the right actual arguments. (The actual arguments need not be an array name and array size . They only have to be the address of an array element and a number of elements to be processed .)

  8. Write a function that sets each element in an array to the sum of the corresponding elements in two other arrays. That is, if array 1 has the values 2 , 4 , 5 , and 8 and array 2 has the values 1 , , 4 , and 6 , the function assigns array 3 the values 3 , 4 , 9 , and 14 . The function should take three array names and an array size as arguments. Test the function in a simple program.

  9. Write a program that declares a 3-by-5 array and initializes it to some values of your choice. Have the program print the values, double all the values, and then display the new values. Write a function to do the displaying and a second function to do the doubling. Have the functions take the array name and the number of rows as arguments.

  10. Rewrite the rain program in Listing 10.12 so that the main tasks are performed by functions instead of in main() .

  11. Write a program that prompts the user to enter three sets of five double numbers each. The program should accomplish all of the following:

    1. Store the information in a 3-by-5 array.

    2. Compute the average of each set of five values.

    3. Compute the average of the values.

    4. Determine the largest value of the 15 values.

    5. Report the results.

    Each major task should be handled by a separate function.

I l @ ve RuBoard


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net