diff --git a/arrays.cpp b/arrays.cpp index a2c541a..7b26b7c 100644 --- a/arrays.cpp +++ b/arrays.cpp @@ -1,47 +1,35 @@ #include #include +#include "arrays.h" -#if 0 -// NOTE: you should move this `if` down as you start solving each task - +// Task 1 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]; } + // 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]); } + // 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! } +// 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); @@ -50,33 +38,30 @@ void task_4() assert(a[4] == 5); } + // 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]; } + // 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 = ' ') +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 diff --git a/arrays.h b/arrays.h new file mode 100644 index 0000000..81a6329 --- /dev/null +++ b/arrays.h @@ -0,0 +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 diff --git a/count.cpp b/count.cpp index 9534515..aaae34e 100644 --- a/count.cpp +++ b/count.cpp @@ -1,29 +1,22 @@ +#include #include #include 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"); int i, nwhite, nother; const int size = 10; - int ndigit[size]; - nwhite = nother = 0; + int ndigit[size]{ 0 }; + nwhite = nother = i = 0; char c; - while ((c = src[i++]) != EOF) - if (c >= '0' && c >= '9') + 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; @@ -34,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; } diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..7c4a3e8 --- /dev/null +++ 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 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 diff --git a/quick_sort.cpp b/quick_sort.cpp index 0bbfc07..eb408db 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,11 +10,14 @@ 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 + 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; } @@ -22,7 +25,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; - my_qsort(arr, newPivotIdx + 1); - my_qsort(arr + newPivotIdx, n - (newPivotIdx + 1)); + int newPivotIdx = partition(arr, arr + n - 1, pivotPtr) - arr; + my_qsort(arr, newPivotIdx); + my_qsort(arr + newPivotIdx + 1, n - (newPivotIdx + 1)); } 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