Archive

Archive for the ‘education’ Category

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.

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>) ;

or

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.