Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- void test_myswap();
- void test_myswap_by_pc();
- void myswap(int& x, int& y);
- void test_checsign();
- bool checsign(int, int*, int&, int&);
- int sum(int a, int b) {
- return a + b;
- }
- void pointer_vs_constant() {
- /*
- c - const
- p - pointer
- r - reference
- */
- int x = 17;
- int y = 22;
- x = x + 12;
- cout << x << endl;
- const int cx = x;
- cout << cx << endl;
- x = x + 1000;
- cout << cx << endl;
- cout << x << endl;
- cout << "sum(x, cx) : " << sum(x, cx) << endl;
- const int* pcx = &cx;
- cout << "*pcx: " << *pcx << endl;
- const int cArr[]{5,6,7,8};
- const int* pca = cArr;
- //cArr[0] = 6;
- //*pca = 6;
- cout << "Const1 " << *pca << endl;
- pca = pca + 1;
- cout << "Const2 " << *pca << endl;
- pca = &x; // это замороченный момент
- const int* const cpcx = &(cArr[2]);
- //cpcx = cpcx + 1;
- //*cpcx = 888;
- int z = 33;
- int* const cpz = &z;
- cout << z << " " << *cpz << endl;
- //cpz++;
- *cpz = 44;
- cout << z << " " << *cpz << endl;
- z = 55;
- cout << z << " " << *cpz << endl;
- //*pcx = 14; - даже через разъименование нельзя
- //myswap(x, cx); - ссылка привела бы к изменению - так нельзя
- //cx++; - константы нельзя менять
- }
- int main() {
- //test_checsign();
- //test_myswap();
- //test_myswap_by_pc();
- pointer_vs_constant();
- return 0;
- }
- /// <summary>
- /// Count numbers of positive and negative elements in array
- /// </summary>
- /// <param name="size">A integer size of array</param>
- /// <param name="arr">A array of int</param>
- /// <param name="n_neg">Nuber of negative</param>
- /// <param name="n_pos">Number of positive</param>
- /// <returns>Is error in the function</returns>
- bool checsign(int size, // size
- int* arr, //
- int& n_neg, //
- int& n_pos //
- ) {
- n_neg = 0;
- n_pos = 0;
- for (int k = 0; k < size; k++) {
- if (arr[k] < 0) n_neg++;
- else if (arr[k] > 0) n_pos++;
- }
- return false;
- }
- void test_checsign() {
- int arr[]{ 1, 3, 0, -2, 1,-1, 12, 7 };
- int n_neg = 0;
- int n_pos = 0;
- bool is_err = checsign(size(arr), arr, n_neg, n_pos);
- if (is_err) {
- cout << "BAD error" << endl;
- }
- if (n_pos != 5) {
- cout << "BAD value positive wrong" << endl;
- }
- if (n_neg != 2) {
- cout << "BAD value negative wrong" << endl;
- }
- if (!is_err && (n_pos == 5) && (n_neg == 2)) {
- cout << "OK";
- }
- }
- void myswap4(int x, int y) {
- // BAD
- int temp = x;
- x = y;
- y = temp;
- cout << "In: " << x << " " << y << endl;
- }
- void myswap3(int* px, int* py) {
- // BAD
- int* temp = px;
- px = py;
- py = temp;
- cout << "In: " << *px << " " << *py << endl;
- }
- void myswap2(int* px, int* py) {
- // SO SO
- int temp = *px;
- *px = *py;
- *py = temp;
- cout << "In: " << *px << " " << *py << endl;
- }
- void myswap(int& x, int& y) {
- int temp = x;
- x = y;
- y = temp;
- cout << "In: " << x << " " << y << endl;
- }
- void myswap_alg(int& x, int& y) {
- x = x + y;
- y = x - y;
- x = x - y;
- }
- void test_myswap() {
- int x = 4;
- int y = 5;
- swap(x, y);
- cout << x << " " << y << endl;
- // out: 4 5
- myswap(x, y);
- cout << "Out: " << x << " " << y << endl;
- // out: 5 4
- }
- void test_myswap_by_pc() {
- int x = 4;
- int y = 5;
- cout << "Part 1: ";
- if (x == 4 && y == 5)
- cout << "OK" << endl;
- else
- cout << "BAD" << endl;
- myswap(x, y);
- cout << "Part 2: ";
- if (x == 5 && y == 4)
- cout << "OK" << endl;
- else
- cout << "BAD" << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment