Advertisement
MariuszPienkosz

mixing the sorted arrays

Nov 23rd, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | None | 0 0
  1. //Zad18-18c-s13810
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. void mix(int* a, int a_size, int* b, int b_size);
  7. void swap_t(int* a, int a_ind, int* b, int b_ind);
  8. void show_tab(int *tab, int tab_size);
  9.  
  10. void mix(int* a, int a_size, int* b, int b_size){
  11.  
  12.     for(int i=0; i<b_size; i++){
  13.         for(int j=0; j<a_size; j++){
  14.             if(*(a+j) > *(b+i))
  15.                 swap_t(a, j, b, i);
  16.         }
  17.     }
  18.  
  19.     for(int i=0; i<b_size; i++){
  20.         for(int j=0; j<b_size; j++){
  21.             if(*(b+j) > *(b+i))
  22.                 swap_t(b, j, b, i);
  23.         }
  24.     }
  25. }
  26.  
  27. void swap_t(int* a, int a_ind, int* b, int b_ind){
  28.     int temp = *(a+a_ind);
  29.     *(a+a_ind) = *(b+b_ind);
  30.     *(b+b_ind) = temp;
  31. }
  32.  
  33. void show_tab(int *tab, int tab_size){
  34.     cout << "{";
  35.     for(int i=0; i<tab_size; i++){
  36.         cout << *(tab++);
  37.         if(i != tab_size -1)
  38.             cout << " , ";
  39.     }
  40.     cout << "}";
  41. }
  42.  
  43. int main()
  44. {
  45.     int a[] = {1, 2, 2, 3, 5, 6};
  46.     int b[] = {2, 4, 5, 8};
  47.  
  48.     int a_size = 6;
  49.     int b_size = 4;
  50.  
  51.     cout << "Przed miksowaniem" << endl;
  52.     cout << "a = ";
  53.     show_tab(a, a_size);
  54.     cout << "\nb = ";
  55.     show_tab(b, b_size);
  56.     cout << endl;
  57.     mix(a, a_size, b, b_size);
  58.  
  59.     cout << "\nPo miksowaniu" << endl;
  60.     cout << "a = ";
  61.     show_tab(a, a_size);
  62.     cout << "\nb = ";
  63.     show_tab(b, b_size);
  64.  
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement