# COMP1511 Revision Arrays Sample Solutions

### Revision Exercise: First Middle Last

cp -n /web/cs1511/21T3/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

Sample solution for first_middle_last.c
// Print the first, middle, and last elements of an array.

#include <stdio.h>
#include <stdlib.h>

#define MAX_ARRAY_SIZE 100

void print_first_middle_last(int size, int array[size]);

// DO NOT CHANGE THIS MAIN FUNCTION
int main(int argc, char *argv[]) {
// Create an array.
int array[MAX_ARRAY_SIZE];

// Get the size of the array.
int size;
printf("Enter array size: ");
scanf("%d", &size);
if (size > MAX_ARRAY_SIZE) {
printf("Error: array is too big!\n");
return 1;
}

// Scan values into the array.
printf("Enter array values: ");
int i = 0;
while (i < size) {
scanf("%d", &array[i]);
i = i + 1;
}

print_first_middle_last(size, array);

return 0;
}

// Print the first, middle, and last values of the array, on
// separate lines.
void print_first_middle_last(int size, int array[size]) {
printf("%d\n", array[0]);
int middle = size/2;
printf("%d\n", array[middle]);
printf("%d\n", array[size - 1]);
}


### Revision Exercise: Show Array

cp -n /web/cs1511/21T3/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

Sample solution for show_array.c
// Print out an array.
// A sample solution.

#include <stdio.h>
#include <assert.h>

#define MAX_SIZE 1000

void show_array(int size, int array[size]);

// DO NOT CHANGE THIS MAIN FUNCTION
int main(int argc, char *argv[]) {
// Create the array.
int array[MAX_SIZE];

// Get the array size.
int size;
printf("Enter array size: ");
assert(scanf("%d", &size) == 1);
assert(size > 0);

// Scan in values for the array.
printf("Enter array values: ");
int i = 0;
while (i < size) {
assert(scanf("%d", &array[i]) == 1);
i = i + 1;
}

// Print the values.
show_array(size, array);

return 0;
}

// This function prints the array in the format
// [1, 2, 3, ...]
void show_array(int size, int array[size]) {
// Print open bracket
printf("[");

// If the array has elements, print them
if (size > 0) {
// Print the first element separately
printf("%d", array[0]);

// Loop over the rest of the elements
int i = 1;
while (i < size) {

// Print in list format
printf(", %d", array[i]);
i = i + 1;
}
}

// Print closing bracket
printf("]\n");
}


### Revision Exercise: Scan Array

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

cp -n /web/cs1511/21T3/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

Sample solution for scan_array.c
// Scan in an array.
// A sample solution.

#include <stdio.h>
#include <assert.h>

#define MAX_SIZE 1000

void scan_array(int size, int array[size]);
void show_array(int size, int array[size]);

// DO NOT CHANGE THIS MAIN FUNCTION
int main(int argc, char *argv[]) {
// Create the array.
int array[MAX_SIZE];

// Get the array size.
int size;
printf("Enter array size: ");
assert(scanf("%d", &size) == 1);
assert(size > 0);

// Scan in values for the array.
printf("Enter array values: ");
int i = 0;
while (i < size) {
assert(scanf("%d", &array[i]) == 1);
i = i + 1;
}

// Print the values.
show_array(size, array);

return 0;
}

// This function reads in values from
// standard input into an array.
// It assumes scanf reads a number
void scan_array(int size, int array[size]) {
int i = 0;
while (i < size) {
i = i + 1;
}
}

// This function prints the array in the format
// [1, 2, 3, ...]
void show_array(int size, int array[size]) {
// Print open bracket
printf("[");

// If the array has elements, print them
if (size > 0) {
// Print the first element separately
printf("%d", array[0]);

// Loop over the rest of the elements
int i = 1;
while (i < size) {

// Print in list format
printf(", %d", array[i]);
i = i + 1;
}
}

// Print closing bracket
printf("]\n");
}


### Revision Exercise: Copy Array

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

cp -n /web/cs1511/21T3/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

Sample solution for copy_array.c
// Copy an array.
// A sample solution.

#include <stdio.h>
#include <assert.h>

#define MAX_SIZE 1000

void scan_array(int size, int array[size]);
void show_array(int size, int array[size]);
void copy_array(int size, int from_array[size], int to_array[size]);

// DO NOT CHANGE THIS MAIN FUNCTION
int main(int argc, char *argv[]) {

// Create the arrays.
int array1[MAX_SIZE];
int array2[MAX_SIZE];

// Get the array size.
int size;
printf("Enter array size: ");
assert(scanf("%d", &size) == 1);
assert(size > 0);

// Scan in the values for the array.
printf("Enter array values: ");
scan_array(size, array1);

// Print the values of the first array.
show_array(size, array1);

// Copy the values from array1 to array2.
copy_array(size, array1, array2);

// Print the copied values.
show_array(size, array2);

return 0;
}

// Copy size elements from from_array into to_array.
void copy_array(int size, int from_array[size], int to_array[size]) {
int i = 0;
while (i < size) {
to_array[i] = from_array[i];
i = i + 1;
}
}

// This function reads in values from standard input into an array.
// It assumes scanf successfully reads in a number.
void scan_array(int size, int array[size]) {
int i = 0;
while (i < size) {
i = i + 1;
}
}

// This function prints the array in the format
// [1, 2, 3, ...]
void show_array(int size, int array[size]) {
// Print open bracket
printf("[");

// If the array has elements, print them
if (size > 0) {
// Print the first element separately
printf("%d", array[0]);

// Loop over the rest of the elements
int i = 1;
while (i < size) {

// Print in list format
printf(", %d", array[i]);
i = i + 1;
}
}

// Print closing bracket
printf("]\n");
}


### 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/21T3/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

Sample solution for array_functions.c
// Some useful array functions
// Sample solution.

#include <assert.h>
#include <stdio.h>

#define SMALL_ARRAY 5
#define MEDIUM_ARRAY 20
#define LARGE_ARRAY 200

// Display useful facts about the array (changes the array).
void array_facts(int size, int array[]);

void scan_array(int size, int array[]);
void show_array(int size, int array[]);

// Array functions without side effects.
int array_max(int size, int array[]);
int array_min(int size, int array[]);
int array_sum(int size, int array[]);
int array_average(int size, int array[]);

// Changing the whole array.
void array_add(int size, int array[], int num);
void array_scale(int size, int array[], int num);

int main(int argc, char *argv[]) {
// Create an array with 5 elements.
int array_a[SMALL_ARRAY] = {1, 2, 3, 4, 5};

// Assert the min, max, and sum are what we expect.
assert(array_min(SMALL_ARRAY, array_a) == 1);
assert(array_max(SMALL_ARRAY, array_a) == 5);
assert(array_sum(SMALL_ARRAY, array_a) == 15);
assert(array_average(SMALL_ARRAY, array_a) == 3);

// Add 1 to all of the values in the array.

// Check the min/max/sum/average again.
assert(array_min(SMALL_ARRAY, array_a) == 2);
assert(array_max(SMALL_ARRAY, array_a) == 6);
assert(array_sum(SMALL_ARRAY, array_a) == 20);
assert(array_average(SMALL_ARRAY, array_a) == 4);

// Create a medium-sized array to perform more unit tests.
int array_b[MEDIUM_ARRAY] = {0};
int b = 0;
while (b < MEDIUM_ARRAY) {
array_b[b] = b * b;
b = b + 1;
}

// Check that the min/max/sum/average are what we'd expect.
assert(array_min(MEDIUM_ARRAY, array_b) == 0);
assert(array_max(MEDIUM_ARRAY, array_b) == 361);
assert(array_sum(MEDIUM_ARRAY, array_b) == 2470);
assert(array_average(MEDIUM_ARRAY, array_b) == 123);

// Scale the array by -2, and then re-check the min/max/sum/average.
array_scale(MEDIUM_ARRAY, array_b, -2);
assert(array_min(MEDIUM_ARRAY, array_b) == -722);
assert(array_max(MEDIUM_ARRAY, array_b) == 0);
assert(array_sum(MEDIUM_ARRAY, array_b) == -4940);
assert(array_average(MEDIUM_ARRAY, array_b) == -247);

// Test an array with manual values
int manual_array[MEDIUM_ARRAY] = {0};
scan_array(MEDIUM_ARRAY, manual_array);
array_facts(MEDIUM_ARRAY, manual_array);

printf("All tests passed. You are Awesome!");

return 0;
}

// Display some useful facts about an array.
// NOTE: this will change the array that is used.
void array_facts(int size, int array[]) {
printf("The largest value in the array is %d\n",
array_max(size, array));
printf("The smallest value in the array is %d\n",
array_min(size, array));
printf("The sum of all values in the array is %d\n",
array_sum(size, array));
printf("The average value in the array is %d\n",
array_average(size, array));

int s = 0;
while (s < 5) {
printf("The array with %d added to all elements:\n", s);
show_array(size, array);
s = s + 1;
}

printf("The array with 10 subtracted from all elements:\n");
show_array(size, array);

int m = 1;
while (m <= 5) {
printf("The array with all elements multiplied by %d:\n", m);
array_scale(size, array, m);
show_array(size, array);
m = m + 1;
}
}

// Read values from the screen, and store the values in an array.
void scan_array(int size, int array[]) {
printf("Enter %d numbers: ", size);
int i = 0;
while (i < size) {
scanf("%d", &array[i]);
i = i + 1;
}
}

// Show an array on the screen.
// This should look like [0, 1, 2, 3, 4, 5]
void show_array(int size, int array[]) {
printf("[");
int i = 0;
while (i < size) {
printf("%d", array[i]);
i = i + 1;

// Add the comma and space
if (i != size) {
printf(", ");
}
}
printf("]\n");
}

// Return the largest value in an array.
int array_max(int size, int array[]) {
int max = array[0];
int i = 0;
while (i < size) {
if (array[i] > max) {
max = array[i];
}
i = i + 1;
}
return max;
}

// Return the smallest value in the array.
int array_min(int size, int array[]) {
int min = array[0];
int i = 0;
while (i < size) {
if (array[i] < min) {
min = array[i];
}
i = i + 1;
}
return min;
}

// Return the sum of all values in the array.
int array_sum(int size, int array[]) {
int sum = 0;
int i = 0;
while (i < size) {
sum += array[i];
i = i + 1;
}
return sum;
}

// Return the average of all values in the array.
int array_average(int size, int array[]) {
return array_sum(size, array) / size;
}

// Add num to all values in the array.
void array_add(int size, int array[], int num) {
int i = 0;
while (i < size) {
array[i] += num;
i = i + 1;
}
}

// Multiply all values in the array by num.
void array_scale(int size, int array[], int num) {
int i = 0;
while (i < size) {
array[i] *= num;
i = i + 1;
}
}