C prgramming examples Prf. Gustav Alns Cmputer Science Department ETH Zürich alns@inf.ethz.ch http://www.inf.ethz.ch/department/is/iks/
Prgramming examples in C Dynamic queue Use f timing functins Generating randm numbers Arguments and main Use f static Cunting bits Prgramming examples taken frm the n-line bk: Prgramming in C UNIX System Calls and Subrutines using C http://www.cs.cf.ac.uk/dave/c/ce.html Gustav Alns, ETH Zürich. C prgramming examples 2
/* */ /* queue.c */ /* Dem f dynamic data structures in C */ #include <stdi.h> #define FALSE 0 #define NULL 0 typedef struct { int dataitem; struct listelement *link; listelement; Gustav Alns, ETH Zürich. C prgramming examples 3 d { vid Menu (int *chice); listelement * AddItem (listelement * listpinter, int data); listelement * RemveItem (listelement * listpinter); vid PrintQueue (listelement * listpinter); vid ClearQueue (listelement * listpinter); main () { listelement listmember, *listpinter; int data, chice; listpinter = NULL; Menu (&chice); switch (chice) { case 1: printf ("Enter data item value t ad scanf ("%d", &data); listpinter = AddItem (listpinter, d break; case 2: if (listpinter == NULL) printf ("Queue empty!\n"); else listpinter = RemveItem (listp break; case 3: PrintQueue (listpinter); break; case 4: break; default: printf ("Invalid menu chice - try ag break; while (chice!= 4); ClearQueue (listpinter); exit (0); /* main */
vid Menu (int *chice) { char lcal; printf ("\nenter\t1 t add item,\n\t2 t remve item\n\ \t3 t print queue\n\t4 t quit\n"); d { lcal = getchar (); if ((isdigit (lcal) == FALSE) && (lcal!= '\n')) { printf ("\nyu must enter an integer.\n"); printf ("Enter 1 t add, 2 t remve, 3 t print, 4 t quit\n"); while (isdigit ((unsigned char) lcal) == FALSE); *chice = (int) lcal - '0'; Gustav Alns, ETH Zürich. C prgramming examples 4
listelement * AddItem (listelement * listpinter, int data) { listelement * lp = listpinter; if (listpinter!= NULL) { while (listpinter -> link!= NULL) listpinter = listpinter -> link; listpinter -> link = (struct listelement *) mallc (sizef (listelement)); listpinter = listpinter -> link; listpinter -> link = NULL; listpinter -> dataitem = data; return lp; else { listpinter = (struct listelement *) mallc (sizef (listelement)); listpinter -> link = NULL; listpinter -> dataitem = data; return listpinter; Gustav Alns, ETH Zürich. C prgramming examples 5
listelement * RemveItem (listelement * listpinter) { listelement * tempp; printf ("Element remved is %d\n", listpinter -> dataitem); tempp = listpinter -> link; free (listpinter); return tempp; vid PrintQueue (listelement * listpinter) { if (listpinter == NULL) printf ("queue is empty!\n"); else while (listpinter!= NULL) { printf ("%d\t", listpinter -> dataitem); listpinter = listpinter -> link; printf ("\n"); vid ClearQueue (listelement * listpinter while (listpinter!= NULL) { listpinter = RemveItem (listpinter Gustav Alns, ETH Zürich. C prgramming examples 6
Use f timing functins /* timer.c */ /* Cmputes the time in secnds t d a cmputatin */ #include <stdi.h> #include <sys/types.h> #include <time.h> main() { int i; time_t t1,t2; (vid) time(&t1); fr (i=1;i<=300;++i) printf(``%d %d %dn'',i, i*i, i*i*i); (vid) time(&t2); printf(``n Time t d 300 squares and cubes= %d secndsn'', (int) t2-t1); Gustav Alns, ETH Zürich. C prgramming examples 7
Generating randm numbers /* randm.c */ #include <stdi.h> #include <sys/types.h> #include <time.h> main() { int i; time_t t1; (vid) time(&t1); srand48((lng) t1); /* use time in secnds t set seed */ printf( 5 randm numbers (Seed = %d):n,(int) t1); fr (i=0;i<5;++i) printf( %d, lrand48()); printf( \n\n ); /* flush print buffer */ Gustav Alns, ETH Zürich. C prgramming examples 8
On randm number generatin in C lrand48() returns nn-negative lng integers unifrmly distributed ver the interval (0, 2**31). A similar functin drand48() returns duble precisin numbers in the range [0.0,1.0). srand48() sets the seed fr these randm number generatrs. It is imprtant t have different seeds when we call the functins therwise the same set f pseud-randm numbers will generated. time() always prvides a unique seed. Gustav Alns, ETH Zürich. C prgramming examples 9
Arguments and main #include <stdi.h> main(int argc, char **argv) { /* prgram t print arguments frm cmmand line */ int i; printf("argc = %d\n\n",argc); fr (i=0;i<argc;++i) printf("argv[%d]: %s\n",i, argv[i]); Gustav Alns, ETH Zürich. C prgramming examples 10
Use f static #include <stdi.h> vid stat(); main() { int cunter; /* lp cunter */ fr (cunter = 0; cunter < 5; cunter++) { stat(); vid stat() { int temprary = 1; static int permanent = 1; (vid)printf("temprary %d Permanent %d\n", temprary, permanent); temprary++; permanent++; Gustav Alns, ETH Zürich. C prgramming examples 11
Cunting bits int bitcunt(unsigned char x) { int cunt; fr (cunt=0; x!= 0; x>>=1) if ( x & 01) cunt++; return cunt; Gustav Alns, ETH Zürich. C prgramming examples 12