diff --git a/ex6.1/bubble_sort_int.cpp b/ex6.1/bubble_sort_int.cpp new file mode 100644 index 0000000..9eae5f0 --- /dev/null +++ b/ex6.1/bubble_sort_int.cpp @@ -0,0 +1,129 @@ +/* + * Bubble sort some lists by exploiting templates + * + */ +#include +#include + + +#define LIST_SIZE 10 +template +void display( T t[], int n ); + +template +void sort( T* A, const int size ); + +template +void order( T& a, T& b ); + + +int main() { + int list[LIST_SIZE] = { 2295, 14500, 9127, 23079, 20612, 5571, 21959, 10032, 18339, 16673 }; + float flist[LIST_SIZE] = { 835.2, 3248.8, 0.1213123, 8.234234, 8242.25823, 802348234.8, .9123812414, 0.00000001, 842, 10000000 }; + const char* clist[LIST_SIZE] = { + "b", + "cccccccccc", + "aaabcd", + "axefsaf", + "poqkud", + "6xnj439yu", + "nullieksj", + "bell", + "uuisdjk", + "aaaaaaa", + }; + + // Print the original + std::cout << "Original: "; + display(list, LIST_SIZE); + std::cout << std::endl; + + // sort it + sort( list, LIST_SIZE ); + + // Print it + std::cout << "Sorted: "; + display(list, LIST_SIZE); + std::cout << std::endl; + + + // Doing Floats + + // Print the original + std::cout << "Original: "; + display(flist, LIST_SIZE); + std::cout << std::endl; + + // sort it + sort( flist, LIST_SIZE ); + + // Print it + std::cout << "Sorted: "; + display(flist, LIST_SIZE); + std::cout << std::endl; + + + // Doing Chars + + + // Print the original + std::cout << "Original: "; + display(clist, LIST_SIZE); + std::cout << std::endl; + + // sort it + sort( clist, LIST_SIZE ); + + // Print it + std::cout << "Sorted: "; + display(clist, LIST_SIZE); + std::cout << std::endl; + + + return 0; +} +template +void display( T t[], int n) { + for( int i = 0; i < n ; i++ ) + { + if ( i != 0 ) + { + std::cout << ", "; + } + + std::cout << t[i]; + } + +} + +template +void sort( T* A, const int size ) { + for (int i = 0 ; i < size ; i++ ) { + for (int j = 0 ; j < size - 1 ; j++) { + order( A[ i ], A[ j ] ); + } + } +} + +template +void order( T& a, T& b ) { + // b is greater or equal to a, do nothing + if ( a < b ) { + return; + } + + T tmp = a; + a = b; + b = tmp; +} + +template<> +void order( const char* &a, const char* &b ) { + if ( strcmp(a, b) <= 0 ) { + return ; + } + + const char* tmp = a; + a = b; + b = tmp; +} diff --git a/ex6.2/Array.hh b/ex6.2/Array.hh index ab5718e..84bab41 100644 --- a/ex6.2/Array.hh +++ b/ex6.2/Array.hh @@ -1,16 +1,16 @@ #ifndef ARRAY_HH #define ARRAY_HH +template class Array { public: - - Array(int size) : _size(size) { - _arr = new double[_size] ; + Array(int size, T default_value) : _size(size) { + _arr = new T[_size] ; + _default = default_value; } - Array(const Array& other) : _size(other._size) { - _arr = new double[other._size] ; + _arr = new T[other._size] ; // Copy elements for (int i=0 ; i<_size ; i++) { @@ -35,10 +35,14 @@ public: return *this ; } - double& operator[](int index) { + T& operator[](int index) { + if ( index > _size ) { + resize(index+1); + } + return _arr[index] ; } - const double& operator[](int index) const { + const T& operator[](int index) const { return _arr[index] ; } @@ -46,13 +50,17 @@ public: void resize(int newSize) { // Allocate new array - double* newArr = new double[newSize] ; + T* newArr = new T[newSize] ; // Copy elements for (int i=0 ; i<_size ; i++) { newArr[i] = _arr[i] ; } + for ( int i=_size ; i +#include "Array.hh" + +int main() { + Array intlist(1, 99); + Array strlist(2, 9.9); + + intlist[4] = 50; + std::cout << "Intlist" << std::endl; + for (int i=0; i +class Array { +public: + Array(int size, T default_value) : _size(size) { + _arr = new T[_size] ; + _default = default_value; + } + + Array(const Array& other) : _size(other._size) { + _arr = new T[other._size] ; + + // Copy elements + for (int i=0 ; i<_size ; i++) { + _arr[i] = other._arr[i] ; + } + } + + ~Array() { + delete[] _arr ; + } + + + Array& operator=(const Array& other) + { + if (&other==this) return *this ; + if (_size != other._size) { + resize(other._size) ; + } + for (int i=0 ; i<_size ; i++) { + _arr[i] = other._arr[i] ; + } + return *this ; + } + + T& operator[](int index) { + if ( index > _size ) { + resize(index+1); + } + + return _arr[index] ; + } + const T& operator[](int index) const { + return _arr[index] ; + } + + int size() const { return _size ; } + + void resize(int newSize) { + // Allocate new array + T* newArr = new T[newSize] ; + + // Copy elements + for (int i=0 ; i<_size ; i++) { + newArr[i] = _arr[i] ; + } + + for ( int i=_size ; i +class Stack { + // Interface + public: + Stack( int in_size ) { + count = 0; + _s = new Array(in_size, 0); + } + + int nitems() { return count ; } + bool full() { return (count == _s->size()) ; } + bool empty() { return (count == 0) ; } + + void push(T c) { + if (full()) { + std::cout << "Stack::push() Error: stack is full" << std::endl ; + + _s->resize( _s->size() + 10 ); + } + + _s[0][count++] = c ; + } + + T pop() { + if (empty()) { + std::cout << "Stack::pop() Error: stack is empty" << std::endl ; + return 0 ; + } + + return _s[0][--count] ; + } + + void inspect() { + for ( int i = nitems() - 1; i >= 0; i-- ) + { + std::cout << i << ": " << _s[0][i] << std::endl; + } + } + + // Implementation + private: + Array* _s ; + int count ; + +}; +#endif diff --git a/ex6.3/main.cpp b/ex6.3/main.cpp new file mode 100644 index 0000000..028d41e --- /dev/null +++ b/ex6.3/main.cpp @@ -0,0 +1,52 @@ +#include +#include "Stack.hh" + +using namespace std ; + +int main() { + Stack s(5) ;// initialize Stack + s.push(5); + + // Write doubles into Stack + int i ; + for (i=0 ; i<10 ; i++) { + cout << "pushing value " << i*i << " in stack" << endl ; + + s.push(i*i) ; + } + + cout << "Clone s to sclone" << endl; + Stack sclone = s; + + cout << "Inspect s's FIFO" << endl; + s.inspect(); + + cout << "Inspect sclone's FIFO" << endl; + sclone.inspect(); + //The Same Data + + while (!s.empty()) { + double val = s.pop() ; + cout << "popping value " << val << " from stack" << endl ; + } + + cout << "Inspect s's FIFO" << endl; + s.inspect(); + + cout << "Inspect sclone's FIFO" << endl; + sclone.inspect(); + //Not the Same Data after copy constructor + + for (int i = 0; i < 5 ; i++ ) + { + s.push(100*i) ; + } + + cout << "Inspect s's FIFO" << endl; + s.inspect(); + + cout << "Inspect sclone's FIFO" << endl; + sclone.inspect(); + //Not the Same Data after copy constructor + return 0 ; +} diff --git a/ex6.3/remarks.txt b/ex6.3/remarks.txt new file mode 100644 index 0000000..6e7a095 --- /dev/null +++ b/ex6.3/remarks.txt @@ -0,0 +1,2 @@ +The implementation of Stack requires to use _s[0] whenever I want to operate on the array directly. +I don't understand why this is necessary.