From 1de596dd9242b677d6b47b51db98ce98feb95465 Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Mon, 14 Dec 2020 15:30:28 +0100 Subject: Updated cpp07 files with new subject --- cpp07/ex01/Makefile | 4 ++-- cpp07/ex01/iter.cpp | 64 ----------------------------------------------------- cpp07/ex01/iter.hpp | 31 ++++++++++++++++++++++++++ cpp07/ex01/main.cpp | 44 ++++++++++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 66 deletions(-) delete mode 100644 cpp07/ex01/iter.cpp create mode 100644 cpp07/ex01/iter.hpp create mode 100644 cpp07/ex01/main.cpp (limited to 'cpp07/ex01') diff --git a/cpp07/ex01/Makefile b/cpp07/ex01/Makefile index 45180bc..7bee119 100644 --- a/cpp07/ex01/Makefile +++ b/cpp07/ex01/Makefile @@ -6,7 +6,7 @@ # By: charles +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/04/14 19:48:34 by charles #+# #+# # -# Updated: 2020/04/14 20:02:14 by charles ### ########.fr # +# Updated: 2020/12/14 15:07:28 by cacharle ### ########.fr # # # # **************************************************************************** # @@ -15,7 +15,7 @@ NAME = iter CXX = clang++ CXXFLAGS = -std=c++98 -Wall -Wextra -Werror -SRC = iter.cpp +SRC = main.cpp OBJ = $(SRC:.cpp=.o) all: $(NAME) diff --git a/cpp07/ex01/iter.cpp b/cpp07/ex01/iter.cpp deleted file mode 100644 index 831286f..0000000 --- a/cpp07/ex01/iter.cpp +++ /dev/null @@ -1,64 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* iter.cpp :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: charles +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/04/14 20:02:19 by charles #+# #+# */ -/* Updated: 2020/12/11 15:45:09 by charles ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include - -template -void iter(T* ptr, size_t len, void (*f)(T const& x)) -{ - for (size_t i = 0; i < len; i++) - f(ptr[i]); -} - -/* sel-melc says const& */ -template -void timeTwo(T const& x) -{ - x *= 2; -} - -int main() -{ - int intArray[] = {1, 2, 3, 4, 10, 20, 30, -1, -2}; - size_t intArraySize = sizeof(intArray) / sizeof(int); - for (size_t i = 0; i < intArraySize; i++) - std::cout << intArray[i] << ", "; - std::cout << std::endl; - iter(intArray, intArraySize, timeTwo); - for (size_t i = 0; i < intArraySize; i++) - std::cout << intArray[i] << ", "; - std::cout << std::endl; - std::cout << "--------------------------------------" << std::endl; - - float floatArray[] = {1.1, 2.2, 3.3, 4.3, 10.001, 20.9, 30.3, -1.2, -2.4}; - size_t floatArraySize = sizeof(floatArray) / sizeof(float); - for (size_t i = 0; i < floatArraySize; i++) - std::cout << floatArray[i] << ", "; - std::cout << std::endl; - iter(floatArray, floatArraySize, timeTwo); - for (size_t i = 0; i < floatArraySize; i++) - std::cout << floatArray[i] << ", "; - std::cout << std::endl; - std::cout << "--------------------------------------" << std::endl; - - unsigned int uintArray[] = {1, 2, 3, 4, 10, 20, 30, 100, 2000}; - size_t uintArraySize = sizeof(uintArray) / sizeof(unsigned int); - for (size_t i = 0; i < uintArraySize; i++) - std::cout << uintArray[i] << ", "; - std::cout << std::endl; - iter(uintArray, uintArraySize, timeTwo); - for (size_t i = 0; i < uintArraySize; i++) - std::cout << uintArray[i] << ", "; - std::cout << std::endl; - - return 0; -} diff --git a/cpp07/ex01/iter.hpp b/cpp07/ex01/iter.hpp new file mode 100644 index 0000000..df1a37a --- /dev/null +++ b/cpp07/ex01/iter.hpp @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* iter.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/12/14 15:06:48 by cacharle #+# #+# */ +/* Updated: 2020/12/14 15:18:09 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ITER_HPP +# define ITER_HPP + +# include + +template +void iter(T* ptr, size_t len, void (*f)(T const& x)) +{ + for (size_t i = 0; i < len; i++) + f(ptr[i]); +} + +template +void timeTwo(T const& x) +{ + std::cout << "timeTwo function " << x << " * 2 = " << x * 2 << std::endl; +} + +#endif diff --git a/cpp07/ex01/main.cpp b/cpp07/ex01/main.cpp new file mode 100644 index 0000000..6ac8e82 --- /dev/null +++ b/cpp07/ex01/main.cpp @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/14 20:02:19 by charles #+# #+# */ +/* Updated: 2020/12/14 15:16:32 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "iter.hpp" + +int main() +{ + int intArray[] = {1, 2, 3, 4, 10, 20, 30, -1, -2}; + size_t intArraySize = sizeof(intArray) / sizeof(int); + for (size_t i = 0; i < intArraySize; i++) + std::cout << intArray[i] << ", "; + std::cout << std::endl; + iter(intArray, intArraySize, timeTwo); + + std::cout << "--------------------------------------" << std::endl; + + float floatArray[] = {1.1, 2.2, 3.3, 4.3, 10.001, 20.9, 30.3, -1.2, -2.4}; + size_t floatArraySize = sizeof(floatArray) / sizeof(float); + for (size_t i = 0; i < floatArraySize; i++) + std::cout << floatArray[i] << ", "; + std::cout << std::endl; + iter(floatArray, floatArraySize, timeTwo); + + std::cout << "--------------------------------------" << std::endl; + + unsigned int uintArray[] = {1, 2, 3, 4, 10, 20, 30, 100, 2000}; + size_t uintArraySize = sizeof(uintArray) / sizeof(unsigned int); + for (size_t i = 0; i < uintArraySize; i++) + std::cout << uintArray[i] << ", "; + std::cout << std::endl; + iter(uintArray, uintArraySize, timeTwo); + + return 0; +} -- cgit