From 2adadc11fcef57f16e7080cfaef97839d9027f5c Mon Sep 17 00:00:00 2001 From: artking4815162342 Date: Tue, 6 Oct 2015 00:14:01 +0300 Subject: [PATCH 1/8] =?UTF-8?q?=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=B7=D0=B0=D0=B3=D0=BE=D0=BB=D0=BE=D0=B2=D0=BE=D1=87?= =?UTF-8?q?=D0=BD=D0=BE=D0=B3=D0=BE=20=D1=84=D0=B0=D0=B9=D0=BB=D0=B0=20?= =?UTF-8?q?=D0=B8=20=D1=84=D0=B0=D0=B9=D0=BB=D0=B0=20main?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- arrays.cpp | 4 ++-- arrays.h | 6 ++++++ main.cpp | 9 +++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 arrays.h create mode 100644 main.cpp diff --git a/arrays.cpp b/arrays.cpp index a2c541a..1246e8f 100644 --- a/arrays.cpp +++ b/arrays.cpp @@ -1,7 +1,7 @@ #include #include -#if 0 + // NOTE: you should move this `if` down as you start solving each task void task_1() @@ -79,4 +79,4 @@ void task_7_print(int * arr, int size, char delim = ' ') } } -#endif + diff --git a/arrays.h b/arrays.h new file mode 100644 index 0000000..5534bce --- /dev/null +++ b/arrays.h @@ -0,0 +1,6 @@ +#ifndef _ARRAYS +#define _ARRAYS + + + +#endif \ No newline at end of file diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..06f7b81 --- /dev/null +++ b/main.cpp @@ -0,0 +1,9 @@ +#include +#include + +using namespace std; + +int main() +{ + +} \ No newline at end of file From bf5059a6227d4d599b38b5dc6a3b3a3c73243c5d Mon Sep 17 00:00:00 2001 From: artking4815162342 Date: Tue, 6 Oct 2015 01:00:49 +0300 Subject: [PATCH 2/8] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=BF=D0=B5=D1=80=D0=B2=D0=B0=D1=8F=20?= =?UTF-8?q?=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- arrays.cpp | 8 +++++--- main.cpp | 1 + 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/arrays.cpp b/arrays.cpp index 1246e8f..580dc09 100644 --- a/arrays.cpp +++ b/arrays.cpp @@ -1,8 +1,7 @@ #include #include - -// NOTE: you should move this `if` down as you start solving each task +using namespace std; void task_1() { @@ -12,7 +11,7 @@ void task_1() * pedantic flag here means: strictly as in language standard * for explanation see: * https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html*/ - int n = 10; + const int n = 10; int a[n]; } @@ -68,6 +67,7 @@ void task_6_poor_copy(int * arr1, int * arr2) } } +/* // Task 7 // print array `arr` void task_7_print(int * arr, int size, char delim = ' ') @@ -78,5 +78,7 @@ void task_7_print(int * arr, int size, char delim = ' ') std::cout << a[i] << delim; } } +*/ + diff --git a/main.cpp b/main.cpp index 06f7b81..5ed8465 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,6 @@ #include #include +#include "arrays.h" using namespace std; From 08b98f75c85192764315c9301e5f5a61e10e1180 Mon Sep 17 00:00:00 2001 From: artking4815162342 Date: Tue, 6 Oct 2015 22:26:30 +0300 Subject: [PATCH 3/8] =?UTF-8?q?=D0=B2=D1=8B=D0=BF=D0=BE=D0=BB=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=BE=20=D0=B7=D0=B0=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5=202,?= =?UTF-8?q?=20=D0=B2=D1=8B=D0=B1=D1=80=D0=B0=D1=82=D1=8C=20=D0=BD=D0=B5?= =?UTF-8?q?=D0=BE=D0=B1=D1=85=D0=BE=D0=B4=D0=B8=D0=BC=D0=BE=20assert=20?= =?UTF-8?q?=D1=81=20=D0=BE=D1=82=D1=80=D0=B8=D1=86=D0=B0=D0=BD=D0=B8=D0=B5?= =?UTF-8?q?=D0=BC,=20=D0=BF=D0=BE=D1=81=D0=BA=D0=BE=D0=BB=D1=8C=D0=BA?= =?UTF-8?q?=D1=83=20=D1=8D=D1=82=D0=BE=D1=82=20=D1=8D=D0=BB=D0=B5=D0=BC?= =?UTF-8?q?=D0=B5=D0=BD=D1=82=20=D0=B8=D0=BD=D0=B8=D1=86=D0=B8=D0=B0=D0=BB?= =?UTF-8?q?=D0=B8=D0=B7=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=20=D0=BD=D1=83?= =?UTF-8?q?=D0=BB=D1=91=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- arrays.cpp | 6 +++--- arrays.h | 2 ++ main.cpp | 3 ++- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/arrays.cpp b/arrays.cpp index 580dc09..4574915 100644 --- a/arrays.cpp +++ b/arrays.cpp @@ -19,9 +19,9 @@ void task_1() void task_2() { int a[5] = {0}; - - //assert(!a[4]) or assert(a[4]) -- place here one of these and - // explain your choice in commit msg + assert(!a[4]); + //place here one of these and + //explain your choice in commit msg } // returns zero diff --git a/arrays.h b/arrays.h index 5534bce..b85d445 100644 --- a/arrays.h +++ b/arrays.h @@ -1,6 +1,8 @@ #ifndef _ARRAYS #define _ARRAYS +void task_1(); +void task_2(); #endif \ No newline at end of file diff --git a/main.cpp b/main.cpp index 5ed8465..83730ab 100644 --- a/main.cpp +++ b/main.cpp @@ -6,5 +6,6 @@ using namespace std; int main() { - + task_1(); + task_2(); } \ No newline at end of file From c48fd6e269de29917de298ddf159a9e4d84a5510 Mon Sep 17 00:00:00 2001 From: artking4815162342 Date: Tue, 6 Oct 2015 23:03:37 +0300 Subject: [PATCH 4/8] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=B2=D1=81=D0=B5=20=D1=84=D1=83=D0=BD?= =?UTF-8?q?=D0=BA=D1=86=D0=B8=D0=B8=20=D1=84=D0=B0=D0=B9=D0=BB=D0=B0=20arr?= =?UTF-8?q?ay.cpp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- arrays.cpp | 29 ++++++++++++----------------- arrays.h | 13 +++++++++++++ main.cpp | 20 ++++++++++++++++++++ 3 files changed, 45 insertions(+), 17 deletions(-) diff --git a/arrays.cpp b/arrays.cpp index 4574915..47f1617 100644 --- a/arrays.cpp +++ b/arrays.cpp @@ -1,8 +1,6 @@ #include #include -using namespace std; - void task_1() { /* Task 1: find a bug in the following declaration. @@ -30,7 +28,7 @@ int task_3_zero() // Task 3 // Hint 1: fix with initialization (not assignment) // Hint 2: you can use only one zero and not five of them! cf. task 2 - int a[5]; + int a[5] = { 0 }; assert(!a[4]); return a[4]; // don't touch this! @@ -40,7 +38,7 @@ void task_4() { // Task 4: asserts at the end must hold // Hint: fix with initialization (not assignment) - int a[5]; + int a[5]{1, 2, 3, 4, 5}; assert(a[0] == 1); assert(a[1] == 2); @@ -53,32 +51,29 @@ void task_4() // copy arr1 to arr2 void task_5_copy(int * arr1, int * arr2, int size) { - arr2 = arr1; + for (int i = 0; i < size; i++) + { + arr2[i] = arr1[i]; + } } // Task 6 // copy array `arr1` to array `arr2` of the same size -void task_6_poor_copy(int * arr1, int * arr2) +void task_6_poor_copy(int * arr1, int * arr2, int size) { - // Hint: something wrong here; test this from main() - for(int i = 0; i < sizeof(arr2); ++i) + for(int i = 0; i < size; ++i) { arr2[i] = arr1[i]; } } -/* // Task 7 // print array `arr` void task_7_print(int * arr, int size, char delim = ' ') { - // Hint: something wrong here; run this from main() - for(int i = 0; i <= size; ++i) + for(int i = 0; i < size; ++i) { - std::cout << a[i] << delim; + std::cout << arr[i] << delim; } -} -*/ - - - + std::cout << std::endl; +} \ No newline at end of file diff --git a/arrays.h b/arrays.h index b85d445..9431f93 100644 --- a/arrays.h +++ b/arrays.h @@ -5,4 +5,17 @@ void task_1(); void task_2(); +int task_3_zero(); + +void task_4(); + +// copy arr1 to arr2 +void task_5_copy(int * arr1, int * arr2, int size); + +// copy array `arr1` to array `arr2` of the same size +void task_6_poor_copy(int * arr1, int * arr2, int size); + +// print array `arr` +void task_7_print(int * arr, int size, char delim = ' '); + #endif \ No newline at end of file diff --git a/main.cpp b/main.cpp index 83730ab..86d5721 100644 --- a/main.cpp +++ b/main.cpp @@ -8,4 +8,24 @@ int main() { task_1(); task_2(); + task_3_zero(); + task_4(); + + int a[3]{1, 2, 3}; + int b[3]; + int c[3]; + + task_5_copy(a, b, 3); + assert(a[0] == 1); + assert(a[1] == 2); + assert(a[2] == 3); + + task_6_poor_copy(a, c, 3); + assert(c[0] == 1); + assert(c[1] == 2); + assert(c[2] == 3); + + task_7_print(a,3); + + system("pause"); } \ No newline at end of file From f55d0b095237574c8b9dcc1659edcf6ab4199dd7 Mon Sep 17 00:00:00 2001 From: artking4815162342 Date: Tue, 6 Oct 2015 23:40:23 +0300 Subject: [PATCH 5/8] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=BE=D1=81=D0=BD=D0=BE=D0=B2=D0=BD=D0=BE=D0=B9?= =?UTF-8?q?=20=D0=B1=D0=BB=D0=BE=D0=BA=20=D0=BA=D0=BE=D0=B4=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- count.cpp | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/count.cpp b/count.cpp index 9534515..04d30aa 100644 --- a/count.cpp +++ b/count.cpp @@ -1,5 +1,6 @@ #include #include +#include using namespace std; @@ -14,19 +15,25 @@ using namespace std; int main() { string src("12 plus 45 minus 39 is 18\n"); - int i, nwhite, nother; + int nwhite, nother; const int size = 10; - int ndigit[size]; + int ndigit[size]{0}; nwhite = nother = 0; char c; - while ((c = src[i++]) != EOF) - if (c >= '0' && c >= '9') + + for (int j = 0; j < src.length(); j ++) + { + c = src[j]; + + if (c >= '0' && c <= '9') ++ndigit[c - '0']; - else if (c == ' ' && c == '\n' && c == '\t') + else if (c == ' ' || c == '\n' || c == '\t') ++nwhite; else ++nother; + } + cout << "source string: " << src << endl << "digits ="; for (int i = 0; i < size; ++i) @@ -34,4 +41,6 @@ int main() cout << ", white space = " << nwhite << ", other = " << nother << endl; + + system("pause"); } From 4830fb602f2bc47d066e7d0465f605884c2ab73f Mon Sep 17 00:00:00 2001 From: artking4815162342 Date: Wed, 7 Oct 2015 00:00:20 +0300 Subject: [PATCH 6/8] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=BE=20=D0=BE=D1=82=D0=BE=D0=B1=D1=80=D0=B0=D0=B6=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D1=87=D0=B0=D1=81=D1=82=D0=BE=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- count.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/count.cpp b/count.cpp index 04d30aa..91af7b9 100644 --- a/count.cpp +++ b/count.cpp @@ -4,14 +4,6 @@ using namespace std; -/* count digits, white space, others */ - -/* TODO: (1) fix all errors; - * (2) add code for painting histogram in console, - * cf. picture in https://en.wikipedia.org/wiki/Histogram - * - */ - int main() { string src("12 plus 45 minus 39 is 18\n"); @@ -34,13 +26,21 @@ int main() ++nother; } - cout << "source string: " << src << endl << "digits ="; for (int i = 0; i < size; ++i) cout << " " << ndigit[i]; cout << ", white space = " << nwhite - << ", other = " << nother << endl; + << ", other = " << nother << endl << endl; + + cout << "table:\n"; + for (int i = 0; i < size; i++) + { + cout << "symbol " << i << " contains " << ndigit[i] << " times\n"; + } + + cout << "white space" << " contains " << nwhite << " times\n"; + cout << "others" << " contains " << nother << " times\n"; system("pause"); } From 74a18383c984a87bf868fe6dbb33ab24f4158480 Mon Sep 17 00:00:00 2001 From: artking4815162342 Date: Wed, 7 Oct 2015 00:45:42 +0300 Subject: [PATCH 7/8] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=BE=D1=81=D0=BD=D0=BE=D0=B2=D0=BD=D0=BE=D0=B9?= =?UTF-8?q?=20=D0=B1=D0=BB=D0=BE=D0=BA=20=D0=BA=D0=BE=D0=B4=D0=B0=20=D0=B1?= =?UTF-8?q?=D1=8B=D1=81=D1=82=D1=80=D0=BE=D0=B9=20=D1=81=D0=BE=D1=80=D1=82?= =?UTF-8?q?=D0=B8=D1=80=D0=BE=D0=B2=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- quick_sort.cpp | 43 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/quick_sort.cpp b/quick_sort.cpp index 0bbfc07..393ee76 100644 --- a/quick_sort.cpp +++ b/quick_sort.cpp @@ -1,28 +1,59 @@ #include - #include using std::rand; using std::swap; +using std::cout; // pivot - "опорный" элемент // partition - переупорядочивает элементы части массива, // заданной отрезком [left, right), так что в начале // следуют элементы меньшие pivot, а в конце - большие; // возвращает место начала блока элементов, больших pivot; -int * partition(int * left, int * right, int pivot) { +int * partition(int * left, int * right, int pivot) +{ int * store = left; // место для вставки элементов, меньших pivot for (int * p = left; p != right; ++p) - if (*p < pivot) + if (*p <= pivot) swap(*p, *store++); return store; } -void my_qsort(int * arr, int n) { +void my_qsort(int * arr, int n) +{ if (n <= 1) return; // массив в 1 или 0 элементов уже упорядочен int * pivotPtr = arr + rand() % n; // случайный выбор опорного элемента int newPivotIdx = partition(arr, arr + n, *pivotPtr) - arr; - my_qsort(arr, newPivotIdx + 1); - my_qsort(arr + newPivotIdx, n - (newPivotIdx + 1)); + my_qsort(arr, newPivotIdx); + my_qsort(arr + newPivotIdx, n - newPivotIdx); +} + +void println(int *a, int n) +{ + for (int i = 0; i < n; i++) + { + cout << a[i] << " "; + } + cout << "\n"; } + +int main() +{ + const int m = 9; + int a[m]{666, 1, 3, 2, 5, 4, 0,-666,99}; + println(a, m); + my_qsort(a, m); + println(a, m); + cout << "\n"; + + //int b[]{1,2,2,2,2,-666,6,7,8,46,4,3,2,22,999,0,-5}; + const int n = 5; + int b[n]{1,666,-666,0,2}; + println(b, n); + my_qsort(b,n); + println(b, n); + cout << "\n"; + + system("pause"); +} \ No newline at end of file From cb3be0d6f0a5d06ee757d7089d18e7bba4936fa9 Mon Sep 17 00:00:00 2001 From: artking4815162342 Date: Wed, 7 Oct 2015 00:46:52 +0300 Subject: [PATCH 8/8] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- quick_sort.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/quick_sort.cpp b/quick_sort.cpp index 393ee76..363df7b 100644 --- a/quick_sort.cpp +++ b/quick_sort.cpp @@ -41,17 +41,16 @@ void println(int *a, int n) int main() { const int m = 9; - int a[m]{666, 1, 3, 2, 5, 4, 0,-666,99}; + int a[m]{666, 1, 3, 2, 5, 4, 0, -666, 99}; println(a, m); my_qsort(a, m); println(a, m); cout << "\n"; - //int b[]{1,2,2,2,2,-666,6,7,8,46,4,3,2,22,999,0,-5}; const int n = 5; - int b[n]{1,666,-666,0,2}; + int b[n]{1, 666, -666, 0, 2}; println(b, n); - my_qsort(b,n); + my_qsort(b, n); println(b, n); cout << "\n";