Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. int max(int* a, int n)
  4. {
  5.     int tmp = a[0], tmp_i = 0;
  6.     for(int i = 1; i < n; i++)
  7.     {
  8.         if(a[i] > tmp)
  9.         {
  10.             tmp = a[i];
  11.             tmp_i = i;
  12.         }
  13.     }
  14.    
  15.     return tmp_i;
  16. }
  17.  
  18. int main()
  19. {
  20.     int n_a, n_b;
  21.    
  22.     std::cout << "A[] size:\n";
  23.     std::cin >> n_a;
  24.     int *A = (int*)malloc(sizeof(int) * n_a);
  25.  
  26.     std::cout << "Enter A[]:\n";
  27.     for(int i = 0; i < n_a; i++)
  28.         std::cin >> A[i];
  29.        
  30.     std::cout << "B[] size:\n";
  31.     std::cin >> n_b;
  32.     int *B = (int*)malloc(sizeof(int) * n_b);
  33.  
  34.     std::cout << "Enter B[]:\n";
  35.     for(int i = 0; i < n_b; i++)
  36.         std::cin >> B[i];
  37.        
  38.  
  39.     int half = (!(n_a % 2)) ? n_a/2 : n_a/2 + 1;
  40.     int a_i = max(&A[half], half) + half;
  41.     std::cout << "Max of the second half of A[]: " << "A[" << a_i << "] = " << A[a_i] << std::endl;
  42.    
  43.     int third = n_b / 3;
  44.     int b_i = max(&B[third], third) + third;
  45.     std::cout << "Max of the second third of B[]: " << "B[" << b_i << "] = " << B[b_i];
  46.    
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement