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

Test Conditions

These questions must be completed under self-administered exam-like conditions.

You must time the test yourself and ensure you comply with the conditions below.


You may access this language documentation while attempting this test:
Any violation of the test conditions will results in a mark of zero for the entire weekly test component.

Return How Many Even Numbers are in an Array

Write a C function which returns the number of even values in an array.

Place your answer in a file named count_even_function.c

Your function should take two parameters: the length of the array, and the array. It must have this prototype:


int count_even(int length, int array[length]);
Your function should return a single integer: the number of even values in the array.

For example if the array contains these 8 elements:

16, 7, 8, 12, 13, 19, 21, 12

Your function should return 4, because these 4 elements are even:

16, 8, 12, 12

Assumptions/Restrictions/Clarifications.

An even number is divisible by 2.

Your function should return a single integer.

Your function should not change the array it is given.

Your function should not call scanf (or getchar or fgets).

You can assume the array only contains positive integers.

You can assume the array contains at least one integer.

Your function should not print anything. It should not call printf.

Your submitted file may contain a main function. It will not be tested or marked.

Starting Code

Here is some code you can use to start this question.
#include <stdio.h>
#include <stdlib.h>

// return the number of even values in an array.
int count_even(int length, int array[]) {
    // PUT YOUR CODE HERE (you must change the next line!)
    return 42;
}

// This is a simple main function which could be used
// to test your count_even function.
// It will not be marked.
// Only your count_even function will be marked.

# define TEST_ARRAY_SIZE 8

int main(void) {
    int test_array[TEST_ARRAY_SIZE] = {16, 7, 8, 12, 13, 19, 21, 12};

    int result = count_even(TEST_ARRAY_SIZE, test_array);

    printf("%d\n", result);
    return 0;
}

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

1511 autotest count_even_function

Autotest Results

98% of 863 students who have autotested count_even_function.c so far, passed all autotest tests.
  • 98% passed test 0
  • 99% passed test 1
  • 98% passed test 2
  • 98% passed test 3
  • 99% passed test 4
When you are finished working on this exercise you must submit your work by running give:
give cs1511 test07_count_even_function count_even_function.c

Remove Duplicates From array

Write a C function that removes duplicate elements from an array, by copying the non-duplicate values to a second array, i.e. only the first occurrence of any value should be copied.

Place your answer in a file named remove_duplicates_function.c

Your function should take three parameters: the length of source array, the source array itself, and the destination array. It must have this prototype:


int remove_duplicates(int length, int source[length], int destination[length]);
Your function should return a single integer: the number of elements copied to the destination array.

For example if the source array contains these 8 elements:

3, 1, 4, 1, 5, 9

Your function should copy these 5 values to the destination array:

3, 1, 4, 5, 9

Your function should return the integer 5, because there were 5 values copied -- the second occurrence of the digit 1 was not copied.

Assumptions/Restrictions/Clarifications.

You can assume the source array only contains positive integers.

You can assume the source array contains at least one integer.

You can assume that the destination array will always be large enough to fit all of the copied values.

You cannot assume anything about the number of duplicates, i.e. there may not be any duplicates, or conversely, the entire array may be duplicates.

Your function should return a single integer.

Your function should not change the array it is given.

Your function should not call scanf (or getchar or fgets).

Your function should not print anything. It should not call printf.

Your submitted file may contain a main function. It will not be tested or marked.

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

1511 autotest remove_duplicates_function

Autotest Results

84% of 816 students who have autotested remove_duplicates_function.c so far, passed all autotest tests.
  • 86% passed test 0
  • 85% passed test 1
  • 88% passed test 2
  • 87% passed test 3
  • 86% passed test 4
  • 90% passed test 5
  • 89% passed test 6
When you are finished working on this exercise you must submit your work by running give:
give cs1511 test07_remove_duplicates_function remove_duplicates_function.c

Most Frequent Integer

Write a C program most_frequent_integer.c which reads integers from standard input until it reaches end-of-input.

It should then print the most frequently occurring integer.

If there is a tie for the most frequent integer, your program should print the largest integer that is tied for most frequent.

Match the the example below EXACTLY.

./most_frequent_integer
1
1
2
3
5

1

Explanation: given the input 1 1 2 3 5, your program should print 1, as this was the most frequently occurring integer -- it appeared twice.
./most_frequent_integer
655
10
204
8192
76
38
204
43912
204

204

Explanation: given the input 655 10 204 8192 76 38 204 43912 204, your program should print 204, as this was the most frequently occurring integer -- it appeared 3 times.
./most_frequent_integer
1
2
1
2
1
2

2

Explanation: given the input 1 2 1 2 1 2, your program should print 2, as this is the largest most frequently occurring integer -- both 1 and 2 appeared three times, and 2 is the largest value.

Assumptions/Restrictions/Clarifications.

You can assume the input will only only contain integers, one per line.

You can assume each line will contain one and only one integer.

You can assume your input contains at least one integer.

You can assume your input contains no more than 100,000 integers.

You are free to write this program in any way you wish: there is no specific function that you need to implement. Note that your program will need to have a `main` function.

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

1511 autotest most_frequent_integer

Autotest Results

81% of 654 students who have autotested most_frequent_integer.c so far, passed all autotest tests.
  • 85% passed test 0
  • 85% passed test 1
  • 88% passed test 10
  • 91% passed test 2
  • 92% passed test 3
  • 91% passed test 4
  • 88% passed test 5
  • 84% passed test 6
  • 86% passed test 7
  • 87% passed test 8
  • 86% passed test 9
When you are finished working on this exercise you must submit your work by running give:
give cs1511 test07_most_frequent_integer most_frequent_integer.c

Submission

When you are finished each exercise make sure you submit your work by running give.

You can run give multiple times. Only your last submission will be marked.

Don't submit any exercises you haven't attempted.

If you are working at home, you may find it more convenient to upload your work via give's web interface.

Remember you have until Wednesday 25 April 23:59:59 to complete this test.

Automarking will be run several days after the submission deadline for the test. When complete you can view automarking here and you can view the the resulting mark via give's web interface