Week 2 Laboratory — C Refresher

It is suggested that you make a directory (folder) where you can put all of your lab code. Within that directory, make subdirectories for each lab. Change into the subdirectory for this lab and do the rest of the lab there.

In case you've forgotten your Linux commands:

We have created some scripts that can automatically run your program against some tests. To run these tests you can execute the dry run program with an argument that corresponds to the lab and week, i.e. lab02 for this week. It expects to find all the programs to be submitted as part of this lab in the current directory. You can use dry run as follows:

prompt$ ~cs1921/bin/dryrun lab02

or specific tests (e.g. test #2) as follows:

prompt$ ~cs1921/bin/dryrun lab02 2

Exercises

Ensure that all your programs follow the Style Guide.

  1. (Loops) Write a program called collatz.c (named after the mathematician who invented this problem) that asks for a positive integer n, checks that it is greater than 0 and then outputs the series of numbers according to the following computation until 1 is reached:

    Examples of the program executing are:

    Enter a positive number: 8
    8
    4
    2
    1
    

    Enter a positive number: 17
    17
    52
    26
    13
    40
    20
    10
    5
    16
    8
    4
    2
    1
    

    If the number is not positive, then the output should be:

    Enter a positive number: 0
    Not a positive number.
    

    Enter a positive number: abc
    Not a positive number.
    

  2. (Arrays) Write a program called fibonacci.c that computes an array with the first 30 Fibonacci numbers and then outputs these numbers. The Fibonacci numbers are defined as follows:

    F0 = 1

    F1 = 1

    Fn = Fn-1 + Fn-2 for n ≥ 2

    The first 11 lines of the output of the program should be:

    fib[0] = 1
    fib[1] = 1
    fib[2] = 2
    fib[3] = 3
    fib[4] = 5
    fib[5] = 8
    fib[6] = 13
    fib[7] = 21
    fib[8] = 34
    fib[9] = 55
    fib[10] = 89
    

    You should match the above format exactly.

  3. (Strings) Write a program called string.c that asks for a string, replaces all occurrences of the letter 'A' by the letter 'T' and vice versa, and then outputs the modified string.

    Examples of the program executing are:

    Enter a string: ATGCTTCGGCAA
    TAGCAACGGCTT
    

    Enter a string: TAO tag
    ATO tag
    

    Enter a string: 12345
    12345
    

    You can assume that input lines have at most 127 characters.

  4. (Files) Extend your program from the previous exercise to a program called file.c that reads a file yeast.dna line-by-line, replaces every 'A' by 'T' and vice versa, and outputs the modified strings on the screen.

    Download the file yeast.dna to test your program. The output should begin with

    TAGACTGTCCCATACTGTTCGTTTCCCTTGACGACGTAGCTTTACGTCTTCGCCCCATCG
    CCACTCTTATCCCCTGCTACGGAACAGTTACCGTGCATAAAGTTTTTCGGCTTCCCTGAA
    TGGGCCCTGGCTCTGGTGCTGGTAGTCTTGTACGGCTCCTACTTCGTGGTGGTCTACAAG
    

    Again you can assume that input lines have at most 127 characters.

  5. (Pointers) Write a function that takes as parameters two pointers to floats and returns a pointer to the float with the greater value. Write a program called floats.c that asks for two floating point numbers and uses the function to display the greater of the two.

    Examples of the program executing are:

    Enter a number: 3.141593
    Enter a number: 2.718282
    The greater number is 3.141593
    

    Enter a number: -3.141593
    Enter a number: 2.718282
    The greater number is 2.718282
    

    Enter a number: 100
    Enter a number: 100
    The greater number is 100.000000
    

    You should match the above format exactly.

In general, each file or program that is submitted in each lab in the course is worth 2 marks. The sum of the marks you get for the lab submissions is scaled to a mark out of 10, and this contributes 10% to your final course mark.

For this lab, you should submit your files using the following give command (but the command may not work until the lab itself):


  give  cs1921  lab02  collatz.c  fibonacci.c  string.c  file.c  floats.c

Make sure you spell the filenames correctly.