diff --git a/ex2/ex2.1/bubble_sort_int.cpp b/ex2/ex2.1/bubble_sort_int.cpp new file mode 100644 index 0000000..0c83ce5 --- /dev/null +++ b/ex2/ex2.1/bubble_sort_int.cpp @@ -0,0 +1,90 @@ +/* + * Bubble sort a list of 10 integers + * + * I'd rather have the method without pointers. + * As a user of the function, I don't have to think about passing pointers, which is also nice + */ +#include + + +#define LIST_SIZE 10 +#define USE_PTR 1 + +void sort( int* A, const int size ); +#if !USE_PTR +void order( int& a, int& b ); +#else +void orderptr( int* a, int* b ); +#endif + + +int main() { + int list[LIST_SIZE] = { 2295, 14500, 9127, 23079, 20612, 5571, 21959, 10032, 18339, 16673 }; + + // Print the original + std::cout << "Original: "; + + for( int i = 0; i < LIST_SIZE ; i++ ) + { + if ( i != 0 ) + { + std::cout << ", "; + } + + std::cout << list[i]; + } + std::cout << std::endl; + + // sort it + sort( list, LIST_SIZE ); + + // Print it + std::cout << "Sorted: "; + for( int i = 0; i < LIST_SIZE ; i++ ) + { + if ( i != 0 ) + { + std::cout << ", "; + } + + std::cout << list[i]; + } + std::cout << std::endl; + + return 0; +} + +void sort( int* A, const int size ) { + for (int i = 0 ; i < size ; i++ ) { + for (int j = 0 ; j < size - 1 ; j++) { + #if !USE_PTR + order( A[ i ], A[ j ] ); + #else + orderptr( &A[ i ], &A[ j ] ); + #endif + } + } +} + +#if !USE_PTR +void order( int& a, int& b ) { + // b is greater or equal to a, do nothing + if ( a < b ) { + return; + } + + int tmp = a; + a = b; + b = tmp; +} +#else +void orderptr( int* a, int* b ) { + if ( *a < *b ) { + return; + } + + int* tmp = a; + a = b; + b = tmp; +} +#endif diff --git a/ex2/ex2.1/bubble_sort_str.cpp b/ex2/ex2.1/bubble_sort_str.cpp new file mode 100644 index 0000000..b6b700a --- /dev/null +++ b/ex2/ex2.1/bubble_sort_str.cpp @@ -0,0 +1,126 @@ +/* + * Bubble sort a list of 10 strings + * + * We want the strings to persist in memory, that is why we use a 'const char*'. + * + */ +#include +#include + + +#define LIST_SIZE 10 +#define USE_PTR 1 + +#define VERBOSE 0 + +void sort( const char** A, const int size ); +#if !USE_PTR +void order( const char* a, const char* b ); +#else +void orderptr( const char* &a, const char* &b ); +#endif + + +int main() { + const char* list[LIST_SIZE] = { + "b", + "cccccccccc", + "aaabcd", + "axefsaf", + "poqkud", + "6xnj439yu", + "nullieksj", + "bell", + "uuisdjk", + "aaaaaaa", + }; + + // Print the original + std::cout << "Original: "; + + for( int i = 0; i < LIST_SIZE ; i++ ) + { + if ( i != 0 ) + { + std::cout << ", "; + } + + std::cout << list[i]; + } + std::cout << std::endl; + + // sort it + sort( list, LIST_SIZE ); + + // Print it + std::cout << "Sorted: "; + for( int i = 0; i < LIST_SIZE ; i++ ) + { + if ( i != 0 ) + { + std::cout << ", "; + } + + std::cout << list[i]; + } + std::cout << std::endl; + + return 0; +} + +void sort( const char** A, const int size ) { + for ( int i = 0 ; i < size ; i++ ) { + for ( int j = 0 ; j < size - 1 ; j++ ) { + #if VERBOSE + std::cout << "SORT: " << A[i] << ", " << A[j]<< std::endl; + #endif + + #if !USE_PTR + order( A[ i ], A[ j ] ); + #else + orderptr( A[ i ], A[ j ] ); + #endif + } + } +} + +#if !USE_PTR +void order( const char* a, const char* b ) { + #if VERBOSE + std::cout << "Order: " << a << std::endl; + #endif + // b is greater or equal to a, do nothing + if ( strcmp(a, b) > 0) { + #if VERBOSE + std::cout << "Ordering OK" << std::endl; + #endif + return; + } + #if VERBOSE + std::cout << "Ordering ToDo" << std::endl; + #endif + + const char* tmp = a; + a = b; + b = tmp; +} +#else +void orderptr( const char* &a, const char* &b ) { + #if VERBOSE + std::cout << "Order: " << a << ", " << b << std::endl; + #endif + if ( strcmp( a, b ) > 0 ) { + #if VERBOSE + std::cout << "Ordering OK" << std::endl; + #endif + return; + } + #if VERBOSE + std::cout << "Ordering ToDo" << std::endl; + #endif + + const char* tmp = a; + a = b; + b = tmp; +} +#endif diff --git a/ex2/ex2.2/overloading.cpp b/ex2/ex2.2/overloading.cpp new file mode 100644 index 0000000..0937450 --- /dev/null +++ b/ex2/ex2.2/overloading.cpp @@ -0,0 +1,55 @@ +/* + * Function Overloading + * + * generalisation of type is possible + * => not writing an int implementation means always use the double + * => => even when to ints are supplied, they are generalised to doubles + */ + +#include + +//int min( int a, int b ); +double min( double a, double b ); + +int min( int a[], int size); + + + +int main() { + + const int size = 5; + int list[size] = { 42, 37, 29, 23, 19 }; + + std::cout << "min(5, 1) = " << min(5,1) << std::endl; + std::cout << "min([42,37,29,23,19],5) = " << min(list,size) << std::endl; + + std::cout << "min( 3.12345, 3.15432 ) = " << min(3.12345, 3.15432) << std::endl; + + + std::cout << "min( 3.12345, 4 ) = " << min(3.12345, 4) << std::endl; + return 0; +} + +//int min( int a, int b ) { +// if ( a < b ) { +// return a; +// } +// return b; +//} + +double min( double a, double b ) { + if ( a < b ) { + return a; + } + return b; +} + +int min( int a[], int size ) { + int m = a[0]; + + for( int i = 1; i < size ; ++i ) { + m = min(a[i], m); + } + + return m; +} diff --git a/ex2/ex2.3/namespacing.cpp b/ex2/ex2.3/namespacing.cpp new file mode 100644 index 0000000..9e6a1e1 --- /dev/null +++ b/ex2/ex2.3/namespacing.cpp @@ -0,0 +1,34 @@ +#include + +namespace Black { + void print(int k) { + std::cout << "Black print" << std::endl; + }; +} + +namespace White { + void print(int k) { + std::cout << "White print" << std::endl; + }; +} + + +void sub1() { + using White::print ; + print(5); +} + +void print(int k) { + if ( k > 0 ) + { + print(k-1); + } +} + +int main() { + sub1(); + + print(7); + + return 0; +} diff --git a/ex2/ex2.3/namespacing.txt b/ex2/ex2.3/namespacing.txt new file mode 100644 index 0000000..6f7d913 --- /dev/null +++ b/ex2/ex2.3/namespacing.txt @@ -0,0 +1,11 @@ +a) + the `print(5)` line calls White::print + the `print(k-1)` line calls itself + +b) + the line is allowed by itself, + however since we define a `void print(int k)` function + there is no way to distinguish which one is called. + thus it becomes illegal. + + Including it in main would be OK diff --git a/ex2/ex2.4/build.txt b/ex2/ex2.4/build.txt new file mode 100644 index 0000000..8f9a030 --- /dev/null +++ b/ex2/ex2.4/build.txt @@ -0,0 +1,8 @@ +d) +g++ -c max.cc; +g++ -c min.cc; + +ar q libMinMax.a max.o min.o + +g++ test.cpp -L. -llibMinMax.a + diff --git a/ex2/ex2.4/max.cc b/ex2/ex2.4/max.cc new file mode 100755 index 0000000..28649ee --- /dev/null +++ b/ex2/ex2.4/max.cc @@ -0,0 +1,20 @@ +//max.cc +namespace mylib { + double max( double a, double b ) { + if ( a < b ) { + return b; + } + + return a; + } + + int max( int a[], int size ) { + int m = a[0]; + + for( int i = 1; i < size ; ++i ) { + m = max(a[i], m); + } + + return m; + } +} diff --git a/ex2/ex2.4/max.hh b/ex2/ex2.4/max.hh new file mode 100755 index 0000000..cc6f4e5 --- /dev/null +++ b/ex2/ex2.4/max.hh @@ -0,0 +1,8 @@ +//max.hh +#ifndef MAX_HH +#define MAX_HH +namespace mylib { + double max( double a, double b ); + int max( int a[], int size); +} +#endif diff --git a/ex2/ex2.4/min.cc b/ex2/ex2.4/min.cc new file mode 100755 index 0000000..5485aca --- /dev/null +++ b/ex2/ex2.4/min.cc @@ -0,0 +1,19 @@ +//min.cc +namespace mylib { + double min( double a, double b ) { + if ( a < b ) { + return a; + } + return b; + } + + int min( int a[], int size ) { + int m = a[0]; + + for( int i = 1; i < size ; ++i ) { + m = min(a[i], m); + } + + return m; + } +} diff --git a/ex2/ex2.4/min.hh b/ex2/ex2.4/min.hh new file mode 100755 index 0000000..0359993 --- /dev/null +++ b/ex2/ex2.4/min.hh @@ -0,0 +1,8 @@ +//min.hh +#ifndef MIN_HH +#define MIN_HH +namespace mylib { + double min( double a, double b ); + int min( int a[], int size); +} +#endif diff --git a/ex2/ex2.4/test.cpp b/ex2/ex2.4/test.cpp new file mode 100644 index 0000000..eb148b2 --- /dev/null +++ b/ex2/ex2.4/test.cpp @@ -0,0 +1,30 @@ +#include + +#include "max.hh" +#include "min.hh" + +#define LIST_SIZE 5 +int main() { + double dbl_a = 2.31233; + double dbl_b = 91.3239; + + int list[ LIST_SIZE ] = { 0, 2 ,4 , 6, -9 }; + + + std::cout << "Doubles" << std::endl; + std::cout << "max(" << dbl_a << ", " << dbl_b << ") = " << mylib::max(dbl_a, dbl_b ) << std::endl; + std::cout << "min(" << dbl_a << ", " << dbl_b << ") = " << mylib::min(dbl_a, dbl_b ) << std::endl; + std::cout << std::endl; +/* + std::cout << "Int & Doubles" << std::endl; + std::cout << "max(" << list[4] << ", " << dbl_b << ") = " << mylib::max(list[4], dbl_b ) << std::endl; + std::cout << "min(" << list[4] << ", " << dbl_b << ") = " << mylib::min(list[4], dbl_b ) << std::endl; + std::cout << std::endl; +*/ + + std::cout << "List" << std::endl; + //std::cout << "max( list, " << LIST_SIZE << ") = " << mylib::max ) << std::endl; + //std::cout << "min( list, " << dbl_a << ", " << dbl_b << ") = " << mylib::max(dbl_a, dbl_b ) << std::endl; + + return 0; +}