## $Id: s3-quiz.html,v 1.4 1996/10/04 21:55:27 ellard2 Exp ellard2 $
## Dan Ellard - Review Problems

Review Problems - Midterm 1


Assumptions

For the following problems, you may assume that the Roberts libraries are already included, and that the following definitions are available:

		/* The length of all the arrays	is	*/
		/*	always ARRAY_LENGTH.		*/
	#define	ARRAY_LENGTH	129

		/* The different ways that an array can	*/
		/* be sorted.  Note that these values	*/
		/* may appear to be chosen at random-	*/
		/* they were.				*/
	#define	ASCENDING	102
	#define	DESCENDING	93
	#define	UNSORTED	8


Array problems

  1. void array_get (int array [ARRAY_LENGTH]);

    Write array_get, a function that reads in ARRAY_LENGTH integers from the user and places them into the given array array. Feel free to use GetInteger.

  2. int array_min (int array [ARRAY_LENGTH]);

    Write array_min, a function that returns the minimum (smallest) value in the array.

  3. int array_min_index (int array [ARRAY_LENGTH]);

    Write array_min_index, that is like array_min, except that it returns the index in array occupied by the smallest element.

  4. int array_max (int array [ARRAY_LENGTH]);

    Write array_max, a function that returns the maximum (largest) value in the array.

  5. int array_max_index (int array [ARRAY_LENGTH]);

    Write array_max_index. This function is similar to array_max, except that it returns the index in array occupied by the largest element instead of the value of the largest element.

  6. int array_max_abs (int array [ARRAY_LENGTH]);

    Write array_max_abs, a function that returns the value of the element with the largest absolute value in the array. (Hint- you may assume that array_min and array_max have already been written. You may find them useful.)

  7. int array_min_abs (int array [ARRAY_LENGTH]);

    Write array_min_abs, a function that returns the element with the smallest absolute value in the array. (Like for the previous problem, you may assume that array_min and array_max have already been written- but in this case they are not nearly so useful.)

  8. int array_sum (int array [ARRAY_LENGTH]);

    Write array_sum, a function that returns the sum of all the elements in array.

  9. int array_product (int array [ARRAY_LENGTH]);

    Write array_product, a function that returns the product of all of the elements in array.

  10. int array_average (int array [ARRAY_LENGTH]);

    Write array_average, a function that returns the average value of the elements in array (as an integer).

  11. bool array_sorted_ascending (int array [ARRAY_LENGTH]);

    Write array_sorted_ascending, a function that returns TRUE if array is sorted in ascending order (each number <= the next), FALSE otherwise.

  12. bool array_sorted_descending (int array [ARRAY_LENGTH]);

    Write array_sorted_descending, a function that returns TRUE if array is sorted in descending order (each number >= the next), FALSE otherwise.

  13. int array_sorted (int array [ARRAY_LENGTH]);

    Write array_sorted, a function that returns ASCENDING if array is sorted in ascending order, DESCENDING if array is sorted in descending order, or UNSORTED if array is not sorted. If all of the elements of the array have the same value, then this function should return ASCENDING.

  14. void array_print (int array [ARRAY_LENGTH]);

    Write array_print, a function that prints the contents of the array. Make the output easy to read.

  15. void array_larger (int a1 [ARRAY_LENGTH], int a2 [ARRAY_LENGTH);

    Write array_larger, a function that returns TRUE if every element in a1 is larger than every element in a2. (Hint- there's a hard way and an easy way to do this. The easy way is very easy, and uses some of the functions mentioned previously.)


Other problems

  1. void print_multiples (int min, int max, int factor);

    Write a function that prints out every number from min to max (including both min and max) that is evenly divisible by factor. You may assume that min, max, and factor have already been defined and are integers. You may assume that factor is not zero, although you may not assume that it is positive.

  2. void print_number (int number);

    Write a function print_number that prints out any integer greater than zero and less than 1000 in "words", so that the number 123 would be printed as "one two three", and 47 would be printed as "four seven".

    Do this three ways: first using an array, then another using switch, and finally using a cascaded if statement. What is good or bad about each of these approaches?


Dan Ellard (ellard@deas.harvard.edu)