Lesson of 13 July 2009¶
Some simple C programs
Fibonacci serie, recursive and non recursive implementation
File fibonacci.cc
1/*
2 A simple implementation of the calculation of Fibonacci numbers.
3
4 F[0] = 0 ;
5 F[1] = 1 ;
6 F[k+1] = F[k] + F[k-1] ;
7
8 */
9
10#include <stdio.h> // use C stile I/O
11
12// non recursive implementation
13
14int // return an integer
15fibonacci // name of the function
16 ( // arguments of the function
17 int k // request of the k-th Fibonacci number
18 )
19{ // begin of the body of the function
20 int fk ; // k-th Fibonacci number
21 int fkp1 ; // k+1 Fibonacci number
22 int fkm1 ; // k-1 Fibonacci number
23
24 switch ( k ) {
25 case 0:
26 // do not need return 1 here because there is return 1
27 // for case 1:
28 case 1:
29 return 1 ;
30 // break; is not necessary because return exit from switch
31 default:
32 // normal case, k>1. I can modify k because k is passed "by value".
33 // In FORTRAN arguments are passed "by reference".
34
35 // initialization, setup fk, fkp1 and fkm1 for k=1
36 fkm1 = 1 ;
37 fk = 1 ;
38 fkp1 = 2 ;
39
40 // update fk
41 while ( k-- > 1 ) { // while ( k-- > 2 ) can be interpreted
42 // as while ( k > 2 ) whith k decremented
43 // after the comparison
44 fkp1 = fk + fkm1 ; // new value ;
45
46 // shift new value
47 fkm1 = fk ;
48 fk = fkp1 ;
49 } ;
50 return fkp1 ;
51 }
52
53} // end of the body of the function
54
55
56// Recursive implementation
57
58int
59fibonacciR( int k ) {
60 printf("Entering Fibonacci %d\n",k) ;
61 switch ( k ) {
62 case 0:
63 case 1:
64 return 1 ;
65 default:
66 return fibonacciR(k-1)+fibonacciR(k-2) ;
67 }
68 printf("Exiting Fibonacci %d\n",k) ;
69}
70
71
72int
73main() {
74 // check fibonacci function
75 int f10 = fibonacci( 10 ) ;
76 int f10R = fibonacciR( 10 ) ;
77
78 printf("fibonacci(10) = %d\n", f10 ) ;
79 printf("fibonacciR(10) = %d\n", f10R ) ;
80
81}
QuickSort algorithm of O’Hara, recursive implementation on integer C vector
File quicksort.cc
1/*
2 Algorithm quick sort
3 */
4
5#include <iostream> // use I/O of C++
6
7using namespace std ; // load the functions in standard namespace
8
9/*
10 Sort a verctor of integer by quick sort algorithm
11 (John O'Hara algorithm)
12 */
13void
14quickSort (int a[], int lo, int hi, int level) {
15 // a[]: a is a pointer to the vector,
16 // to access the element use a[0] = the first element
17 // a[1] the second element, a[k] the k+1 element.
18 //
19 // lo is the lower index, hi is the upper index
20 // of the region of array a that is to be sorted
21 //
22 // level = level of recursion
23 for ( int kk = 0 ; kk < level ; ++kk ) cout << " " ;
24 cout << "[" << lo << ", " << hi << "]\n" ;
25
26 // sort only if we have at least 2 element
27 if ( hi <= lo ) return ;
28
29 int i = lo ; // i is set to the beginning of the region
30 int j = hi ; // j is set to the end of the region
31 int pivot = a[(lo+hi)/2] ; // (lo+hi)/2 the pivot is chosen
32 // in the middle of the vector
33 // but any index between lo and hi
34 // can be used.
35 // (lo+hi)/2 is truncated to
36 // the lower near integer
37 // partition
38 while ( j > i ) { // if i==j only one elemet: the region is sorted
39 // if i < j empty region, nothing to sort
40
41 while ( a[i] < pivot ) i++ ; // a[ii] for ii <= i are less than pivot
42 while ( a[j] > pivot ) j-- ; // a[jj] for jj >= j are greater than pivot
43
44 if ( i <= j ) { // check if the sub-regions are complete.
45 // if not, we need to exchange the elements outside the
46 // sub-regions
47 int h=a[i] ; a[i]=a[j]; a[j]=h ; // exchange a[i] with a[j
48 i++; j--; // a[i] and a[j] are in the correct region
49 // so that we do not need to check again
50 }
51 } ;
52
53 // recursion onfy if region is not empty
54 quickSort(a, lo, j, level+1);
55 quickSort(a, i, hi, level+1);
56}
57
58int
59main() {
60 int vec[100] ; // declare and instance a vector of 100 elements
61
62 // initialize some elements
63 vec[0] = 9 ;
64 vec[1] = 1 ;
65 vec[2] = -1 ;
66 vec[3] = 2 ;
67 vec[4] = 2 ;
68 vec[5] = 6 ;
69 vec[6] = 5 ;
70 vec[7] = -23 ;
71 vec[8] = -2 ;
72 vec[9] = 1 ;
73
74 quickSort( vec, 0, 9, 0 ) ;
75
76 // cout = character out object
77 for ( int i = 0 ; i < 10 ; ++i )
78 cout << i << " " << vec[i] << '\n' ;
79
80 // define and initialize a vector
81 int b[] = { 1, 2, -3, 0, -45, -1, 0, 3, 67} ;
82
83 // evaluate the size of vector b.
84 // sizeof return the size in byte of the element in argument
85 // sizeof(b) return the number of bytes used by the whole vector
86 // sizeof(b[0]) return the number of bytes used by and element of the vector
87 int szb = sizeof(b)/sizeof(b[0]) ;
88
89 quickSort( b, 0, szb-1, 0 ) ;
90
91 //cout = character out object
92 for ( int i = 0 ; i < szb ; ++i )
93 cout << i << " " << b[i] << '\n' ;
94
95 return 0 ;
96}