From a514b66d10c0c6ff1dc3bf08f78fe491cc98b40c Mon Sep 17 00:00:00 2001 From: deepanshumidha5140 <110282117+deepanshumidha5140@users.noreply.github.com> Date: Sat, 1 Oct 2022 05:50:13 +0530 Subject: [PATCH 1/3] Create accumulate.cpp --- snippets/vector/accumulate.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 snippets/vector/accumulate.cpp diff --git a/snippets/vector/accumulate.cpp b/snippets/vector/accumulate.cpp new file mode 100644 index 00000000..ce3c78df --- /dev/null +++ b/snippets/vector/accumulate.cpp @@ -0,0 +1,21 @@ +// Style Guide => https://github.com/Bhupesh-V/30-Seconds-Of-STL/blob/master/CONTRIBUTING.md/#Style Guide +/* + Author : Deepanshu Midha + Date : 01/10/2022 + Time : 05:50 + Description : Returns the sum of all the values lying in a range between [first, last) +*/ + +#include +#include +using namespace std; + +int main() +{ + std::vector v = {3, 13, 27}; + int sum = 7; + cout << "Result using accumulate: "; + cout << accumulate(v.begin(), v.end(), sum); + + return 0; +} From f3f690ea77eba4751ee863a323e1b27f3d0b4401 Mon Sep 17 00:00:00 2001 From: deepanshumidha5140 <110282117+deepanshumidha5140@users.noreply.github.com> Date: Sat, 1 Oct 2022 06:10:34 +0530 Subject: [PATCH 2/3] Update accumulate.cpp --- snippets/vector/accumulate.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/snippets/vector/accumulate.cpp b/snippets/vector/accumulate.cpp index ce3c78df..870d74a5 100644 --- a/snippets/vector/accumulate.cpp +++ b/snippets/vector/accumulate.cpp @@ -8,14 +8,13 @@ #include #include -using namespace std; +#include -int main() -{ +int main() { std::vector v = {3, 13, 27}; int sum = 7; - cout << "Result using accumulate: "; - cout << accumulate(v.begin(), v.end(), sum); + std::cout << "Result using accumulate: "; + std::cout << accumulate(v.begin(), v.end(), sum); return 0; } From 3eb8ea12b59a6c8f7680c0b21e4d234020d3beb4 Mon Sep 17 00:00:00 2001 From: deepanshumidha5140 <110282117+deepanshumidha5140@users.noreply.github.com> Date: Sat, 1 Oct 2022 06:14:41 +0530 Subject: [PATCH 3/3] Create accumulate.md --- vector/accumulate.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 vector/accumulate.md diff --git a/vector/accumulate.md b/vector/accumulate.md new file mode 100644 index 00000000..fa130225 --- /dev/null +++ b/vector/accumulate.md @@ -0,0 +1,24 @@ +# Accumulate + +**Description :** +- This function returns the sum of all the values lying in a range between [first, last) with the variable sum. +- Usually the sum of elements in a particular range or a complete array is found using a linear operation which requires adding all the elements in the range one by one and storing it into some variable after each iteration. + +**Example** : + +```cpp +#include +#include +#include + +int main() { + std::vector v = {3, 13, 27}; + int sum = 7; + std::cout << "Result using accumulate: "; + std::cout << accumulate(v.begin(), v.end(), sum); + + return 0; +} +``` +**[See Sample code](https://github.com/deepanshumidha5140/30-seconds-of-cpp/blob/master/snippets/vector/accumulate.cpp)** +**[Run Code](https://rextester.com/BFYS36316)**