Advertisement
Infernale

NCTU LAB 12/03 NUM 4

Mar 12th, 2019
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.56 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void bubbleSort(int arr[], int n){
  5.     for(int i=0;i<n-1;i++){
  6.         bool swap = false;
  7.         for(int j=0;j<n-i-1;j++){
  8.             if(arr[j]>arr[j+1]){
  9.                 int temp = arr[j];
  10.                 arr[j] = arr[j+1];
  11.                 arr[j+1] = temp;
  12.                 swap = true;
  13.             }
  14.         }
  15.         if(!swap){
  16.             break;
  17.         }
  18.     }
  19. }
  20.  
  21. void ssearch(int x[], int y[], int z[], int l, int m, int n){
  22.     int i=0, j=0, k=0;
  23.     while(i<l && j<m && k<n){
  24.         if(x[i]==y[j] && y[j]==z[k]){
  25.             cout << "The smallest common element is " << x[i] << endl;
  26.             return;
  27.         }else if(x[i]<y[j]){
  28.             i++;
  29.         }else if(y[j]<z[k]){
  30.             j++;
  31.         }else{
  32.             k++;
  33.         }
  34.     }
  35.     cout << "Not found!" << endl;
  36. }
  37.  
  38. int main(){
  39.     int a, b, c;
  40.     cout << "Please input the amount of numbers in the first, second and third array: ";
  41.     cin >> a >> b >> c;
  42.     int *fData = new int[a];
  43.     int *sData = new int[b];
  44.     int *tData = new int[c];
  45.     cout << "Input the elements in array I: ";
  46.     for(int i=0;i<a;i++){
  47.         cin >> fData[i];
  48.     }
  49.     cout << "Input the elements in array II: ";
  50.     for(int i=0;i<b;i++){
  51.         cin >> sData[i];
  52.     }
  53.     cout << "Input the elements in array III: ";
  54.     for(int i=0;i<c;i++){
  55.         cin >> tData[i];
  56.     }
  57.     bubbleSort(fData, a), bubbleSort(sData, b), bubbleSort(tData, c);
  58.     ssearch(fData, sData, tData, a, b, c);
  59.     delete[] fData, sData, tData;
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement