Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int count_any_fun = 5;
- void any_fun() {
- static int count_any_fun = 0;
- // global
- ::count_any_fun++;
- cout << "count: " << ::count_any_fun << endl;
- // local-static
- count_any_fun++;
- cout << "count: " << count_any_fun << endl;
- }
- void task_static() {
- any_fun();
- any_fun();
- any_fun();
- }
- template<class T>
- void show_arr(int size, T* arr) {
- for (int k = 0; k < size; k++)
- cout << arr[k] << " ";
- cout << endl;
- }
- template<class T>
- int get_size(T* arr) {
- if (arr)
- return _msize(arr) / sizeof(arr[0]);
- return 0;
- }
- void task_dynamic_array() {
- int psa[]{ 1,2,3 };
- int* pda = new int[] {1,2,3};
- cout << "static : " << psa << endl;
- cout << "dynamic: " << pda << endl;
- cout << "fun : " << any_fun << endl;
- cout << "global : " << &count_any_fun << endl;
- show_arr(get_size(pda), pda);
- show_arr(size(psa), psa);
- delete[] pda;
- }
- /// <summary>
- /// Copy to res all odd nums from array arr.
- /// </summary>
- /// <param name="size">Size</param>
- /// <param name="arr">Input array</param>
- /// <param name="res">Output array</param>
- int* odd_nums(int size, int* arr) {
- int n = 0;
- for (int k = 0; k < size; k++) {
- if (arr[k] % 2 == 0) {
- n++;
- }
- }
- int* res = new int[n];
- n = 0;
- for (int k = 0; k < size; k++) {
- if (arr[k] % 2 == 0) {
- res[n] = arr[k];
- n++;
- }
- }
- return res;
- }
- // In: {2,3,5,6,8,2,3,5,7,8,9}
- // {2,6,8,2,8}
- void test_odd_nums() {
- int arr[]{ 2, 3, 5, 6, 8, 2, 3, 5, 7, 8, 9 };
- int k = NULL;
- int* res = odd_nums(size(arr), arr);
- int out[]{ 2,6,8,2,8 };
- if (size(out) != get_size(res)) {
- cout << "BAD: size";
- return;
- }
- for (int k = 0; k < size(out); k++) {
- if (out[k] != res[k]) {
- cout << "BAD: value";
- return;
- }
- }
- cout << "OK";
- if(res)
- delete[] res;
- }
- int get_rand(int a, int b) {
- return rand() % (b - a + 1) + a;
- }
- void gen_arr(int size, int* arr) {
- for (int k = 0; k < size; k++) {
- arr[k] = get_rand(0, 10);
- }
- }
- void task_odd_nums() {
- test_odd_nums();
- cout << endl;
- int arr[100];
- srand(time(NULL));
- gen_arr(size(arr), arr);
- cout << "arr: ";
- show_arr(size(arr), arr);
- int* pres = odd_nums(size(arr), arr);
- cout << endl << "res: ";
- show_arr(get_size(pres), pres);
- cout << "size: " << get_size(pres) << endl;
- delete[] pres;
- }
- int main(){
- //task_dynamic_array();
- task_odd_nums();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement