CS33 -- Laboratory 10

Due: Thursday, Nov. 11 by 11:59 pm

You are encouraged to work with a partner.

Work in a subdirectory called initslab10, where inits are your initials. Once you have completed the C programs and made script files to demonstrate your testing of each, remove any extraneous files so that your directory contains just the .c files and the script files. Then make a tarball of that directory and send it to me as an attachment to the email whose subject line should be CS33 Lab10. Send the email to cfk@cs.swarthmore.edu.

Each program must follow these following guidelines:


LAB 10: Pointers and Arrays


  1. In this first program (called maxptr.c) you will write two functions whose prototypes are shown here:
    int *max(int *a, int *b);
    int *max3(int *a, int *b, int *c);
    
    (We will discuss something very similar in class on Monday or Wednesday.)

    The max function has two pointers to integers as parameters and returns the pointer that points to the larger integer.

    The max3 function has three pointers to integers as parameters and returns the pointer that points to the largest integer. The max3 function should make use of the max function in computing its answer.

    In main, ask the user to enter three values, then increment the largest value, and print all three values back to the user.

    Here is sample output (underlined text is text entered by the user):

    Enter integer values for x, y, and z: 3 8 5
    The values after incrementing the largest value are:
    x = 3, y = 9, z = 5
    

  2. Write a program called histogram.c which will print a histogram of test scores entered. Write and use a function to tabulate the frequency of the test scores entered by the user and return an array with the results. We are interested in the ranges shown in the output below (i.e. 0-9, 10-19, etc.)
    Use another function to print a histogram of the results.

    Here is a test run of my solution. The program prompted with the question mark. I entered the number after the question mark (pretty good class). My program output the histogram.

    licorice% a.out This program displays a histogram of test scores. Enter a -1 to signal end of input. ? 88 ? 76 ? 99 ? 97 ? 85 ? 91 ? 92 ? 61 ? 84 ? 97 ? 98 ? 88 ? -1 0-9 | 10-19 | 20-29 | 30-39 | 40-49 | 50-59 | 60-69 | * 70-79 | * 80-89 | **** 90-99 | ****** 100 | licorice%

  3. In this program (called selection.c) you will write a program that sorts an array of integers using Selection Sort. A different sorting method is discussed on pp. 446-449 of our text. In Selection Sort, the values of an array are sorted (from smallest to largest) by using the following algorithm:

    1. Use #define to set ARRAYSIZE to 25.
    2. In main, create an array of integers with space to hold ARRAYSIZE values.
    3. Write a function called populateArray which populates an array of integers with random values between 0 and highVal (inclusive). If highVal ≤ 0, all elements are set to 0. We will discuss generating random numbers on Monday or Wednesday. You can write the sort and test it on numbers you enter or assign before we discuss generating random numbers. The prototype for this function is:
      void populateArray(int *array, int size, int highVal);
      
    4. Write a function called isSorted that returns TRUE if the array is in sorted order (from smallest to largest); otherwise, it returns FALSE. Both TRUE and FALSE should be defined using the #define syntax, and the type Boolean should be created used typedef. The prototype for this function is shown below:
      Boolean isSorted(int *array, int size);
      
    5. Write the swap and selectionSort functions according to the prototypes shown here:
      void swap(int *a, int *b);
      void selectionSort(int *array, int size);
      
    6. In main, write some test code that allows the user to enter the value of highVal, prints out the array before it's sorted and the value of isSorted, then prints out the array after it's been sorted and, again, the value of isSorted.

Note: As of 1 Oct, the final exam for CS33 has been scheduled by the registrar for Friday 12/17/2009, 9:00am-12:00pm in SC240. Plan to attend.