COMP1511 18s1 (webcms)
COMP1511 18s1 (flask)

Revision Exercise: First Middle Last

Download first_middle_last.c here, or copy it to your CSE account using the following command:
cp -n /web/cs1511/18s1/activities/first_middle_last/first_middle_last.c .
Your task is to add code to this function in 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]) {
    // Your code goes here!
}

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
./print_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

Autotest Results

96% of 615 students who have autotested first_middle_last.c so far, passed all autotest tests.
  • 97% passed test fml_0
  • 97% passed test fml_1
  • 99% passed test fml_2
  • 99% passed test fml_3

Revision Exercise: Show Array

Download show_array.c here, or copy it to your CSE account using the following command:
cp -n /web/cs1511/18s1/activities/show_array/show_array.c .
Your task is to add code to this function in show_array.c:
// this function prints the array in the format
// [1, 2, 3, ...]
void show_array(int size, int array[size]) {
    // Put your code here
}

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

Autotest Results

99% of 602 students who have autotested show_array.c so far, passed all autotest tests.
  • 99% passed test sa_0
  • 99% passed test sa_1
  • 99% passed test sa_2

Revision Exercise: Scan Array

You should make sure you have completed show_array before completing this task. Download scan_array.c here, or copy it to your CSE account using the following command:

cp -n /web/cs1511/18s1/activities/scan_array/scan_array.c .
Your task is to add code to this function in scan_array.c:
// this function reads in values from
// standard input into an array
void scan_array(int size, int array[size]) {
    // PUT YOUR CODE HERE
}

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

Autotest Results

99% of 554 students who have autotested scan_array.c so far, passed all autotest tests.
  • 99% passed test sa_0
  • 99% passed test sa_1
  • 99% passed test sa_2

Revision Exercise: Array Max

Download array_max.c here, or copy it to your CSE account using the following command:
cp -n /web/cs1511/18s1/activities/array_max/array_max.c .
Your task is to add code to this function in array_max.c:
// Return the largest value in a given array.
int array_max(int size, int array[size]) {
    // Put your code here
    return 42;
}

You are to implement the array_max function which should return the largest value in the array.

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

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

dcc array_max.c -o array_max
./array_max
Enter array size: 5
Enter array values: 1 2 3 4 5
Maximum value is 5.
./array_max
Enter array size: 6
Enter array values: -1 -3 -9 -17 -2 -8
Maximum value is -1.

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

1511 autotest array_max

Autotest Results

98% of 566 students who have autotested array_max.c so far, passed all autotest tests.
  • 100% passed test am_0
  • 100% passed test am_1
  • 100% passed test am_2
  • 99% passed test am_3

Revision Exercise: Copy Array

You should make sure you have completed show_array and scan_array before completing this task. Download copy_array.c here, or copy it to your CSE account using the following command:

cp -n /web/cs1511/18s1/activities/copy_array/copy_array.c .
Your task is to add code to this function in 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]) {
    // Put your code here
}

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

Autotest Results

97% of 514 students who have autotested copy_array.c so far, passed all autotest tests.
  • 98% passed test ca_0
  • 97% passed test ca_1
  • 98% passed test ca_2

Revision Exercise: Array Functions

In this exercise, you'll be creating a small collection of functions that could help you with working on arrays. Download array_functions.c here, or copy it to your CSE account using the following command:
cp -n /web/cs1511/18s1/activities/array_functions/array_functions.c .
Your task is to implement the missing functions in 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.

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

Autotest Results

96% of 459 students who have autotested array_functions.c so far, passed all autotest tests.
  • 97% passed test 1
  • 97% passed test 10
  • 97% passed test 11
  • 97% passed test 12
  • 97% passed test 13
  • 97% passed test 14
  • 96% passed test 15
  • 97% passed test 16
  • 97% passed test 17
  • 97% passed test 18
  • 97% passed test 19
  • 97% passed test 2
  • 97% passed test 20
  • 97% passed test 3
  • 97% passed test 4
  • 97% passed test 5
  • 97% passed test 6
  • 97% passed test 7
  • 97% passed test 8
  • 97% passed test 9