html>C-ohjelmointi, laskuharj. 1

C-programming Fall 1999

Problems for Exercise 3 (4.-8.10.1999)

Write your C-programs from the very beginning with a computer. Points will be given only for solutions proven by a computer. Make sure of the correct functioning by compiling your program, correcting possible errors and by running program, if necessary using different test inputs. Avoid using, in your solutions, such features that have not been explained in the course.

1. Write, without using library functions, a funtion that sorts the elements of an integer array in acsensing order.

2. Write a function that counts the frequencies in procents of the letters 'a' ... 'z' in the input. Here you can use suitable standard library functions. What problems do the letters ä, å, and ö cause? How could you also count the frequencies in procents of these letters?

3. Write a recursive function to calculate the greatest common divisor of two integers (gcd). Notice that gcd(a,b) = gcd(b, a % b), if a > 0 and b > 0. (% means the modulos operator.) Ps. You should stop the recursion when a%b is 0 and return the value of b as gcd. Eg. gcd(4,2) = gcd(2,4%2), when a%b that is 4%2 == 0, => gcd = 2 (=b). If either a or b is 0, then gcd = 0: gcd(0,b) => gcd = 0 and gcd(a,0) => gcd = 0.

4. Write a function that tells if a string given is a palindrome or not. A palindrome is a string that is the same whether you read it normally or in reverse order, for example "saippuakauppias" (soap seller). Spaces do not matter in palindromes. So also 'innostunut sonni' (eager bull) and 'sinä ja jänis' (You and a hare) are regarded palindromes.

5. Write a program that prints the frequencies of word lengths of the input. As output could be given something like this "length 1: 30 occurences". At the end of a word there is either a space or a newline. You can expect that every line ends to a newline. You can assume that words have a maximun lenght of 80 character. The lines can be of any length.

6. Write a .c source file deck.c that allows you to handle a regular deck of cards (four suits, 13 cards/suit). Write at least the following functions:

   void suffle(int deck[],int cards);   /* suffles a deck */
   void deal(int deck[], int hand[], int n); /* gives the next n cards 
                                             from the deck to a hand */

To get random numbers use the standard library functions
   void srand(unsigned int_seed)   gives a new seed number for the rand
                                      random numbers
   int rand (void)                  returns a random number between
                                      0-RAND_MAX. RAND_MAX  is at least 32767.
  For example:
   srand(time(NULL)); /*time (NULL) gives the new seed for rand() */ 
   rand()%(n+1)   gives a random number between 0 .. n
   rand()%n+1     gives a random number between 1 .. n
                                                              
Hint: A deck of cards has four suits, each of which has 13 cards. You can describe a card as an int by deciding that values 0-12 are spades, 13-25 are hearts, 26-38 are diamonds, and 39-51 are clubs. After that the suit if a card is n/13, the value of a card is n%13, where n is the integer value of the card.