Archive

Archive for the ‘COMP20005’ 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);
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

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

The basic idea of workshop 2 is for you to practice writing programs that use conditional. By “conditional” I mean the `if``else` 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.

COMP20005 workshop 1

28 July 2012 1 comment

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.
• cplusplus.com — 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.

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

```hello
```

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.