SE250:lab-3:jham005:lab-3.c
Jump to navigation
Jump to search
/* File: lab-3.c Date: 18 March 2008 Author: John Hamer Purpose: Sample solution to SOFTENG 250 lab #3 The code uses a modified version of arraylist.c that allows the ArrayList growth strategy to be customised by setting three parameters: ARRAYLIST_GROWTH_FACTOR ARRAYLIST_MIN_ALLOC ARRAYLIST_GROWTH_INCR The output is a script for the statistical package R. */ #include <stdio.h> #include <time.h> #include <math.h> #include "arraylist.h" /* Set R_EOF to C-z for R running on Windows, or C-d for R running on Unix */ char R_EOF = 'Z' - 'A' + 1; /* Don't attempt further experiments after one takes over 5 seconds */ int MAX_DELAY = CLOCKS_PER_SEC * 5; char INSERT_BACK[] = "back"; char INSERT_FRONT[] = "front"; void Rplot( int plot, int* times, double gfactor, int incr, int minalloc, char* pos, char* colour, char pch ) { int t; ARRAYLIST_GROWTH_FACTOR = gfactor; ARRAYLIST_MIN_ALLOC = minalloc; ARRAYLIST_GROWTH_INCR = incr; printf( "leg.txt = c(leg.txt, \"%3.1f+%d[%d], %s\")\n", gfactor, incr, minalloc, pos ); printf( "leg.pch = c(leg.pch, %d)\n", (int)pch ); printf( "leg.col = c(leg.col, %s)\n", colour ); printf( "data <- read.table( stdin( ) )\n" ); for( t = 0; times[ t ] != 0; t++ ) { ArrayList xs; long i; clock_t t0, diff; arraylist_init( &xs ); if( gfactor == infinity( ) ) ensure_capacity( &xs, times[ t ] ); t0 = clock( ); if( pos == INSERT_BACK ) for( i = 0; i < times[ t ]; i++ ) arraylist_push( &xs, 0 ); else for( i = 0; i < times[ t ]; i++ ) arraylist_put( &xs, 0, 0 ); diff = clock( ) - t0; printf( "%d %ld\n", times[ t ]/1000, diff ); arraylist_clear( &xs ); if( diff > MAX_DELAY ) break; } printf( "%c\n", R_EOF ); if( plot == 0 ) /* The first plot determines the limits of the time axis */ printf( "\nplot( data, type=\"o\", pch=%d, col=%s, xlab=\"Elements added (000s)\", ylab=\"Time (/1000s)\" )\n", pch, colour ); else printf( "\npoints( data, type=\"o\", pch=%d, col=%s )\n", pch, colour ); } int main( ) { double gf[] = { 1.1, 1.2, 1.3, 1.4, 1.5, 2.0, 2.5, 3.0, 4.0, infinity( ), 0 }; int times[] = { 100, 1000, 2000, 5000, 10000, 100000, 1000000, 2000000, 4000000, 8000000, 0 }; //int times[] = { 10000, 12000, 14000, 16000, 18000, 20000, 25000, 30000, 40000, 50000, 0 }; //double gf[] = { 0 }; int g; ARRAYLIST_ALWAYS_MALLOC = 1; /* see comments in arraylist.c:ensure_capacity */ printf( "leg.txt = c()\nleg.pch = c()\nleg.col = c()\n" ); for( g = 0; gf[ g ] != 0; g++ ) Rplot( g, times, gf[ g ], 0, 16, INSERT_BACK, "\"black\"", 'a' + g ); printf( "misc.cols=rainbow(4)\n" ); Rplot( g++, times, 2.0, 0, 16, INSERT_FRONT, "misc.cols[1]", 22 ); Rplot( g++, times, 1.0, 1, 16, INSERT_BACK, "misc.cols[2]", 21 ); Rplot( g++, times, 2.0, 0, 1024, INSERT_BACK, "misc.cols[3]", 19 ); Rplot( g++, times, 1.0, 1000, 16, INSERT_BACK, "misc.cols[4]", 20 ); printf( "title(main=\"Insert into an ArrayList\\nfactor+incr[min], position\")\n" ); printf( "legend( \"topleft\", legend=leg.txt, pch=leg.pch, col=leg.col )\n" ); return 0; }