From 6867e6c3bc88355c8cbb604a5395bd1671fbd38c Mon Sep 17 00:00:00 2001 From: sarieved Date: Fri, 9 Oct 2015 00:02:31 +0300 Subject: [PATCH 01/27] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=B7=D0=B0=D0=B3=D0=BE=D0=BB=D0=BE=D0=B2=D0=BE?= =?UTF-8?q?=D1=87=D0=BD=D1=8B=D0=B9=20=D1=84=D0=B0=D0=B9=D0=BB=20arrays.h?= =?UTF-8?q?=20=D1=81=D0=BE=20=D1=81=D1=82=D1=80=D0=B0=D0=B6=D0=B0=D0=BC?= =?UTF-8?q?=D0=B8=20=D0=B2=D0=BA=D0=BB=D1=8E=D1=87=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- arrays.h | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 arrays.h diff --git a/arrays.h b/arrays.h new file mode 100644 index 0000000..8203709 --- /dev/null +++ b/arrays.h @@ -0,0 +1,7 @@ +#ifndef assert_h +#define assert_h + + + +//assert_h +#endif \ No newline at end of file From 9060640c057427705b6e65e0499b7f65c18a90af Mon Sep 17 00:00:00 2001 From: sarieved Date: Fri, 9 Oct 2015 00:03:20 +0300 Subject: [PATCH 02/27] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D1=84=D0=B0=D0=B9=D0=BB=20main?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.cpp | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 main.cpp diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..e69de29 From 9ec30c81197cc8801d5b1cf1bcf8cac38febdc46 Mon Sep 17 00:00:00 2001 From: sarieved Date: Fri, 9 Oct 2015 00:59:00 +0300 Subject: [PATCH 03/27] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B0=20=D1=84-=D0=B8=D1=8F=20task=5F1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Массив изменён на динамический, т.к. переменной длины --- arrays.cpp | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/arrays.cpp b/arrays.cpp index a2c541a..ed085d3 100644 --- a/arrays.cpp +++ b/arrays.cpp @@ -1,21 +1,13 @@ #include #include -#if 0 -// NOTE: you should move this `if` down as you start solving each task - void task_1() { - /* Task 1: find a bug in the following declaration. - * It should help you to compile this with: - * g++ -c -pedantic arrays.cpp - * pedantic flag here means: strictly as in language standard - * for explanation see: - * https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html*/ int n = 10; - int a[n]; + int * a = new int [n]; } +#if 0 // Task 2 void task_2() { From c86226d59ac9ac7d99509bc7dcabbad68ae58460 Mon Sep 17 00:00:00 2001 From: sarieved Date: Fri, 9 Oct 2015 01:07:18 +0300 Subject: [PATCH 04/27] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D1=91=D0=BD=20assert=20=D0=B2=20=D1=84-=D0=B8=D1=8E=20task=5F2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit assert(!a[4]) - потому что массив заполнен нулями, следовательно a[4] == 0 --- arrays.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/arrays.cpp b/arrays.cpp index ed085d3..55d0681 100644 --- a/arrays.cpp +++ b/arrays.cpp @@ -1,22 +1,23 @@ #include #include +#include "arrays.h" +// Task 1 void task_1() { int n = 10; int * a = new int [n]; } -#if 0 + // Task 2 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]); } +#if 0 // returns zero int task_3_zero() { From 9f170e729290d5272bceaa7b815834d0862a1b6d Mon Sep 17 00:00:00 2001 From: sarieved Date: Fri, 9 Oct 2015 01:12:23 +0300 Subject: [PATCH 05/27] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B0=20=D1=84-=D0=B8=D1=8F=20task=5F3=5Fzer?= =?UTF-8?q?o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Массив инициализирован нулями --- arrays.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/arrays.cpp b/arrays.cpp index 55d0681..d55644e 100644 --- a/arrays.cpp +++ b/arrays.cpp @@ -17,19 +17,16 @@ void task_2() assert(!a[4]); } -#if 0 + // returns zero 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! } +#if 0 void task_4() { // Task 4: asserts at the end must hold From 798e21a0b511703090cd4b7b8090a202cb51e86d Mon Sep 17 00:00:00 2001 From: sarieved Date: Fri, 9 Oct 2015 01:16:12 +0300 Subject: [PATCH 06/27] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B0=20=D1=84-=D0=B8=D1=8F=20task=5F4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Массив инициализирован цифрами от 1 до 5 --- arrays.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/arrays.cpp b/arrays.cpp index d55644e..6450752 100644 --- a/arrays.cpp +++ b/arrays.cpp @@ -26,12 +26,10 @@ int task_3_zero() return a[4]; // don't touch this! } -#if 0 +// Task 4 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); @@ -40,6 +38,7 @@ void task_4() assert(a[4] == 5); } +#if 0 // Task 5 // copy arr1 to arr2 void task_5_copy(int * arr1, int * arr2, int size) From ba9e5b8615d28ade4e12bf7ca3275f034850181e Mon Sep 17 00:00:00 2001 From: sarieved Date: Fri, 9 Oct 2015 01:24:29 +0300 Subject: [PATCH 07/27] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B0=20=D1=84-=D0=B8=D1=8F=20task=5F5=5Fcop?= =?UTF-8?q?y?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Массив должен копироваться поэлементно --- arrays.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/arrays.cpp b/arrays.cpp index 6450752..b1c0eef 100644 --- a/arrays.cpp +++ b/arrays.cpp @@ -38,14 +38,15 @@ void task_4() assert(a[4] == 5); } -#if 0 + // Task 5 -// 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]; } +#if 0 // Task 6 // copy array `arr1` to array `arr2` of the same size void task_6_poor_copy(int * arr1, int * arr2) From 498d524944f8151c791f25287fbc0f57a8dd080f Mon Sep 17 00:00:00 2001 From: sarieved Date: Fri, 9 Oct 2015 01:29:07 +0300 Subject: [PATCH 08/27] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B0=20=D1=84-=D0=B8=D1=8F=20task=5F6=5Fpoo?= =?UTF-8?q?r=5Fcopy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Добавлен параметр size, хранящий длину массивов, т.к. sizeof(arr) будет всегда выдавать 4 (размер в байтах указателя на первый элемент массива) --- arrays.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/arrays.cpp b/arrays.cpp index b1c0eef..a6673dd 100644 --- a/arrays.cpp +++ b/arrays.cpp @@ -46,18 +46,17 @@ void task_5_copy(int * arr1, int * arr2, int size) arr2[i] = arr1[i]; } -#if 0 + // 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]; } } +#if 0 // Task 7 // print array `arr` void task_7_print(int * arr, int size, char delim = ' ') From d4a91cb95d3cc82c19eb43d74c0e98f0d040c0a0 Mon Sep 17 00:00:00 2001 From: sarieved Date: Fri, 9 Oct 2015 01:36:07 +0300 Subject: [PATCH 09/27] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B0=20=D1=84-=D0=B8=D1=8F=20task=5F7=5Fpri?= =?UTF-8?q?nt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1)Параметр ф-ии int * arr изменён на const int * arr, т.к. в ходе печати массив не должен изменяться 2)Значение по умолчанию переменной delim должно быть отображено только в заголовочном файле 3)Цикл выолняется пока i строго меньше size, иначе неизбежен выход за границы массива 4)В печати переменная a заменена на переменную arr, т.к. a - неопределена --- arrays.cpp | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/arrays.cpp b/arrays.cpp index a6673dd..7b26b7c 100644 --- a/arrays.cpp +++ b/arrays.cpp @@ -56,16 +56,12 @@ void task_6_poor_copy(int * arr1, int * arr2, int size) } } -#if 0 + // Task 7 -// print array `arr` -void task_7_print(int * arr, int size, char delim = ' ') +void task_7_print(const 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; } } - -#endif From db1d2b36a38c68cadc3dd5c836ac346e8608bcb0 Mon Sep 17 00:00:00 2001 From: sarieved Date: Fri, 9 Oct 2015 01:36:55 +0300 Subject: [PATCH 10/27] =?UTF-8?q?=D0=92=20=D0=B7=D0=B0=D0=B3=D0=BE=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=D0=BE=D1=87=D0=BD=D1=8B=D0=B9=20=D1=84=D0=B0=D0=B9?= =?UTF-8?q?=D0=BB=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D1=8B?= =?UTF-8?q?=20=D0=B7=D0=B0=D0=B3=D0=BE=D0=BB=D0=BE=D0=B2=D0=BA=D0=B8=20?= =?UTF-8?q?=D0=B2=D1=81=D0=B5=D1=85=20=D1=84-=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- arrays.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/arrays.h b/arrays.h index 8203709..81a6329 100644 --- a/arrays.h +++ b/arrays.h @@ -1,7 +1,26 @@ #ifndef assert_h #define assert_h +// Task 1 +void task_1(); +// Task 2 +void task_2(); + +// returns zero +int task_3_zero(); + +// Task 4 +void task_4(); + +// Task 5 +void task_5_copy(int * arr1, int * arr2, int size); + +// Task 6 +void task_6_poor_copy(int * arr1, int * arr2, int size); + +// Task 7 +void task_7_print(const int * arr, int size, char delim = ' '); //assert_h #endif \ No newline at end of file From 772aa9871b6175bf6e3cbe2bcd634c5ad632422a Mon Sep 17 00:00:00 2001 From: sarieved Date: Fri, 9 Oct 2015 01:48:38 +0300 Subject: [PATCH 11/27] =?UTF-8?q?=D0=92=20main.cpp=20=D0=B4=D0=BE=D0=B1?= =?UTF-8?q?=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B0=20=D0=B4=D0=B5=D0=BC=D0=BE?= =?UTF-8?q?=D0=BD=D1=81=D1=82=D1=80=D0=B0=D1=86=D0=B8=D1=8F=20=D1=80=D0=B0?= =?UTF-8?q?=D0=B1=D0=BE=D1=82=D1=8B=20=D0=B2=D1=81=D0=B5=D1=85=20=D1=84-?= =?UTF-8?q?=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/main.cpp b/main.cpp index e69de29..7c4a3e8 100644 --- a/main.cpp +++ b/main.cpp @@ -0,0 +1,27 @@ +#include +#include "arrays.h" + +int main() +{ + task_1(); // - task_1 + + task_2(); // - task_2 + + task_3_zero(); // - task_3_zero + + task_4(); // - task_4 + + int a1[5]{ 1, 0, -5, 3, 10 }; + int a2[5]{ 0 }; + task_5_copy(a1, a2, 5); // a1 2 - task_5_copy + + task_7_print(a2, 5); // - task_7_print + + std::cout << std::endl; // + + int b1[3]{ 3, 5, -2 }; + int b2[3]; + task_6_poor_copy(b1, b2, 3); // b1 b2 - task_6_poor_copy + + task_7_print(b2, 3); // - task_7_print +} \ No newline at end of file From e609c6337f8e846153ce968dafae08a3b949e9a2 Mon Sep 17 00:00:00 2001 From: sarieved Date: Fri, 9 Oct 2015 02:12:50 +0300 Subject: [PATCH 12/27] =?UTF-8?q?count.cpp:=20=D0=9F=D0=BE=D0=B4=D0=BA?= =?UTF-8?q?=D0=BB=D1=8E=D1=87=D0=B5=D0=BD=20=D0=B7=D0=B0=D0=B3=D0=BE=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=D0=BE=D1=87=D0=BD=D1=8B=D0=B9=20=D1=84=D0=B0=D0=B9?= =?UTF-8?q?=D0=BB=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- count.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/count.cpp b/count.cpp index 9534515..2ab6c5a 100644 --- a/count.cpp +++ b/count.cpp @@ -1,3 +1,4 @@ +#include #include #include From 64347f2a54d0400904083084c958eed9290a55e0 Mon Sep 17 00:00:00 2001 From: sarieved Date: Fri, 9 Oct 2015 02:14:36 +0300 Subject: [PATCH 13/27] =?UTF-8?q?count.cpp:=20=D0=98=D0=BD=D0=B8=D1=86?= =?UTF-8?q?=D0=B8=D0=B0=D0=BB=D0=B8=D0=B7=D0=B8=D1=80=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BC=D0=B5=D0=BD=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=20i=20(=D0=BD=D1=83=D0=BB=D1=91=D0=BC)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- count.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/count.cpp b/count.cpp index 2ab6c5a..2a57db3 100644 --- a/count.cpp +++ b/count.cpp @@ -18,7 +18,7 @@ int main() int i, nwhite, nother; const int size = 10; int ndigit[size]; - nwhite = nother = 0; + nwhite = nother = i = 0; char c; while ((c = src[i++]) != EOF) From e743a8f6f16a2af8b31c72890c123b29e9377b9f Mon Sep 17 00:00:00 2001 From: sarieved Date: Fri, 9 Oct 2015 02:17:03 +0300 Subject: [PATCH 14/27] =?UTF-8?q?count.cpp:=20=D0=98=D0=BD=D0=B8=D1=86?= =?UTF-8?q?=D0=B8=D0=B0=D0=BB=D0=B8=D0=B7=D0=B8=D1=80=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D0=BD=20=D0=BC=D0=B0=D1=81=D1=81=D0=B8=D0=B2=20ndigit=20(?= =?UTF-8?q?=D0=BD=D1=83=D0=BB=D1=8F=D0=BC=D0=B8)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- count.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/count.cpp b/count.cpp index 2a57db3..60a31c4 100644 --- a/count.cpp +++ b/count.cpp @@ -17,7 +17,7 @@ int main() string src("12 plus 45 minus 39 is 18\n"); int i, nwhite, nother; const int size = 10; - int ndigit[size]; + int ndigit[size]{ 0 }; nwhite = nother = i = 0; char c; From 644808afd4db5cd1e3bef4f3224157d0a73c64bd Mon Sep 17 00:00:00 2001 From: sarieved Date: Fri, 9 Oct 2015 02:19:34 +0300 Subject: [PATCH 15/27] =?UTF-8?q?count.cpp:=20=D0=98=D0=B7=D0=BC=D0=B5?= =?UTF-8?q?=D0=BD=D0=B5=D0=BD=D0=BE=20=D1=83=D1=81=D0=BB=D0=BE=D0=B2=D0=B8?= =?UTF-8?q?=D0=B5=20=D1=86=D0=B8=D0=BA=D0=BB=D0=B0=20while?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Выход, когда встречен конец строки, то есть \0 --- count.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/count.cpp b/count.cpp index 60a31c4..6095d1a 100644 --- a/count.cpp +++ b/count.cpp @@ -21,7 +21,7 @@ int main() nwhite = nother = i = 0; char c; - while ((c = src[i++]) != EOF) + while ((c = src[i++]) != '\0') if (c >= '0' && c >= '9') ++ndigit[c - '0']; else if (c == ' ' && c == '\n' && c == '\t') From db346707d2d3bf7ffc3d1ce70c9aadd6f3ba2d98 Mon Sep 17 00:00:00 2001 From: sarieved Date: Fri, 9 Oct 2015 02:21:08 +0300 Subject: [PATCH 16/27] =?UTF-8?q?count.cpp:=20=D0=98=D1=81=D0=BF=D1=80?= =?UTF-8?q?=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=20=D1=83=D1=81=D0=BB=D0=BE=D0=B2?= =?UTF-8?q?=D0=BD=D1=8B=D0=B9=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE?= =?UTF-8?q?=D1=80=20if?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Символ должен быть в границах [0, 9] --- count.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/count.cpp b/count.cpp index 6095d1a..c1e2725 100644 --- a/count.cpp +++ b/count.cpp @@ -22,7 +22,7 @@ int main() char c; while ((c = src[i++]) != '\0') - if (c >= '0' && c >= '9') + if (c >= '0' && c <= '9') ++ndigit[c - '0']; else if (c == ' ' && c == '\n' && c == '\t') ++nwhite; From 97549cc72b307b8174cce6c37a5d15aa855285cc Mon Sep 17 00:00:00 2001 From: sarieved Date: Fri, 9 Oct 2015 02:29:52 +0300 Subject: [PATCH 17/27] =?UTF-8?q?count.cpp:=20=D0=98=D1=81=D0=BF=D1=80?= =?UTF-8?q?=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B0=20=D0=B2=D0=B5=D1=82=D0=BA?= =?UTF-8?q?=D0=B0=20else=20=D1=83=D1=81=D0=BB=D0=BE=D0=B2=D0=BD=D0=BE?= =?UTF-8?q?=D0=B3=D0=BE=20=D0=BE=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80?= =?UTF-8?q?=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Счётчик nwhite должен увеличиваться в любом из указанных вариантов разделителей --- count.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/count.cpp b/count.cpp index c1e2725..0a6ed0e 100644 --- a/count.cpp +++ b/count.cpp @@ -24,7 +24,7 @@ int main() while ((c = src[i++]) != '\0') 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; From 4bd00a040fa4f92825d6ee01ef87a8eb1d86908c Mon Sep 17 00:00:00 2001 From: sarieved Date: Fri, 9 Oct 2015 03:07:23 +0300 Subject: [PATCH 18/27] =?UTF-8?q?count.cpp:=20=D0=94=D0=BE=D0=B1=D0=B0?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=BD=D0=B0=20=D0=B3=D0=B8=D1=81=D1=82=D0=BE?= =?UTF-8?q?=D0=B3=D1=80=D0=B0=D0=BC=D0=BC=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- count.cpp | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/count.cpp b/count.cpp index 0a6ed0e..aaae34e 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"); @@ -35,4 +27,29 @@ int main() cout << ", white space = " << nwhite << ", other = " << nother << endl; + + //Histogram + + cout << endl << "Histogram:" << endl << endl; + + //digits + for (int j = 0; j < size; ++j) + { + cout << " " << j << ":"; + for (int k = 0; k < ndigit[j]; ++k) + cout << "#"; + cout << endl; + } + + //spaces + cout << "Spaces:"; + for (int j = 0; j < nwhite; ++j) + cout << "#"; + cout << endl; + + //others + cout << "Others:"; + for (int j = 0; j < nother; ++j) + cout << "#"; + cout << endl << endl; } From eda2461094783f98ebce05e01c6a0c6a2ff4ac28 Mon Sep 17 00:00:00 2001 From: sarieved Date: Fri, 9 Oct 2015 03:46:54 +0300 Subject: [PATCH 19/27] quick_sort.cpp: int pivot -> int * pivot MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Мы должны передавать указатель на "опорный" элемент --- quick_sort.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/quick_sort.cpp b/quick_sort.cpp index 0bbfc07..496040e 100644 --- a/quick_sort.cpp +++ b/quick_sort.cpp @@ -1,6 +1,6 @@ #include - #include +#include "quick_sort.h" using std::rand; using std::swap; @@ -10,7 +10,7 @@ using std::swap; // заданной отрезком [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) From 3d2ab957effc5a6f0a964bb3f3a4ad757c01cb3c Mon Sep 17 00:00:00 2001 From: sarieved Date: Fri, 9 Oct 2015 03:55:13 +0300 Subject: [PATCH 20/27] =?UTF-8?q?quick=5Fsort.cpp:=20=D0=9C=D0=B5=D0=BD?= =?UTF-8?q?=D1=8F=D0=B5=D0=BC=20=D0=BC=D0=B5=D1=81=D1=82=D0=B0=D0=BC=D0=B8?= =?UTF-8?q?=20=D1=8D=D0=BB=D0=B5=D0=BC=D0=B5=D0=BD=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Меняем местами опорный и крайний правый элементы --- quick_sort.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/quick_sort.cpp b/quick_sort.cpp index 496040e..8eb6245 100644 --- a/quick_sort.cpp +++ b/quick_sort.cpp @@ -12,6 +12,7 @@ using std::swap; // возвращает место начала блока элементов, больших pivot; int * partition(int * left, int * right, int * pivot) { int * store = left; // место для вставки элементов, меньших pivot + swap(*pivot, *right); // меняем местами опорный и крайний правый элементы for (int * p = left; p != right; ++p) if (*p < pivot) swap(*p, *store++); From 23cffcd59103712e39844496f34fb830166da10f Mon Sep 17 00:00:00 2001 From: sarieved Date: Fri, 9 Oct 2015 03:59:59 +0300 Subject: [PATCH 21/27] =?UTF-8?q?quick=5Fsort.cpp:=20=D0=9C=D0=B5=D0=BD?= =?UTF-8?q?=D1=8F=D0=B5=D0=BC=20=D0=BC=D0=B5=D1=81=D1=82=D0=B0=D0=BC=D0=B8?= =?UTF-8?q?=20=D1=8D=D0=BB=D0=B5=D0=BC=D0=B5=D0=BD=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ставим опорный элемент на место первого элемента, большего опорного --- quick_sort.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/quick_sort.cpp b/quick_sort.cpp index 8eb6245..e38e75d 100644 --- a/quick_sort.cpp +++ b/quick_sort.cpp @@ -14,8 +14,9 @@ int * partition(int * left, int * right, int * pivot) { int * store = left; // место для вставки элементов, меньших pivot swap(*pivot, *right); // меняем местами опорный и крайний правый элементы for (int * p = left; p != right; ++p) - if (*p < pivot) + if (*p < *pivot) swap(*p, *store++); + swap(*store, *right); return store; } From 548d06d8ee4db0ac1bf011014bf8ca2c0c4bf6f1 Mon Sep 17 00:00:00 2001 From: sarieved Date: Fri, 9 Oct 2015 04:02:19 +0300 Subject: [PATCH 22/27] quick_sort.cpp: *pivotPtr -> pivotPtr MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Мы должны передать указатель, а не значение --- quick_sort.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/quick_sort.cpp b/quick_sort.cpp index e38e75d..8550c56 100644 --- a/quick_sort.cpp +++ b/quick_sort.cpp @@ -24,7 +24,7 @@ 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; + int newPivotIdx = partition(arr, arr + n, pivotPtr) - arr; my_qsort(arr, newPivotIdx + 1); my_qsort(arr + newPivotIdx, n - (newPivotIdx + 1)); } From 168afa546c8fc0e5d6dcedad3d7174073e2ca4ff Mon Sep 17 00:00:00 2001 From: sarieved Date: Fri, 9 Oct 2015 04:04:06 +0300 Subject: [PATCH 23/27] =?UTF-8?q?quick=5Fsort.cpp:=20=D0=9F=D1=80=D0=B5?= =?UTF-8?q?=D0=B4=D1=83=D0=BF=D1=80=D0=B5=D0=B6=D0=B4=D0=B0=D0=B5=D0=BC=20?= =?UTF-8?q?=D0=B2=D1=8B=D1=85=D0=BE=D0=B4=20=D0=B7=D0=B0=20=D0=B3=D1=80?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D1=86=D1=8B=20=D0=BC=D0=B0=D1=81=D1=81=D0=B8?= =?UTF-8?q?=D0=B2=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- quick_sort.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/quick_sort.cpp b/quick_sort.cpp index 8550c56..37c699c 100644 --- a/quick_sort.cpp +++ b/quick_sort.cpp @@ -24,7 +24,7 @@ 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; + int newPivotIdx = partition(arr, arr + n - 1, pivotPtr) - arr; my_qsort(arr, newPivotIdx + 1); my_qsort(arr + newPivotIdx, n - (newPivotIdx + 1)); } From c639333a17cebdd720cd28fcb24068c6474ce77c Mon Sep 17 00:00:00 2001 From: sarieved Date: Fri, 9 Oct 2015 04:10:17 +0300 Subject: [PATCH 24/27] =?UTF-8?q?quick=5Fsort.cpp:=20=D0=A4-=D0=B8=D1=8F?= =?UTF-8?q?=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- quick_sort.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/quick_sort.cpp b/quick_sort.cpp index 37c699c..a24dc57 100644 --- a/quick_sort.cpp +++ b/quick_sort.cpp @@ -25,6 +25,6 @@ void my_qsort(int * arr, int n) { return; // массив в 1 или 0 элементов уже упорядочен int * pivotPtr = arr + rand() % n; // случайный выбор опорного элемента int newPivotIdx = partition(arr, arr + n - 1, pivotPtr) - arr; - my_qsort(arr, newPivotIdx + 1); + my_qsort(arr, newPivotIdx); my_qsort(arr + newPivotIdx, n - (newPivotIdx + 1)); } From 7b4b0182afc8c61ade3d00815f62668522499cce Mon Sep 17 00:00:00 2001 From: sarieved Date: Fri, 9 Oct 2015 04:11:12 +0300 Subject: [PATCH 25/27] =?UTF-8?q?quick=5Fsort.h:=20=D0=94=D0=BE=D0=B1?= =?UTF-8?q?=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=20=D0=B7=D0=B0=D0=B3=D0=BE=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=D0=BE=D1=87=D0=BD=D1=8B=D0=B9=20=D1=84=D0=B0=D0=B9?= =?UTF-8?q?=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- quick_sort.h | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 quick_sort.h diff --git a/quick_sort.h b/quick_sort.h new file mode 100644 index 0000000..1358bb8 --- /dev/null +++ b/quick_sort.h @@ -0,0 +1,9 @@ +#ifndef quick_sort_h +#define quick_sort_h + +int * partition(int * left, int * right, int * pivot); + +void my_qsort(int * arr, int n); + +//quick_sort_h +#endif \ No newline at end of file From 238981299832ef1b1082dfca0a45966d72daa49d Mon Sep 17 00:00:00 2001 From: sarieved Date: Fri, 9 Oct 2015 04:24:09 +0300 Subject: [PATCH 26/27] =?UTF-8?q?quick=5Fsort.cpp:=20=D0=A3=D1=81=D1=82?= =?UTF-8?q?=D1=80=D0=B0=D0=BD=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BD=D0=B5=D0=BF?= =?UTF-8?q?=D0=BE=D0=BB=D0=B0=D0=B4=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- quick_sort.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/quick_sort.cpp b/quick_sort.cpp index a24dc57..eb408db 100644 --- a/quick_sort.cpp +++ b/quick_sort.cpp @@ -12,9 +12,10 @@ using std::swap; // возвращает место начала блока элементов, больших pivot; int * partition(int * left, int * right, int * pivot) { int * store = left; // место для вставки элементов, меньших pivot + int val = *pivot; // сохранение значения swap(*pivot, *right); // меняем местами опорный и крайний правый элементы for (int * p = left; p != right; ++p) - if (*p < *pivot) + if (*p < val) swap(*p, *store++); swap(*store, *right); return store; @@ -26,5 +27,5 @@ void my_qsort(int * arr, int n) { int * pivotPtr = arr + rand() % n; // случайный выбор опорного элемента int newPivotIdx = partition(arr, arr + n - 1, pivotPtr) - arr; my_qsort(arr, newPivotIdx); - my_qsort(arr + newPivotIdx, n - (newPivotIdx + 1)); + my_qsort(arr + newPivotIdx + 1, n - (newPivotIdx + 1)); } From 568e8b46c395a30548281f2481e8a6490ede3c96 Mon Sep 17 00:00:00 2001 From: sarieved Date: Fri, 9 Oct 2015 04:27:47 +0300 Subject: [PATCH 27/27] =?UTF-8?q?quick=5Fsort.cpp:=20=D0=94=D0=BE=D0=B1?= =?UTF-8?q?=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=20=D1=84=D0=B0=D0=B9=D0=BB=20mai?= =?UTF-8?q?nsq.cpp=20=D1=81=20=D1=82=D0=B5=D1=81=D1=82=D0=B0=D0=BC=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mainqs.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 mainqs.cpp diff --git a/mainqs.cpp b/mainqs.cpp new file mode 100644 index 0000000..8811251 --- /dev/null +++ b/mainqs.cpp @@ -0,0 +1,42 @@ +#include +#include "quick_sort.h" + +void print(const int * arr, int size, char delim = ' ') +{ + for (int i = 0; i < size; ++i) + { + std::cout << arr[i] << delim; + } +} + +int main() +{ + // + + int a[5]{ 3, 2, 1, 0, 5 }; + my_qsort(a, 5); // 0 1 2 3 5 + print(a, 5); + + std::cout << std::endl; + + int b[3]{ 1, 6, 8 }; + my_qsort(b, 3); // 1 6 8 + print(b, 3); + + std::cout << std::endl; + + int c[1]{ 4 }; + my_qsort(c, 1); // 4 + print(c, 1); + + std::cout << std::endl; +} + +/* + : + +0 1 2 3 5 +1 6 8 +4 + +*/ \ No newline at end of file