Archive for the ‘software engineering’ Category

Happy birthday, Ada Lovelace

12 December 2012 Leave a comment

It’s a bit late here down under. But I’m going to say it anyway: Happy birthday, Ada Lovelace. Here’s a nice synopsis about the first computer programmer.

Best practices for scientific computing

11 December 2012 Leave a comment

This paper is a must read for scientists who write software as part of their research, but are not themselves software engineers. I’m not going to repeat the paper, but list some major points that should be kept in mind when developing scientific software alongside research.

  • Write programs for humans, not computers.
  • Automate as much as possible, especially repetitive tasks.
  • Use the computer to record history.
  • Make incremental changes, with constant revision and evolution.
  • Use a version control tool.
  • Don’t repeat yourself or others. It’s ironic that I’m repeating this message from the paper.
  • Plan for mistakes in your software or its output.
  • Optimize your software after you have it working correctly.
  • Document the design and purpose of your software, not its mechanics.
  • Collaborate with others.

COMP20005 how to time a program

29 August 2012 2 comments

Some students have asked me how to time the running of a program. Essentially, the problem is how do we know the amount of time that a program takes to run. In C, you can use functions from the header file time.h to time your program. The program below shows how to do a simple timing of a program.

#include <stdio.h> 
#include <time.h>

int main(int argc, char **argv) { 
  char name; 
  double howlong; 
  time_t start, end; 

  start = time(NULL); 
  printf("Enter your name: "); 
  scanf("%s", &name); 
  end = time(NULL); 
  howlong = difftime(end, start); 
  printf("You took %.3lf seconds, %s\n", howlong, &name); 

  return 0; 
Categories: COMP20005, programming

COMP20005 workshop 3

14 August 2012 Leave a comment

In this workshop, you will solve problems that require you to use loops. You can use the for loop or the while loop. There’s another type of loop called a do loop, but you could transform a do loop into a while loop. Whenever you have a for loop, think about how to transform it into a while loop; and vice versa.

The assigned exercises for this workshop are exercises 4.1, 4.2, 4.5, and 4.9. Be careful with exercises 4.1(f,g). These exercises illustrate two errors in loops: one is a loop with an empty body and the other is an infinite loop. Exercise 4.2 asks you to think about how to transform a do loop into a for loop. This should be easy if you know how to transform a while loop into a for loop. In exercise 4.5, you need to read in a sequence of positive integers and plot the integers as a histogram. Think about how you would handle the case of an integer with one digit and the case of an integer with multiple digits. Exercise 4.9 is a little bit challenging and requires you to know a little bit about prime numbers. The basic idea of the exercise is that your program should read in a positive integer and find the next prime number after the integer. All prime numbers greater than 2 are odd, so you can limit your search to only the odd integers.

COMP20005 workshop 2

12 August 2012 Leave a comment

The basic idea of workshop 2 is for you to practice writing programs that use conditional. By “conditional” I mean the ifelse construct. The assigned exercises for this workshop are:

  • Exercise 3.2, pages 40–41
  • Excerise 3.6, page 43
  • Exericse 3.7, page 43

You should be very careful with exercises 3.2(d) and 3.2(e). Here are some hints. The formatting in exercise 3.2(d) is misleading and should serve to remind you to properly indent your code and use curly braces. In exercise 3.2(e), the semicolon that follows a conditional can make the following block of code not part of the if construct. In other words, something like

if (<condition or guard>) ;


if (<condition or guard>) 

is valid C code. In this case, the whole if block is comprised of the guard and the semicolon. Everything after the semicolon do not belong to the if block.

Exercise 3.6 asks you to determine the change for a given number of cents that is at most 99. You are only allowed to use the denominations of 50, 20, 10, 5, 2, and 1 cents. You should solve this exercise by using only conditionals. So concepts like loops and recursion should not be used to solve this exercise. One way to solve the exercise via conditionals only is to take each denomination from largest to smallest. Think about the maximum number of coins of a given denomination that can go into the number of cents you have. Then write a number of if statements to get the required change. Another way to solve the exericse is to use the quotient and remainder. One of these two methods require less code than the other. Try out both methods and see for yourself.

COMP20005 workshop 1

28 July 2012 5 comments

Learning materials

Welcome to the first workshop for the subject COMP20005 Engineering Computation. The materials for the subject are: the textbook Programming, Problem Solving, and Abstraction with C by Alistair Moffat, and the lecture notes. With a little searching, you should be able to find lots of online introductory materials on C programming. Here are some:

  • The C Book — an introductory textbook on C programming.
  • — a reference on the C library. This has lots of sample code that demonstrate each function in the C standard library.
  • Fundamentals of C Programming — a set of video tutorials that are aimed at beginners.
  • C Programming Tutorials — another set of video tutorials for beginners to the C language.
    • Agenda

      In workshop 1, we will cover the following topics:

      • Getting to know each other and how you can contact your tutors.
      • Where and how to get help.
      • Be familiar with the software environment that we will use in this subject. We will use jEdit for writing up our programs and MinGW to compile our programs.
      • Step through the process of writing a program, compile the program, run the program, and debug the program.
      • Exercises 1.3 and 2.6 in the textbook.

      Compile your program

      When you go through the write, compile, run, and debug loop, you will most likely have to compile your program several times. For example, if your source file is called hello.c, you would compile it from the command line by using the following incantation:

      gcc -Wall -ansi -o hello hello.c

      If you do this several times, it can get boring and tedious. To speed up the compilation process, you can write a little program called a shell script. Under Windows, it’s called a batch file, but under Linux and Mac OS X it’s called a shell script. Assuming that you’re using Windows, the batch file would take the name of your source file, but without the extension “.c”, and compile your source file. To create the batch file, create a file with the name compile.bat. Put the following content into the batch file:

      gcc -Wall -ansi -o %1 %1.c

      Note that %1 is the percentage symbol followed by the number one. Make sure that your batch and source files are in the same directory. From the command line, you would compile your program as follows:

      compile.bat hello

      Notice that the only argument for your batch file is the name of your source file, but without the extension “.c”. You should then be able to run your program (under Windows) by issuing the command


      Getting help

      I will hold office hours each Thursday from 12pm to 2pm in Doug McDonell building 168, room 6.13. Other times are by appointment only. Remember that if you contact your lecturer or tutors via email, make sure that you use the subject prefix “[COMP20005]”. Otherwise, we will not pick up your email.

Primality test in Haskell

14 March 2012 Leave a comment

Problem: Implement a primality test in Haskell. Below is the code. The first implementation of the function divides uses a homemade function for finding the remainder when an integer is divided by another integer.

-- The remainder when a is divided by b.                                        
remainder :: Integer -> Integer -> Integer
remainder a b | a < b = a
              | a == b = 0
              | otherwise = remainder (a - b) b

-- Whether d divides n.                                                         
divides :: Integer -> Integer -> Bool                                        
divides d n = remainder n d == 0

The second implementation of divides uses the built-in function rem to find the remainder upon division by an integer.

-- Whether d divides n.  A more efficient version that uses the built-in        
-- function rem.                                                                
divides :: Integer -> Integer -> Bool
divides d n = rem n d == 0

The full primality test follows:

-- Whether d divides n.  A more efficient version that uses the built-in        
-- function rem.                                                                
divides :: Integer -> Integer -> Bool
divides d n = rem n d == 0

-- The least divisor of n that is at least k.                                   
ldf :: Integer -> Integer -> Integer
ldf k n | divides k n = k
        | k^2 > n = n
        | otherwise = ldf (k + 1) n

-- The least divisor of n.                                                      
ld :: Integer -> Integer
ld n = ldf 2 n

-- Primality test.                                                              
prime :: Integer -> Bool
prime n | n < 1 = error "must be a positive integer"
        | n == 1 = False
        | otherwise = ld n == n