COMP1511 Revision Arrays Exercises
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/21T1/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!
}
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
Download show_array.c here, or copy it to your CSE account using the following command:
cp -n /web/cs1511/21T1/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.
}
[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.
Download scan_array.c here, or copy it to your CSE account using the following command:
cp -n /web/cs1511/21T1/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
}
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.
Download copy_array.c here, or copy it to your CSE account using the following command:
cp -n /web/cs1511/21T1/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
Revision Exercise: Array Functions
Download array_functions.c here, or copy it to your CSE account using the following command:
cp -n /web/cs1511/21T1/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, andarray_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, andscan_array
; which reads in values from input and stores them in the arrayshow_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
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