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:
-
The machine allocates space for
8
intvalues within main memory. -
The identifier
notesis used to reference the location of the first element in main memory.
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.
-
The variable
notesspecifies an address. Array parameters are thus passed by reference, because the address is given. This means that the machine only has to pass the reference to the array—not copy the entire array— when passing arrays to functions. -
The variable
notesspecifies a starting address, but not an ending address or array. Programmers must must keep track of size separately to use an array in processing.
-
Program
max-min.cis a simple C program that computes the maximum, minimum, and average of an array of numbers; and programmax-array.ccomputes the maximum of an array of numbers using a separate function. Copy both of these programs and be sure you know how they work.-
Initialization in
find_maxuses the statementmax = array[0]. Why do you think this is done, rather than settingmaxto a large number (e.g., 1500) and starting the loop index at 0 rather than 1? -
Using
max-array.cas an example, add separate functions,find_minandfind_average, that compute the minimum and the average of the values in an array. -
Write an additional procedure
find_statsthat 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)
max-array.cfile, along with your new functinos. Don't forget documentation! -
Initialization in
-
In program
max-array.cfrom Step 1, modifyfind_maxso that it adds 3 to each array element after computing the maximum. That is, the loop infind_maxshould be changed to
Explain what happens when the array is printed in the main program and why. Include your answer somewhere in thefor (j = 1; j <array_size; j++) { if (array[j] > max) max = array[j]; array[j] += 3.0; }max-array.cfile.
Looping through Arrays
-
Write a program,
reverse-message.cthat 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'noDIn 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. - 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:
- Identified all authors who contributed to the program
- Included an Academic Honesty Statement at the top of each program file.
- Documented each function (except for main).
- Included comments within main to describe what you are doing.
Note: there is no autograder for this program.