Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions TASK.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
##������.
��� ������ �� a[N] ���������. ���������� ����������� �������� (�(n)) ��������, ������� ���������� �� ������ ����� ������, ��� out[i] ����� ����� ������������ ���� ��������� a[N], �� ����������� ��������. �������� ������� ��������.
22 changes: 20 additions & 2 deletions app/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
#include "add.h"
#include <iostream>
#include <ctime>
#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] << " ";
}
6 changes: 0 additions & 6 deletions include/add.h

This file was deleted.

6 changes: 6 additions & 0 deletions include/massive.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef INCLUDE_MASSIVE_H_
#define INCLUDE_MASSIVE_H_

int* made_massive(int* a, int N);

#endif // INCLUDE_MASSIVE_H_
3 changes: 0 additions & 3 deletions src/add.cpp

This file was deleted.

58 changes: 58 additions & 0 deletions src/massive.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include "massive.h"
#include <iostream>

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;
}
7 changes: 0 additions & 7 deletions test/test_add.cpp

This file was deleted.

69 changes: 69 additions & 0 deletions test/test_massive.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include <gtest/gtest.h>
#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]);
}