Skip to content
Open
35 changes: 16 additions & 19 deletions arrays.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#include <iostream>
#include <cassert>

#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.
Expand All @@ -12,17 +9,17 @@ 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];
}

// 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]);
//place here one of these and
//explain your choice in commit msg
}

// returns zero
Expand All @@ -31,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!
Expand All @@ -41,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);
Expand All @@ -54,15 +51,17 @@ 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];
}
Expand All @@ -72,11 +71,9 @@ void task_6_poor_copy(int * arr1, int * arr2)
// 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;
}
}

#endif
std::cout << std::endl;
}
21 changes: 21 additions & 0 deletions arrays.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef _ARRAYS
#define _ARRAYS

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
37 changes: 23 additions & 14 deletions count.cpp
Original file line number Diff line number Diff line change
@@ -1,37 +1,46 @@
#include <string>
#include <cstdio>
#include <iostream>

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;
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)
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");
}
31 changes: 31 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <iostream>
#include <cassert>
#include "arrays.h"

using namespace std;

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");
}
42 changes: 36 additions & 6 deletions quick_sort.cpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,58 @@
#include <iostream>

#include <cstdlib>

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";

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");
}