Main Contents Page
CTEC1401 Programming in C
Quick Reference
Week 15: Strings
Week 16: Functions I
Week 17: Functions II
  1. Before the lab session read CFTTP Chapter 11.

  2. perfect

    Write a program, perfect.c, that

    1. takes one integer parameter p
    2. checks whether p is perfect by adding up all the (positive) numbers less than p which divide it exactly
    3. returns 1 if p is perfect and 0 otherwise

    Create perfect.c by adding a main() function that computes all the perfect numbers less than 64,000. Compile and test your program.

  3. fun1

    Create a text file called fun1.c that contains the following C program:

    #include <stdio.h>
    
    int sqr (int x)
    {
        return x*x;
    }
    
    int main()
    {
        int k;
        for (k=0; k<10; k++)
            printf("sqr(%i) = %3i\n",k, sqr(k));
        return 0;
    }
    
    1. The program uses a function to calculate the square of an (integer) number. When the program is run it prints out a table of squares of the numbers from zero to nine. Save the file and then compile and run the program.
    2. Modify the program so that it prints out the squares of the numbers from zero to 99.
  4. fun2

    In a similar way to the program fun1 write a program, fun2.c, that prints a table of the numbers from zero to 31 along with their cubes. You should write and use a function called cube that calculates the cubes of integer numbers.

  5. fun3

    In a similar way to the program fun1 write a program, fun3.c, that prints a table of the numbers starting at zero along with their squares, cubes, and fourth powers. The table will therefore begin like this:

    0       0       0       0
    1       1       1       1
    2       4       8      16
    3       9      27      81
    

    To compute the fourth-powers you should use the sqr function (twice) - do not write a new fourth-power function. You should extend the table until the results become unreliable. (The previous question has already demonstrated how this can happen.)

  6. gcd

    The greatest common divisor of two integer numbers is the largest integer that can be divided exactly into both of them. Note, the answer may be one of the numbers themselves if the other is a multiple of it. For example, the greatest common divisor of 32 and 12 is 4 whereas the greatest common divisor of 36 and 12 is 12 itself. An algorithm for calculating the greatest common divisor of two numbers (x and y) is given here:

    Repeat
          While x is less than y,
                subtract x from y
          While y is less than x,
                subtract y from x
    Until x is equal to y
    Return x  (or y, since they are the same)
    
    Write a program, gcd.c, that contains a function gcd that takes two integer arguments and returns their greatest common divisor. The program should ask the user to enter two integers and then print out the greatest common divisor.

  7. calc

    Create a text file called calc.c that contains the following C program:

    #include <stdio.h>
    
    float add (float x, float y)
    {
        return x + y;
    }
    
    int main()
    {
        float a, b, r;
        char op;
        do {
           printf("number  op  number?  ");
           scanf(" %f %c %f", &a, &op, &b);
           switch (op)
           {
               case '+' : r = add(a,b);
                          break;
               case 'q' : break;
               default  : op='?';
           }
           if (op=='?')
              printf("Unknown operator\n");
           else if (op=='q')
              printf("Bye\n");
           else
              printf("%f %c %f = %f\n", a, op, b, r);
        }
        while (op != 'q');
        
        return 0;
    }
    
    1. The program behaves like a calculator asking the user to type in binary expressions (like 2.5 + 3.7) and printing out the results. If the user enters 'q' for the operator (e.g. 0 q 0) then this is taken as a signal to stop the program.
    2. Add a new function to the program that multiplies its two arguments together and returns their product. Then modify the switch statement so that the operator '*' causes this new product function to be called. Test the program by using a mixture of '+' and '*' calculations.
    3. Add operators for subtraction ('-') and division ('/') and test your program again.
    4. We introduce some special operators. Write functions to implement each of these (adding one at a time and testing after each new operator is incorporated into your calculator).
      Symbol    Description    Example
      m Minimum 2.3 m 1.7 = 1.7
      M Maximum 2.3 M 1.7 = 2.3
  8. Supplementary Exercises

  9. charlib

    It is useful to have a library of functions that operate on single characters. For example, to convert a lower case alphabetic character into its upper case counterpart; or to see if a character represents a decimal digit or not. These are just two examples, but there are many more. This exercise requires you to build up a library of character functions along with a test program that demonstrates them working - call your program charlib.c.

    This exercise, like the previous calc program, requires you to adopt an incremental development approach in which you add a function and then test it thorouhgly; add the next function and extend your main program to test the new function; etc. At each stage you add more test code to your main program without removing earlier tests - this will ensure that you have not broken anything that previously worked during your development.

    The functions that your library should contain are these - we provide the function headers.

    HeaderDescription
    int isLowercase(char c) Returns true (1) if c is a lower case alphabetic character, otherwise 0
    int isUppercase(char c) Returns true (1) if c is an upper case alphabetic character, otherwise 0
    int isDigit(char c) Returns true (1) if c is a digit character ('0', '1', ... '9'), otherwise 0
    int isWhitespace(char c) Returns true (1) if c is a whitespace character (i.e. space, tab, newline), otherwise 0
    int isAlpha(char c) Returns true (1) if c is an alphabetic character (upper or lower case), otherwise 0
    int isAlphanum(char c) Returns true (1) if c is an alphanumeric character (i.e. alphabetic or digit), otherwise 0
    int isPrintable(char c) Returns true (1) if c is printable, otherwise 0
    char toLowercase(char c) If c is an uppercase char then returns its lowercase counterpart, otherwise returns c unchanged
    char toUppercase(char c) If c is a lowercase char then returns its uppercase counterpart, otherwise returns c unchanged
    char nextAlpha(char c) Returns the next alphabetic character in sequence. If c=='z' then returns 'a' so it "wraps around". Similarly, if c=='Z' then returns 'A'.
    char prevAlpha(char c) Returns the previous alphabetic character in sequence. If c=='a' then returns 'z' so it "wraps around". Similarly, if c=='A' then returns 'Z'.
    char nextAlphaN(char c, int N) Returns the next/previous alphabetic character in sequence using N as the offset value. If N is positive then the direction is "next", if N is negative then the direction is "prev". The function should "wrap around" so that nextAlphaN('a', -3) would return 'x'. (Note, if N==1 then the function behaves like nextAlpha and if N== −1 then the function behaves like prevAlpha.)