Arrays

The goal of this lab is to introduce arrays as data structures and as parameters to functions.

An array is used to arrange data in memory. The cubbies just outside the dining hall represent a real-life example of this data structure; the fixed-size boxes are arrayed in a grid. The following steps illustrate the use of arrays within C.

Arrays and Functions

Consider the following declaration within a C program:

int notes[8] = {523, 587, 659, 698, 783, 880, 987, 1048};

C uses this declaration in at least two ways:

To clarify, the variable notes identifies the start of the the array. Thus, notes[0] specifies the first value in the block of memory, notes[1] specifies the second value in the block of memory, etc. This interpretation of the array has two consequences when using functions.


  1. Program max-min.c is a simple C program that computes the maximum, minimum, and average of an array of numbers; and program max-array.c computes the maximum of an array of numbers using a separate function. Copy both of these programs and be sure you know how they work.
    1. Initialization in find_max uses the statement max = array[0]. Why do you think this is done, rather than setting max to a large number (e.g., 1500) and starting the loop index at 0 rather than 1?
    2. Using max-array.c as an example, add separate functions, find_min and find_average, that compute the minimum and the average of the values in an array.
    3. Write an additional procedure find_stats that computes the maximum, minimum, and average values within one function. Since multiple values are to be computed and used by the main program, this function must pass values back using reference parameters. The function declaration should be:
      void
      find_stats (double array[], int array_size,
                  double * p_max, double * p_min, double * p_average)
    Include your answer to problem 1a somewhere in the max-array.c file, along with your new functinos. Don't forget documentation!
  1. In program max-array.c from Step 1, modify find_max so that it adds 3 to each array element after computing the maximum. That is, the loop in find_max should be changed to
    for (j = 1; j <array_size; j++)
    { 
      if (array[j] > max)
        max = array[j];
      array[j] += 3.0;
    }
    Explain what happens when the array is printed in the main program and why. Include your answer somewhere in the max-array.c file.

Looping through Arrays

  1. Write a program, reverse-message.c that reads a message, then prints the reversal of the message. Here is an example of how the program runs:

    Enter a message: Don't get mad, get even.
    Reversal is: .neve teg ,dam teg t'noD
    

    In order to accomplish this task, read in the message one character at a time using scanf("%c",??), where you should replace ?? with a location within an array. You should define your array with a set size (say 100), and stop reading in the message when either the array is full, or you encounter the '\n' character.

  2. Revise your program from problem 3, save this new program in a file called reverse-message-v2.c. In this version, use a pointer instead of an integer to keep track of the current position in the array.

Lab Report 4 Submission

For this lab submission, submit the files max-array.c, reverse-message.c, and reverse-message-v2.c into Gradescope. Your file max-array.c should include the answer to problems 1a and 2. Before submitting, review the Guidelines for submitting work. A testing trasncript or plan is not required for this assignment (though you are welcome to create one and submit it anyway!). Make sure you have:

  1. Identified all authors who contributed to the program
  2. Included an Academic Honesty Statement at the top of each program file.
  3. Documented each function (except for main).
  4. Included comments within main to describe what you are doing.

Note: there is no autograder for this program.