commit e0535f2b4acf2e1ce6a18b93eb9e84a20a90e054 Author: yosyo Date: Sat Aug 16 20:30:27 2025 +0200 「🎉」 init: hello world ! diff --git a/ex00/Makefile b/ex00/Makefile new file mode 100644 index 0000000..69ff34e --- /dev/null +++ b/ex00/Makefile @@ -0,0 +1,58 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: mmoussou + +int main(void) { + int a = 2; + int b = 3; + + ::swap(a, b); + std::cout << "a = " << a << ", b = " << b << std::endl; + std::cout << "min( a, b ) = " << ::min(a, b) << std::endl; + std::cout << "max( a, b ) = " << ::max(a, b) << std::endl; + + std::string c = "chaine1"; + std::string d = "chaine2"; + + ::swap(c, d); + std::cout << "c = " << c << ", d = " << d << std::endl; + std::cout << "min( c, d ) = " << ::min(c, d) << std::endl; + std::cout << "max( c, d ) = " << ::max(c, d) << std::endl; + + return 0; +} + diff --git a/ex00/whatever.hpp b/ex00/whatever.hpp new file mode 100644 index 0000000..6357fb0 --- /dev/null +++ b/ex00/whatever.hpp @@ -0,0 +1,21 @@ +#pragma once + +template void swap(T &a, T &b) +{ + T tmp; + + tmp = a; + a = b; + b = tmp; +} + +template T min(T &a, T &b) +{ + return (a < b ? a : b); +} + + +template T max(T &a, T&b) +{ + return (a > b ? a : b); +} diff --git a/ex01/Makefile b/ex01/Makefile new file mode 100644 index 0000000..821cfc4 --- /dev/null +++ b/ex01/Makefile @@ -0,0 +1,58 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: mmoussou void iter(T *arr, int len, void(*func)(T &)) { + for (int i = 0; i< len; i++) { + func(arr[i]); + } +} + +template void iter(const T *arr, int len, void(*func)(const T &)) { + for (int i = 0; i< len; i++) { + func(arr[i]); + } +} diff --git a/ex01/main.cpp b/ex01/main.cpp new file mode 100644 index 0000000..3e0951e --- /dev/null +++ b/ex01/main.cpp @@ -0,0 +1,23 @@ +#include "iter.hpp" +#include + +void increment(int &nb) +{ + nb++; +} + +void print(const int &nb) +{ + std::cout << nb << " "; +} + +int main(void) +{ + int arr[4] = {1, 2, 3, 4}; + + iter(arr, 4, &print); + std::cout << std::endl; + iter(arr, 4, &increment); + iter(arr, 4, &print); + std::cout << std::endl; +} diff --git a/ex02/Array.hpp b/ex02/Array.hpp new file mode 100644 index 0000000..5ef147f --- /dev/null +++ b/ex02/Array.hpp @@ -0,0 +1,47 @@ +#include +#include +#include +#include + +template class Array { +public: + Array(): _arr(new T[0]), _size(0) {} + Array(const int n): _arr(new T[n]), _size(n) {} + Array(const Array &cpy) {*this = cpy;} + ~Array(void) {delete[] _arr;} + + Array &operator=(const Array &cpy) + { + if (this != &cpy) { + _size = cpy._size; + _arr = new T[_size]; + if (_arr) + { + for (std::size_t i = 0; i < _size; i++) + _arr[i] = cpy._arr[i]; + } + } + return (*this); + } + + T &operator[](const size_t &pos) + { + if (pos < 0 || pos >= _size) + { + throw outOfBoundException(); + } + return _arr[pos]; + } + + class outOfBoundException : public std::exception { + virtual const char *what() const throw() + { + return ("value is out of bound"); + } + }; + + +private: + T *_arr; + size_t _size; +}; diff --git a/ex02/Makefile b/ex02/Makefile new file mode 100644 index 0000000..a56f759 --- /dev/null +++ b/ex02/Makefile @@ -0,0 +1,58 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: mmoussou +#include +#include + +#define MAX_VAL 750 +int main(int, char**) +{ + 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() % 100; + numbers[i] = value; + mirror[i] = value; + } + //SCOPE + { + Array tmp = numbers; + Array test(tmp); + } + + std::cout << "checking if values are the same after a copy..." << std::endl; + 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; + } + } + std::cout << "the values are the same ! :D" << std::endl << std::endl; + + std::cout << "trying -2:" << std::endl; + try + { + numbers[-2] = 0; + } + catch(const std::exception& e) + { + std::cerr << e.what() << '\n'; + } + + std::cout << std::endl << "trying a correct value (15):" << std::endl; + try + { + std::cout << numbers[15] << std::endl; + numbers[15] = 0; + std::cout << numbers[15] << std::endl; + } + catch(const std::exception& e) + { + std::cerr << e.what() << '\n'; + } + + std::cout << std::endl << "trying MAX_VAL:" << std::endl; + try + { + numbers[MAX_VAL] = 0; + } + catch(const std::exception& e) + { + std::cerr << e.what() << '\n'; + } + + std::cout << std::endl; + for (int i = 0; i < MAX_VAL; i++) + { + std::cout << numbers[i] << " "; + } + std::cout << std::endl; + delete [] mirror; + return 0; +}