diff --git a/cpp07/ex01/iter.hpp b/cpp07/ex01/iter.hpp index d13b540..96f5794 100644 --- a/cpp07/ex01/iter.hpp +++ b/cpp07/ex01/iter.hpp @@ -6,7 +6,13 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/08/06 20:21:25 by apommier #+# #+# */ -/* Updated: 2022/08/06 20:21:26 by apommier ### ########.fr */ +/* Updated: 2022/08/07 15:43:16 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ +template +void iter(T *tab, int size, void (*ft)(const T &)) +{ + for (int i = 0; i < size; i++) + ft(tab[i]); +} \ No newline at end of file diff --git a/cpp07/ex01/main.cpp b/cpp07/ex01/main.cpp index 458a3a7..62b7567 100644 --- a/cpp07/ex01/main.cpp +++ b/cpp07/ex01/main.cpp @@ -6,11 +6,30 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/08/06 20:21:01 by apommier #+# #+# */ -/* Updated: 2022/08/06 20:22:07 by apommier ### ########.fr */ +/* Updated: 2022/08/07 15:50:31 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ -int main() +#include "iter.hpp" +#include +#include + +template +void display( T &var ) { - return (0); + std::cout << "value is: " << var << std::endl; +} + +int main( void ) +{ + std::string stringTab[3] = {"First", "Second", "Third"}; + char charTab[7] = "Hello!"; + int intTab[3] = {10, 50, 150}; + + std::cout << "==================Int tab==================\n"; + iter(intTab, 3, display); + std::cout << "\n==================Char tab==================\n"; + iter(charTab, 6, display); + std::cout << "\n==================String tab==================\n"; + iter(stringTab, 3, display); } \ No newline at end of file diff --git a/cpp07/ex02/Array.hpp b/cpp07/ex02/Array.hpp index df25cad..b526ebe 100644 --- a/cpp07/ex02/Array.hpp +++ b/cpp07/ex02/Array.hpp @@ -6,7 +6,82 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/08/06 20:21:58 by apommier #+# #+# */ -/* Updated: 2022/08/06 20:21:59 by apommier ### ########.fr */ +/* Updated: 2022/08/07 16:48:34 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ +#include +#include + +template +class Array{ + public : + + Array() + { + this->_array = new T[0](); + this->_size = 0; + } + + Array(const unsigned int n) + { + this->_size = n; + this->_array = new T[n]; + } + + Array(Array const & toCopy) + { + this->_array = new T[toCopy._size]; + this->_size = toCopy._size; + for (unsigned int i = 0; i < toCopy._size; i++) + _array[i] = toCopy._array[i]; + } + + ~Array() + { + delete[] _array; + } + + Array &operator=(const Array &rhs) + { + if (rhs != *this) + { + if (this->_size > 0) + delete[] this->_array; + if (rhs._size > 0) + { + + this->_array = new T[rhs._size](); + for (int i = 0; i < rhs._size; i++) + this->_array[i] = rhs._array[i]; + } + this->_size = rhs._size; + } + return (*this); + } + + T &operator[](const unsigned int index) + { + if (index >= this->_size) + throw invalidIndex(); + return (this->_array[index]); + } + + unsigned int size() const + { + return (this->_size); + } + + class invalidIndex : public std::exception { + char const *what() const throw() + { + return ("You're trying to access a unauthorized memory place ! Stop it"); + } + }; + + private : + + T *_array; + unsigned int _size; + +}; \ No newline at end of file diff --git a/cpp07/ex02/Makefile b/cpp07/ex02/Makefile index 19c30fa..c385448 100644 --- a/cpp07/ex02/Makefile +++ b/cpp07/ex02/Makefile @@ -6,7 +6,7 @@ # By: apommier +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2022/08/06 20:21:51 by apommier #+# #+# # -# Updated: 2022/08/06 20:21:55 by apommier ### ########.fr # +# Updated: 2022/08/07 16:46:31 by apommier ### ########.fr # # # # **************************************************************************** # @@ -15,7 +15,7 @@ SRCS = main.cpp OBJS = ${SRCS:.cpp=.o} CC = c++ -CFLAGS = -Wall -Wextra -Werror -std=c++98 +CFLAGS = -Wall -Wextra -Werror -g -std=c++98 RM = rm -rf .cpp.o: diff --git a/cpp07/ex02/main.cpp b/cpp07/ex02/main.cpp index 0f63a68..c69704d 100644 --- a/cpp07/ex02/main.cpp +++ b/cpp07/ex02/main.cpp @@ -6,11 +6,113 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/08/06 20:21:49 by apommier #+# #+# */ -/* Updated: 2022/08/06 20:22:10 by apommier ### ########.fr */ +/* Updated: 2022/08/07 17:08:20 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ -int main() +#include +#include +#include "Array.hpp" + +#define MAX_VAL 750 +int main(int, char**) { - return (0); + Array numbers(MAX_VAL); + int* mirror = new int[MAX_VAL]; + srand(time(NULL)); + for (int i = 0; i < MAX_VAL; i++) + { + const int value = rand(); + numbers[i] = value; + mirror[i] = value; + } + //SCOPE + { + Array tmp = numbers; + Array test(tmp); + } + for (int i = 0; i < MAX_VAL; i++) + { + if (mirror[i] != numbers[i]) + { + std::cerr << "didn't save the same value!!" << std::endl; + return 1; + } + } + try + { + numbers[-2] = 0; + } + catch(const std::exception& e) + { + std::cerr << e.what() << '\n'; + } + try + { + numbers[MAX_VAL] = 0; + } + catch(const std::exception& e) + { + std::cerr << e.what() << '\n'; + } + + for (int i = 0; i < MAX_VAL; i++) + { + numbers[i] = rand(); + } + std::cout << "\n===================Modify array===================\n"; + try + { + std::cout << "numbers[MAX_VAL - 1] = " << numbers[MAX_VAL - 1] << std::endl; + std::cout << "----Assignation----\n"; + numbers[MAX_VAL - 1] = 0; + std::cout << "numbers[MAX_VAL - 1] = " << numbers[MAX_VAL - 1] << std::endl; + } + catch(const std::exception& e) + { + std::cerr << e.what() << '\n'; + } + delete [] mirror;// + + + +//char array + std::cout << "\n===================Char array===================\n"; + Array chars(15); + char* mirror2 = new char[15]; + for (int i = 0; i < 15; i++) + { + const int value = rand() % 93 + 31; + chars[i] = value; + mirror2[i] = value; + } + //SCOPE + { + Array tmp = chars; + Array test(tmp); + } + for (int i = 0; i < 15; i++) + { + if (mirror2[i] != chars[i]) + { + std::cerr << "didn't save the same value!!" << std::endl; + return 1; + } + } + for (int i = 0; i < 15; i++) + std::cout << chars[i] << std::endl; + try + { + std::cout << chars.size() << std::endl; + std::cout << "chars[MAX_VAL - 1] = " << chars[14] << std::endl; + std::cout << "----Assignation----\n"; + chars[14] = '0'; + std::cout << "chars[MAX_VAL - 1] = " << chars[14] << std::endl; + } + catch(const std::exception& e) + { + std::cerr << e.what() << '\n'; + } + delete[] mirror2; + return 0; } \ No newline at end of file