diff --git a/TASK.md b/TASK.md new file mode 100644 index 0000000..9bf35aa --- /dev/null +++ b/TASK.md @@ -0,0 +1,2 @@ +##Задача. +Дан массив из a[N] элементов. Необходимо реализовать линейный (О(n)) алгоритм, который сформирует на выходе такой массив, что out[i] будет равен произведению всех элементов a[N], за исключением текущего. Оператор деления запрещён. \ No newline at end of file diff --git a/app/main.cpp b/app/main.cpp index 4485790..0bc45f8 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -1,6 +1,24 @@ -#include "add.h" #include +#include +#include "massive.h" + +using std::cout; +using std::endl; int main() { - std::cout << "2 + 2 = " << add(2, 2) << std::endl; + srand(time(0)); + int const N = 7; + int* a = new int[N]; + int* result = new int[N]; + + cout << "Source array:" << endl; + for (int i = 0; i < N; i++) + a[i] = rand() % 10; + for (int i = 0; i < N; i++) + cout << a[i] << " "; + + cout << "\nArray of products except the current elements:" << endl; + result = made_massive(a, N); + for (int i = 0; i < N; ++i) + cout << result[i] << " "; } diff --git a/include/add.h b/include/add.h deleted file mode 100644 index ebb1c94..0000000 --- a/include/add.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef INCLUDE_ADD_H_ -#define INCLUDE_ADD_H_ - -int add(int x, int y); - -#endif // INCLUDE_ADD_H_ diff --git a/include/massive.h b/include/massive.h new file mode 100644 index 0000000..bb031e3 --- /dev/null +++ b/include/massive.h @@ -0,0 +1,6 @@ +#ifndef INCLUDE_MASSIVE_H_ +#define INCLUDE_MASSIVE_H_ + +int* made_massive(int* a, int N); + +#endif // INCLUDE_MASSIVE_H_ diff --git a/src/add.cpp b/src/add.cpp deleted file mode 100644 index 35bf82f..0000000 --- a/src/add.cpp +++ /dev/null @@ -1,3 +0,0 @@ -int add(int x, int y) { - return x + y; -} diff --git a/src/massive.cpp b/src/massive.cpp new file mode 100644 index 0000000..fe31052 --- /dev/null +++ b/src/massive.cpp @@ -0,0 +1,58 @@ +#include "massive.h" +#include + +int* made_massive(int* a, int N) { + int* out = new int[N]; + int* tmp = new int[N - 1]; + int pr1 = 1, pr2 = 1, pr = 1; + int zero = 0, index = 0; + + // empty massive + if (N == 0) + throw std::logic_error("Error description: empty massive.\n"); + + // massive of 1 element + if (N == 1) { + out[0] = 1; + return out; + } + + for (int i = 0; i < N; i++) { + if (a[i] == 0) { + zero++; + index = i; + } else { + pr *= a[i]; + } + } + + // massive with 1 null-element + if (zero == 1) { + for (int i = 0; i < N; i++) { + out[i] = 0; + } + out[index] = pr; + return out; + } + + // massive with null-elements + if (zero > 1) { + for (int i = 0; i < N; i++) { + out[i] = 0; + } + return out; + } + + for (int i = 0; i < N - 1; i++) { + pr1 *= a[i]; + tmp[i] = pr1; + } + for (int j = N - 1; j > 0; j--) { + out[j] = pr2 * tmp[j - 1]; + pr2 *= a[j]; + } + out[0] = pr2; + delete[] tmp; + + return out; +} diff --git a/test/test_add.cpp b/test/test_add.cpp deleted file mode 100644 index 66c2df3..0000000 --- a/test/test_add.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include -#include "add.h" - -TEST(Addition, CanAddTwoNumbers) { - EXPECT_EQ(add(2, 2), 4); - EXPECT_EQ(add(-2, 2), 0); -} diff --git a/test/test_massive.cpp b/test/test_massive.cpp new file mode 100644 index 0000000..8480835 --- /dev/null +++ b/test/test_massive.cpp @@ -0,0 +1,69 @@ +#include +#include "massive.h" + +TEST(MadeMassive, throw_when_work_with_empty_massive) { + const int N = 0; + int* a = nullptr; + + EXPECT_ANY_THROW(made_massive(a, N)); +} + +TEST(MadeMassive, can_work_with_massive_of_one_element) { + const int N = 1; + int a[1] = { 5 }; + int exp[1] = { 1 }; + int *res = made_massive(a, N); + + for (int i = 0; i < N; ++i) + EXPECT_EQ(res[i], exp[i]); +} + +TEST(MadeMassive, can_work_with_massive_with_null_element) { + const int N = 6; + int a[6] = { 1, 2, 0, 4, 1, 1 }; + int exp[6] = { 0, 0, 8, 0, 0, 0 }; + int *res = made_massive(a, N); + + for (int i = 0; i < N; ++i) + EXPECT_EQ(res[i], exp[i]); +} + +TEST(MadeMassive, can_work_with_massive_with_null_elements) { + const int N = 6; + int a[6] = { 1, 2, 0, 4, 0, 1 }; + int exp[6] = { 0, 0, 0, 0, 0, 0 }; + int *res = made_massive(a, N); + + for (int i = 0; i < N; ++i) + EXPECT_EQ(res[i], exp[i]); +} + +TEST(MadeMassive, can_work_with_simple_massive_1) { + const int N = 6; + int a[6] = { 1, 2, 3, 4, 1, 5 }; + int exp[6] = { 120, 60, 40, 30, 120, 24 }; + int *res = made_massive(a, N); + + for (int i = 0; i < N; ++i) + EXPECT_EQ(res[i], exp[i]); +} + +TEST(MadeMassive, can_work_with_simple_massive_2) { + const int N = 6; + int a[6] = { 1, 1, 1, 1, 1, 1 }; + int exp[6] = { 1, 1, 1, 1, 1, 1 }; + int *res = made_massive(a, N); + + for (int i = 0; i < N; ++i) + EXPECT_EQ(res[i], exp[i]); +} + +TEST(MadeMassive, can_work_with_simple_massive_3) { + const int N = 6; + int a[6] = { 3, 3, 3, 3, 3, 3 }; + int exp[6] = { 243, 243, 243, 243, 243, 243 }; + int *res = made_massive(a, N); + + for (int i = 0; i < N; ++i) + EXPECT_EQ(res[i], exp[i]); +}