Lab 08: Arrays and Pointers

Due 11:59pm Wednesday November 16, 2011

Introduction

The program handin33 will only submit files in the cs33/labs/08 directory. (You should run update33 first to set up the directory and create any necessary files.)

Remember: You are encouraged to work with a partner.

Each program must follow these following guidelines:


  1. In this first program (called minptr.c) you will write two functions whose prototypes are shown here:
    int *min(int *a, int *b);
    int *min3(int *a, int *b, int *c);
    
    (This first function should be familiar to you -- we discussed something very similar to this in class on Thursday.)

    The min function takes two pointers to integers as parameters and returns the pointer that points to the smaller integer.

    The min3 function takes three pointers to integers as parameters and returns the pointer that points to the smallest integer. The min3 function should make use of the min function in computing its answer.

    In main, ask the user to enter three values, then increment the smallest 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: 8 3 5
    The values after incrementing the smallest value are:
    x = 8, y = 4, z = 5
    

  2. In this program (called selection.c) you will write a program that sorts an array of integers using Selection Sort. In Selection Sort, the values of an array are sorted (from smallest to largest) by using the following algorithm:
    • Set i = 0
    • Find the smallest element in the portion of the list from i to the end.
    • Swap this smallest element with the element in position i.
    • Increment i
    • Repeat until i is the index of the last element of the list.

    1. In main, dynamically allocate (using malloc) an array of n integers. Be sure that when you are done using the memory, you should free any memory you malloc.

    2. 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. See cs33/class/11-03/dieroll.c for reference on how to create random values and on how to initialize the random number generator. The prototype for this function is:
      void populateArray(int *array, int size, int highVal);
      
    3. 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);
      
    4. Write the swap and selectionSort functions according to the prototypes shown here:
      void swap(int *a, int *b);
      void selectionSort(int *array, int size);
      
    5. In main, write some test code that allows the user to enter the value of n (the size of the array) and highVal. Call populateArray and print out the array before it's sorted and the value of isSorted. Then, call selectionSort and print out the array after it's been sorted and, again, the value of isSorted.

  3. 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.

    The prototypes for these functions should be:

    int* tabulate();
    void histogram(int *);
    

    Note: To receive full credit on this question, your program should be able to handle a arbitrarily large number of scores input by the user; however, you can receive significant partial credit for accepting a limited number of scores (e.g. 100). To handle an arbitrarily large number of scores, you may need to repeatedly allocate memory dynamically (using malloc). Be sure to free any memory you malloc when you are done using it.

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

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