In this assignment, you needed lists of random integers (or - as at least one of you worked out - any integers) for testing. So make a function which constructs such lists (in the simplest way possible of course!), e.g.
int *ConsIntList( int n ) { int *list = (int *)malloc( n*sizeof(int) ); int i; for(i=0;i<n;i++) list[i] = rand(); return list; }
This function can be called as many times as needed to generate test lists of appropriate sizes. Once written and checked, it allows you to concentrate on other aspects of the problem.
Timing is a similar problem: you are presented with the problem that each machine has a slightly different method of telling the time. So build a generic timing function that returns a time to the best accuracy that the current machine will allow, e.g.
double TimeNow() { double t; #ifdef IRIX /* Irix code to calculate t in secs */ #endif #ifdef SUNOS /* Sun code to calculate t in secs */ #endif return t; }Note that this function is double: it is designed to return seconds. This allows it to get the maximum resolution on any machine.
The #ifdef's are for serious programmers who want their code to run without alteration on any machine: each compiler will predefine some suitable string which will allow it to be identified. You will need to check the manuals for individual compilers to find out what the correct values for these strings are.
One of you made a starttime()/stoptime() pair and used a static variable to store the time when the starttime function was called. A good idea, which can be done effectively in a single function:
/* timer.c */ static double last = 0.0; double TimeSinceLastCall() { double t, diff; #ifdef IRIX /* calculate t in secs as above */ .. #endif diff = t - last; last = t; return diff; }This function can be called at the start of a timed section of code (and the return value thrown away) and again at the end of the timed section. The second return value is the running time of the timed section.
This is akin to the "toolbuilding" approach above.
As a consequence, very accurate timing is impossible. If you're expecting a straight line (and you can usually transform your results so that they should be a straight line), a statistician would take the plot and determine the correlation coefficient, which would give a measure of the probability that the results deviate from a straight line (taking into account experimental error). However, you can just run a straight line through your results and if all the points lie close to it and appear randomly on both sides, then it's a straight line. With a little extra effort, you could use your calculator - or get a spreadsheet - to calculate the standard deviation from the mean of the normalised results. This will give you a simple measure of whether your results lie on a straight line or not.
Reporting the slope of the straight line is a nice touch (especially if you add the correlation coefficient - which tells how good a straight line it is!), but your ultimate conclusion should still be O(1) or O(n).
gcc -DNDEBUG *.c
all the assert's will be automagically removed!
The same applies to reports produced by your word processor: export them as plain text without the extra CRs.
Reports which are not plain text will not be accepted - there are a large number of word processors out there: it's much more productive (and therefore beneficial for you!) if the tutors spend time marking your report's content than trying to work out which WP will read it!