Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
54 changes: 21 additions & 33 deletions arrays.cpp
Original file line number Diff line number Diff line change
@@ -1,47 +1,38 @@
#include <iostream>
#include <cassert>
#include "arrays.h"


#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 const 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]); // ��������� ������ ��������������� ����
}

// 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!
}


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 @@ -52,31 +43,28 @@ void task_4()

// Task 5
// copy arr1 to arr2
void task_5_copy(int * arr1, int * arr2, int size)
void task_5_copy(const 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(const int * arr1, int * arr2, int size)
{
// Hint: something wrong here; test this from main()
for(int i = 0; i < sizeof(arr2); ++i)
{
arr2[i] = arr1[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)
{
std::cout << a[i] << delim;
}
}
for (int i = 0; i < size; i++)
{
std::cout << arr[i] << delim;
}


#endif
}
18 changes: 18 additions & 0 deletions arrays.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef arrays
#define arrays

void task_1();

void task_2();

int task_3_zero();

void task_4();

void task_5_copy(const int * arr1, int * arr2, int size);

void task_6_poor_copy(const int * arr1, int * arr2, int size);

void task_7_print(const int * arr, int size, char delim = ' ');

#endif
34 changes: 28 additions & 6 deletions count.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <string>
#include <cstdio>
#include <iostream>

using namespace std;

Expand All @@ -16,22 +17,43 @@ 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 = 0;
i = 0;

char c;
while ((c = src[i++]) != EOF)
if (c >= '0' && c >= '9')
while ((c = src[i++]) != '\n')
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 =";
cout << "source string: " << src << endl << "digits = ";
for (int i = 0; i < size; ++i)
cout << " " << ndigit[i];
cout << ndigit[i] << " ";

cout << ", white space = " << nwhite
<< ", other = " << nother << endl;

// ������ �����������
cout << endl << "white space: ";
for (int j = 0; j < nwhite; j++)
cout << "=";
cout << endl;

cout << "others: ";
for (int j = 0; j < nother; j++)
cout << "=";
cout << endl;

for (int i = 0; i < size; i++)
{
cout << i << ": ";
for (int j = 0; j < ndigit[i]; j++)
cout << "=";
cout << endl;
}
system("pause");
}
30 changes: 30 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <locale>
#include <cassert>
#include <iostream>
#include <cstdio>
#include "arrays.h"

using namespace std;

int main()
{
task_1();

task_2();

assert(0 == task_3_zero());

task_4();

int a[3]{1, 2, 3}; // ���� task_6() ������������ �������, �.�. ������� ���������
int b[3] = { 0 };
task_5_copy(a, b, 3);
assert(a[0] == b[0]);
assert(a[1] == b[1]);
assert(a[2] == b[2]);

task_7_print(b, 3);

system("pause");

}