Practice Array Questions

Arrays

Array Revision Exercises

The following exercises are intended for revising your skills with arrays, and are not intended to be an example of realistic exam questions.

Revision Exercise: Read Ten

Write a C program read_ten.c which reads 10 integers from standard input, stores them in an array, and prints them on one line.

You may assume that the program’s input will contain only integers and will contain at least 10 integers, in other words you can assume scanf succeeeds.

Match the the example below EXACTLY.

$ ./read_ten
1
2
4
16
32
64
128
256
512
1024
Numbers were: 1 2 4 16 32 64 128 256 512 1024

Revision Exercise: Reverse Ten

Write a C program reverse_ten.c which reads 10 integers from standard input, and prints them on them one line in reverse order.

Hint: use read_ten.c as a starting point.

You may assume that the program’s input will contain only integers and will contain at least 10 integers, in other words you can assume scanf succeeeds.

Match the the example below EXACTLY.

$ ./reverse_ten
1
2
4
16
32
64
128
256
512
1024
Numbers were: 1024 512 256 128 64 32 16 4 2 1

Revision Exercise: Odd-Even Ten

Write a C program odd_even_ten.c which reads 10 integers from standard input, prints the odd numbers on one line, then prints the even numbers on one line.

Hint: use read_ten.c as a starting point.

You may assume that the program’s input will contain only positive integers and will contain at least 10 integers, in other words you can assume scanf succeeeds.

Match the the example below EXACTLY.

$ ./odd_even_ten
1
2
4
16
32
64
128
256
512
1024
Odd numbers were: 1
Even numbers were: 2 4 16 32 64 128 256 512 1024
$ ./odd_even_ten
3
5
7
11
13
11
9
7
5
3
Odd numbers were: 3 5 7 11 13 11 9 7 5 3
Even numbers were:

Revision Exercise: Odd-Even Negative

Write a C program odd_even_negative.c which reads integers from standard input until it reads a negative integer. It should then print the odd numbers on one line then print the even numbers on one line.

Hint: use odd_even_ten.c as a starting point.

You may assume that the program’s input will contain only integers, in other words you can assume scanf succeeeds.

You can assume a negative integer will always be read.

You can assume at most 1000 integers are read before a negative integer is read.

Match the the example below EXACTLY.

 $ ./odd_even_negative
1
2
3
2
-42
Odd numbers were: 1 3
Even numbers were: 2 2
 $ ./odd_even_negative
1
2
4
16
32
64
128
256
512
1024
2048
4096
-8192
Odd numbers were: 1
Even numbers were: 2 4 16 32 64 128 256 512 1024 2048 4096

Revision Exercise: Odd-Even EOF

Write a C program odd_even_eof.c which reads integers from standard input until it reaches end-of-input. It should then print the odd numbers on one line then print the even numbers on one line.

Hint: use odd_even_negative.c as a starting point.

You may assume that the program’s input will contain only integers.

You can assume at most 1000 integers will be read before end-of-input is reached.

Match the the example below EXACTLY.

$ ./odd_even_eof
1
2
4
16
32
64
128
256
512
1024
2048
4096
Ctrl-D
Odd numbers were: 1
Even numbers were: 2 4 16 32 64 128 256 512 1024 2048 4096
$ ./odd_even_eof
3
-3
4
-4
4
Ctrl-D
Odd numbers were: 3 -3
Even numbers were: 4 -4 4

Array Warm-up Exam Questions

The following questions are intended to be similar in style and format to what you would expect for an exam question, but of an easier difficulty.

Warm-up Exam Question: We Need More 17!

We need a function that will put replace every element of an array with the value 17. (Trust us, it’s very important)

You can use the stub code in weNeedSeventeen.c. This .c file contains a main function with a few basic unit tests.

Your function should take two parameters: the length of the array, and the array itself.

It must have this prototype:

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

Your function should not return anything.

For example, if the array contained the following 6 elements: 3, 1, 4, 1, 5, 9 Your function should replace each of those elements with the value 17: 17, 17, 17, 17, 17, 17

Assumptions

You cannot assume anything about the pre-existing values in the array.

However, it shouldn’t matter, since you’re just overwriting everything with the value 17.

Your function should not print anything.

Warm-up Exam Question: We Need More Numbers!

We’ve discovered a fatal flaw in our previous function – what if we need to use a value other than 17??

Write a C function that will fill an array with a specified value, i.e. every array element should become the specified value.

You can use the stub code in fillArray.c. This .c file contains a main function with a few basic unit tests.

Your function should take three parameters: the length of the array, the array itself, and the value to fill the array with.

It must have this prototype:

void fillArray(int size, int array[], int value);

Your function should not return anything.

For example, if value was 42, and the array contained the following 6 elements: 3, 1, 4, 1, 5, 9 Your function should replace each of those elements with the value 42: 42, 42, 42, 42, 42, 42

Assumptions

You cannot assume anything about the pre-existing values in the array.

However, it shouldn’t matter, since you’re just overwriting everything with the specified value.

Your function should not print anything.

Practice Exam Questions

The following questions are intended to be similar to what you would expect for an exam question.

Practice Exam Question: Count Even

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

You can use the stub code in countEven.c. This .c file contains a main function with a few basic unit tests.

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

int countEven(int length, int array[]);

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

You can assume the array contains only positive integers.

Practice Exam Question: Remove Duplicates

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.

You can use the stub code in removeDuplicates.c. This .c file contains a main function with a few basic unit tests.

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 removeDuplicates(int size, int source[], int dest[]);

Your function should return a single integer: the number of elements copied to the dest array.

For example, if the source array contained the following 6 elements: 3, 1, 4, 1, 5, 9

Your function should copy these 5 values to the dest 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

You can assume the array only contains positive integers.

You can assume that the dest 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 not print anything.

Practice Exam Question: Indivisible

Write a C program indivisible.c which reads positive integers that are greater than 1 from standard input until it reaches end-of-input. It should then print the integers read which are not exactly divisible by any other of the integers read.

In other words it should not print an integer if another integer that has been read is a factor of that number.

Hint: use odd_even_eof.c as a starting point.

You may assume that the program’s input will contain only integers.

You may assume that all integers are >1.

You can assume at most 1000 integers will be read before end-of-input is reached.

Match the the example below EXACTLY.

 $ ./indivisible
42
7
6
12
Ctrl-D
Indivisible numbers: 7 6
 $ ./indivisible
2
3
4
5
6
7
8
9
10
Ctrl-D
Indivisible numbers: 2 3 5 7
 $ ./indivisible
5
6
5
Ctrl-D
Indivisible numbers: 6

Strings

String Revision Exercises

The following exercises are intended for revising your skills with strings, and are not intended to be an example of realistic exam questions.

Revision Exercise: Find Character In String

Write a function that searches for a character within a string, and returns the position of the character, or -1 if it was not found. If the character appears multiple times, return the position of its first appearance. Do not call any other functions.

Your function must have the following prototype:

int posInString(char *string, char toFind);

Revision Exercise: Letter Frequency

Write a function to work out how many times a specified character occurs in a string.

Your function should take two parameters: the string, and the character to search for.

Your function should return a single integer: the number of times the specified character appears.

Your function must have this prototype:

int letterFrequency(char *string, char charToFind);

For example, if the character to find was o, and the string was:

"Hello, world!"

Your function should return 2, as the letter o occurs twice in the string (‘o’ at the end of Hello, and ‘o’ in world).

Revision Exercise: Count Vowels

Write a function to work out how many vowels are in a string.

Vowels are the letters ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’, in both uppercase and lowercase.

Your function should take one parameter: the string to search through.

Your function should return a single integer: the number of vowels contained in that string.

Your function must have this prototype:

int countVowels(char *string);

For example, if the string was:

"Hello, world!"

Your function should return 3, as the string contains 3 vowels: ‘e’, ‘o’, and ‘o’.

Revision Exercise: Count Digits

Write a function to work out how many digits are in a string.

Your function should take one parameter: the string to search through.

Your function should return a single integer: the number of digits contained in that string.

Your function must have this prototype:

int countDigits(char *string);

For example, if the string was:

"Password123!"

Your function should return 3, as the string contains 3 digits: ‘1’, ‘2’, and ‘3’.

Revision Exercise: Count Spaces

Write a function to work out how many whitespace characters are in a string.

Whitespace characters are defined for this question as ‘ ‘, ‘\t’ (tab), and ‘\n’ (newline).

Your function should take one parameter: the string to search through.

Your function should return a single integer: the number of whitespace characters contained in that string.

Your function must have this prototype:

int countSpaces(char *string);

For example, if the string was:

"Hello, world!"

Your function should return 1, as the string contains 1 space.

String Practice Exam Questions

The following questions are intended to be similar to what you would expect for an exam question.

Practice Exam Question: Count Emoticon Characters

In an effort to curb the rising tide of emoticons (such as :)) in high school essays, you have been asked to count the number of “emoticon characters” in a string.

An “emoticon character” is defined as any punctuation character (as defined by ispunct()) except , and ..

The characters ‘.’ and ‘,’ are not to be included in the count, because they are usually used for legitimate purposes.

Write a C function that counts the number of punctuation characters in a specified string, excluding commas (,) and full stops (.).

Your function should take one parameter: the string to check. It must have this prototype:

int countEmoticonCharacters(char *string);

Your function should return a single integer: the number of “emoticon characters” (as defined above) in the string.

For example, if the string was:

"Hello, there! :)"

your function should return 3, as the string contains the following four punctuation symbols:

, ! : )

but , is excluded from the total count, as it’s usually used legitimately.

Assumptions

You cannot assume anything about the length or contents of the string.

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

Your function should not print anything.

Practice Exam Question: Count Words

Write a function that counts the number of “words” in a string. For our purposes, a word is a sequence of one or more non-whitespace characters (as defined by the function isSpace() provided).

You can use the stub code in countWords.c. This .c file contains a main function with a few basic unit tests.

For example:

"Hello world", "Hello\nWorld" and " h \n w " all contain two words.

" " contains zero words.

Your function should take one parameter: the string to check. It must have this prototype:

int countWords(char *string);

Your function should return a single integer: the number of words in the string.

For example, if the string was:

"Hello, there! :)"

your function should return 3, as the string contains 3 words: “Hello,”, “there!”, and “:)”.

Assumptions

You cannot assume anything about the length or contents of the string.

Your function should not print anything.