# COMP1511 Revision Arrays Sample Solutions

### Revision Exercise: First Middle Last

cp -n /web/cs1511/20T3/activities/first_middle_last/first_middle_last.c .


// Print the first, middle, and last values of an array,
// on separate lines.
void print_first_middle_last(int size, int array[MAX_LENGTH]) {
}

You are to implement the print_first_middle_last function which should print the first, middle, and last elements of the array, each on a new line.

The middle element of the array is the one with index size/2.

The file first_middle_last.c contains a main function which reads values into an array and calls print_first_middle_last.

Here is how first_middle_last.c should behave after you add the correct code to the function print_first_middle_last:

dcc first_middle_last.c -o first_middle_last
./first_middle_last
Enter array size: 5
Enter array values: 1 2 3 4 5
1
3
5
./first_middle_last
Enter array size: 6
Enter array values: 1 2 3 4 5 6
1
4
6


When you think your program is working you can use autotest to run some simple automated tests:

1511 autotest first_middle_last


### Revision Exercise: Show Array

cp -n /web/cs1511/20T3/activities/show_array/show_array.c .


// This function prints the array in the format
// [1, 2, 3, ...]
void show_array(int size, int array[size]) {
}

You are to implement the void show_array function which should print the given array surround by square brackets and with value separated by commas, e.g.  [1, 2, 3] 

The file show_array.c contains a main function which reads values into an array and calls show_array.

Here is how show_array.c should behave after you add the correct code to the function show_array:

dcc show_array.c -o show_array
./show_array
Enter array size: 3
Enter array values: 1 2 3
[1, 2, 3]
./show_array
Enter array size: 1
Enter array values: 17
[17]


When you think your program is working you can use autotest to run some simple automated tests:

1511 autotest show_array


### Revision Exercise: Scan Array

You should make sure you have completed show_array before completing this task.

cp -n /web/cs1511/20T3/activities/scan_array/scan_array.c .


// This function reads in values from standard input into an array.
void scan_array(int size, int array[size]) {
}

Copy your show_array function code into scan_array.c.

Next, write the scan_array function, which is given an array and its size and reads in values from standard input to fill the array.

The file scan_array.c contains a main function which reads values into an array and calls scan_array.

Here is how scan_array.c should behave after you add the correct code to the function scan_array:

dcc scan_array.c -o scan_array
./scan_array
Enter array size: 3
Enter array values: 1 2 3
[1, 2, 3]
./scan_array
Enter array size: 1
Enter array values: 17
[17]


When you think your program is working you can use autotest to run some simple automated tests:

1511 autotest scan_array


### Revision Exercise: Copy Array

You should make sure you have completed show_array and scan_array before completing this task.

cp -n /web/cs1511/20T3/activities/copy_array/copy_array.c .


// Copy size elements from from_array into to_array.
void copy_array(int size, int from_array[size], int to_array[size]) {
}


Copy your scan_array and show_array and into copy_array.c

You will be implementing an array copying function copy_array which should copy the values from the from_array to the to_array.

Here is how copy_array.c should behave after you add the correct code to the function copy_array:

dcc copy_array.c -o copy_array
./copy_array
Enter array size: 3
Enter array values: 1 2 3
[1, 2, 3]
[1, 2, 3]
./copy_array
Enter array size: 1
Enter array values: 17
[17]
[17]


When you think your program is working you can use autotest to run some simple automated tests:

1511 autotest copy_array


### Revision Exercise: Array Functions

In this exercise, you'll be creating a small collection of functions that could help you with working on arrays.

cp -n /web/cs1511/20T3/activities/array_functions/array_functions.c .


and implement the functions.

Some of these functions read through an array and return some information about the array but do not change the array in any way.

These functions are:

• array_max; which returns the maximum value in the array,
• array_min; which returns the minimum value in the array,
• array_average; which returns the average value in the array, and
• array_sum; which returns the sum of all values in the array.

Note: array_average should return the greatest int less than or equal to the statistical mean of the array.

Some other functions change the array values or have an effect outside the program. We say that these functions have side effects as they do something other than just return a value.

These functions are:

• array_add; which adds a value to every element in the array,
• array_scale; which multiplies every value in array by the same value, and
• scan_array; which reads in values from input and stores them in the array
• show_array; which shows an array on the screen in the style [1, 2, 3, 4, 5, 6]
• copy_array; which copies one array into another
The file array_functions.c contains a main function with some unit tests to help you test your functions.

When you think your program is working you can use autotest to run some simple automated tests:

1511 autotest array_functions