Advertisement
valenki13

lesson_321_16

Sep 18th, 2023
851
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.08 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void show_word(int size, char vects[]);
  6.  
  7. void test_concat() {
  8.     char word1[]{ 'a', 'b', 'c' };
  9.     char word2[]{ 'd', 'e','f', 'g' };
  10.  
  11.     char word3[1024];
  12.  
  13.     int size1 = sizeof(word1) / sizeof(word1[0]);
  14.     int size2 = sizeof(word2) / sizeof(word2[0]);
  15.     int size3 = size1 + size2;
  16.  
  17.     for(int k = 0; k < size1; k++) {
  18.         word3[k] = word1[k];
  19.     }
  20.  
  21.     for (int k = 0; k < size2; k++) {
  22.         word3[k+size1] = word2[k];
  23.     }
  24. /*
  25.     for (int k = size1; k < size1 + size2; k++) {
  26.         word3[k] = word2[k-size1];
  27.     }
  28. */
  29.     show_word(size1, word1);
  30.     show_word(size2, word2);
  31.     show_word(size3, word3);
  32. }
  33.  
  34.  
  35. int get_rand(int a, int b) {
  36.     return rand() % (b - a + 1) + a;
  37. }
  38.  
  39. void show_word(int size, char vects[]) {
  40.     for (int k = 0; k < size; k++) {
  41.         cout << vects[k];
  42.     }
  43.     cout << ' ';
  44. }
  45.  
  46.  
  47. void show_arr(int size, int vects[]) {
  48.     for (int k = 0; k < size; k++) {
  49.         cout << vects[k] << "\t";
  50.     }
  51.     cout << endl;
  52. }
  53.  
  54. void gen_arr(int size, int vects[]) {
  55.     for (int k = 0; k < size; k++) {
  56.         vects[k] = get_rand(-10, 10);
  57.     }
  58. }
  59.  
  60. int sum_arr(int size, int vects[]) {
  61.     int sum{ 0 }; // оператор универсального присваивания для переменных
  62.     for (int k = 0; k < size; k++) {
  63.         sum += vects[k];
  64.     }
  65.     return sum;
  66. }
  67.  
  68. void test0() {
  69.     const int size = 8;
  70.     int vects[size]{ 17, 20, 23 };
  71.     int vects2[size];
  72.     int summ_vect[size];
  73.  
  74.     show_arr(size, vects);
  75.     cout << "summ: " << sum_arr(size, vects) << endl;
  76.  
  77.     gen_arr(size, vects);
  78.     gen_arr(size, vects2);
  79.  
  80.     for (int k = 0; k < size; k++) {
  81.         summ_vect[k] = vects[k] + vects2[k];
  82.     }
  83.  
  84.     show_arr(size, vects);
  85.     show_arr(size, vects2);
  86.     show_arr(size, summ_vect);
  87. }
  88.  
  89. void test1() {
  90.     // оределение кол-ва элементов в массиве
  91.     int arr[]{ 2,4345, 54, 54, 23, 32, 54, 43, 35, 34 };
  92.     int x = 7;
  93.     double y = 14.6;
  94.     cout << sizeof(x) << endl;
  95.     cout << sizeof(y) << endl;
  96.     cout << sizeof(arr) << endl;
  97.     cout << sizeof(arr) / sizeof(arr[0]) << endl;
  98. }
  99.  
  100.  
  101.  
  102. int main() {
  103.     srand((int)time(NULL));
  104.     test_concat();
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement