============================================================================ Faculty of Applied Science and Engineering, University of Toronto CSC181: Introduction to Computer Programming, Fall 2000 Practical 2: Writing Functions ============================================================================ The purpose of this practical is to get you writing functions. You will not be graded for this practical. Your Tasks 1. Redo the exercises from the tutorial notes for week 2, but this time write the programs as functions. Test your functions with a small 'main' method. For example, question 4 can be redone as follows: Write a function that takes as arguments an array of integers and the size of the array, and returns true (non-zero) if the array contains at least one negative integer, false (zero) otherwise. 2. Write a function 'bringMinimumToFront', which given an array of ints, and the size of the array, swaps the minimum element in the array with the first element of the array. For example, if we have the following array: +---+---+---+---+---+ a | 3 | 5 | 9 | 2 | 8 | +---+---+---+---+---+ 0 1 2 3 4 Then a call to bringMinimumToFront should change the array to this: +---+---+---+---+---+ a | 2 | 5 | 9 | 3 | 8 | +---+---+---+---+---+ 0 1 2 3 4 Note that a[0] and a[3] were swapped, since a[3] was 2, the minimum element in the array. 3. Write a function 'printLetterFrequencies', which given a char array, prints out how many times each letter from 'a' to 'z', appears. For example, if we call printLetterFrequencies with the char array "hello!", the function should output the following: e 1 h 1 l 2 o 1 As it turns out, you don't need to pass the length of the char array as an argument to the function. You can use the function 'strlen' to determine the length of the string. For example, strlen("hello!") returns 6. (It doesn't include the null character, '\0'.) 4. For data compression and other purposes, we often need to look for runs of repeated characters in data. For example, in the following string there is a run of 4 b's, a run of 2 c's, and a run of 5 f's. abbbbccdefffffg To be exact, we also consider that the a, the d, the e and the g are all runs of length 1. We can encode a string into a compressed version by representing each run as a pair of characters. The first character of a pair is a letter and the second character is the length of the run for that letter. For example, the above string would be encoded as follows: a1b4c2d1e1f5g1 Write a function 'encode', which given a char array containing a string of letters, and another array which is blank, encodes it in the manner described above, and stores the results in the blank array. For example, encode("abbbbccdefffffg", a), should store in 'a' the string "a1b4c2d1e1f5g1". Hint: For this task, you will probably need the functions 'sprintf' and 'strcat'. Refer to your C textbook for information on how to use these functions.