File tree Expand file tree Collapse file tree 3 files changed +147
-0
lines changed Expand file tree Collapse file tree 3 files changed +147
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #include < cassert>
3
+
4
+ #if 0
5
+ // NOTE: you should move this `if` down as you start solving each task
6
+
7
+ void task_1()
8
+ {
9
+ /* Task 1: find a bug in the following declaration.
10
+ * It should help you to compile this with:
11
+ * g++ -c -pedantic arrays.cpp
12
+ * pedantic flag here means: strictly as in language standard
13
+ * for explanation see:
14
+ * https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html*/
15
+ int n = 10;
16
+ int a[n];
17
+ }
18
+
19
+ // Task 2
20
+ void task_2()
21
+ {
22
+ int a[5] = {0};
23
+
24
+ //assert(!a[4]) or assert(a[4]) -- place here one of these and
25
+ // explain your choice in commit msg
26
+ }
27
+
28
+ // returns zero
29
+ int task_3_zero()
30
+ {
31
+ // Task 3
32
+ // Hint 1: fix with initialization (not assignment)
33
+ // Hint 2: you can use only one zero and not five of them! cf. task 2
34
+ int a[5];
35
+
36
+ assert(!a[4]);
37
+ return a[4]; // don't touch this!
38
+ }
39
+
40
+ void task_4()
41
+ {
42
+ // Task 4: asserts at the end must hold
43
+ // Hint: fix with initialization (not assignment)
44
+ int a[5];
45
+
46
+ assert(a[0] == 1);
47
+ assert(a[1] == 2);
48
+ assert(a[2] == 3);
49
+ assert(a[3] == 4);
50
+ assert(a[4] == 5);
51
+ }
52
+
53
+ // Task 5
54
+ // copy arr1 to arr2
55
+ void task_5_copy(int * arr1, int * arr2, int size)
56
+ {
57
+ arr2 = arr1;
58
+ }
59
+
60
+ // Task 6
61
+ // copy array `arr1` to array `arr2` of the same size
62
+ void task_6_poor_copy(int * arr1, int * arr2)
63
+ {
64
+ // Hint: something wrong here; test this from main()
65
+ for(int i = 0; i < sizeof(arr2); ++i)
66
+ {
67
+ arr2[i] = arr1[i];
68
+ }
69
+ }
70
+
71
+ // Task 7
72
+ // print array `arr`
73
+ void task_7_print(int * arr, int size, char delim = ' ')
74
+ {
75
+ // Hint: something wrong here; run this from main()
76
+ for(int i = 0; i <= size; ++i)
77
+ {
78
+ std::cout << a[i] << delim;
79
+ }
80
+ }
81
+
82
+ #endif
Original file line number Diff line number Diff line change
1
+ #include < string>
2
+ #include < cstdio>
3
+
4
+ using namespace std ;
5
+
6
+ /* count digits, white space, others */
7
+
8
+ /* TODO: (1) fix all errors;
9
+ * (2) add code for painting histogram in console,
10
+ * cf. picture in https://en.wikipedia.org/wiki/Histogram
11
+ *
12
+ */
13
+
14
+ int main ()
15
+ {
16
+ string src (" 12 plus 45 minus 39 is 18\n " );
17
+ int i, nwhite, nother;
18
+ const int size = 10 ;
19
+ int ndigit[size];
20
+ nwhite = nother = 0 ;
21
+
22
+ char c;
23
+ while ((c = src[i++]) != EOF)
24
+ if (c >= ' 0' && c >= ' 9' )
25
+ ++ndigit[c - ' 0' ];
26
+ else if (c == ' ' && c == ' \n ' && c == ' \t ' )
27
+ ++nwhite;
28
+ else
29
+ ++nother;
30
+
31
+ cout << " source string: " << src << endl << " digits =" ;
32
+ for (int i = 0 ; i < size; ++i)
33
+ cout << " " << ndigit[i];
34
+
35
+ cout << " , white space = " << nwhite
36
+ << " , other = " << nother << endl;
37
+ }
Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+
3
+ #include < cstdlib>
4
+
5
+ using std::rand;
6
+ using std::swap;
7
+
8
+ // pivot - "опорный" элемент
9
+ // partition - переупорядочивает элементы части массива,
10
+ // заданной отрезком [left, right), так что в начале
11
+ // следуют элементы меньшие pivot, а в конце - большие;
12
+ // возвращает место начала блока элементов, больших pivot;
13
+ int * partition (int * left, int * right, int pivot) {
14
+ int * store = left; // место для вставки элементов, меньших pivot
15
+ for (int * p = left; p != right; ++p)
16
+ if (*p < pivot)
17
+ swap (*p, *store++);
18
+ return store;
19
+ }
20
+
21
+ void my_qsort (int * arr, int n) {
22
+ if (n <= 1 )
23
+ return ; // массив в 1 или 0 элементов уже упорядочен
24
+ int * pivotPtr = arr + rand () % n; // случайный выбор опорного элемента
25
+ int newPivotIdx = partition (arr, arr + n, *pivotPtr) - arr;
26
+ my_qsort (arr, newPivotIdx + 1 );
27
+ my_qsort (arr + newPivotIdx, n - (newPivotIdx + 1 ));
28
+ }
You can’t perform that action at this time.
0 commit comments