Skip to content

css-211-arrays #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
6867e6c
Добавлен заголовочный файл arrays.h со стражами включения
sarieved Oct 8, 2015
9060640
Добавлен файл main
sarieved Oct 8, 2015
9ec30c8
Исправлена ф-ия task_1
sarieved Oct 8, 2015
c86226d
Добавлён assert в ф-ию task_2
sarieved Oct 8, 2015
9f170e7
Исправлена ф-ия task_3_zero
sarieved Oct 8, 2015
798e21a
Исправлена ф-ия task_4
sarieved Oct 8, 2015
ba9e5b8
Исправлена ф-ия task_5_copy
sarieved Oct 8, 2015
498d524
Исправлена ф-ия task_6_poor_copy
sarieved Oct 8, 2015
d4a91cb
Исправлена ф-ия task_7_print
sarieved Oct 8, 2015
db1d2b3
В заголовочный файл добавлены заголовки всех ф-ий
sarieved Oct 8, 2015
772aa98
В main.cpp добавлена демонстрация работы всех ф-ий
sarieved Oct 8, 2015
e609c63
count.cpp: Подключен заголовочный файл <iostream>
sarieved Oct 8, 2015
64347f2
count.cpp: Инициализирована переменная i (нулём)
sarieved Oct 8, 2015
e743a8f
count.cpp: Инициализирован массив ndigit (нулями)
sarieved Oct 8, 2015
644808a
count.cpp: Изменено условие цикла while
sarieved Oct 8, 2015
db34670
count.cpp: Исправлен условный оператор if
sarieved Oct 8, 2015
97549cc
count.cpp: Исправлена ветка else условного оператора
sarieved Oct 8, 2015
4bd00a0
count.cpp: Добавлена гистограмма
sarieved Oct 9, 2015
eda2461
quick_sort.cpp: int pivot -> int * pivot
sarieved Oct 9, 2015
3d2ab95
quick_sort.cpp: Меняем местами элементы
sarieved Oct 9, 2015
23cffcd
quick_sort.cpp: Меняем местами элементы
sarieved Oct 9, 2015
548d06d
quick_sort.cpp: *pivotPtr -> pivotPtr
sarieved Oct 9, 2015
168afa5
quick_sort.cpp: Предупреждаем выход за границы массива
sarieved Oct 9, 2015
c639333
quick_sort.cpp: Ф-ия исправлена
sarieved Oct 9, 2015
7b4b018
quick_sort.h: Добавлен заголовочный файл
sarieved Oct 9, 2015
2389812
quick_sort.cpp: Устранение неполадок
sarieved Oct 9, 2015
568e8b4
quick_sort.cpp: Добавлен файл mainsq.cpp с тестами
sarieved Oct 9, 2015
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
53 changes: 19 additions & 34 deletions arrays.cpp
Original file line number Diff line number Diff line change
@@ -1,47 +1,35 @@
#include <iostream>
#include <cassert>
#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);
Expand All @@ -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
26 changes: 26 additions & 0 deletions arrays.h
Original file line number Diff line number Diff line change
@@ -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
44 changes: 31 additions & 13 deletions count.cpp
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
#include <iostream>
#include <string>
#include <cstdio>

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;
Expand All @@ -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;
}
27 changes: 27 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <iostream>
#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
}
42 changes: 42 additions & 0 deletions mainqs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <iostream>
#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

*/
15 changes: 9 additions & 6 deletions quick_sort.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <iostream>

#include <cstdlib>
#include "quick_sort.h"

using std::rand;
using std::swap;
Expand All @@ -10,19 +10,22 @@ 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;
}

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));
}
9 changes: 9 additions & 0 deletions quick_sort.h
Original file line number Diff line number Diff line change
@@ -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