Archive for the ‘programming’ 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.

COMP20005 how to time a program

29 August 2012 1 comment

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.