daily pastebin goal
9%
SHARE
TWEET

Untitled

a guest Oct 23rd, 2018 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int largest (int [], int);
  6. int smallest (int [], int);
  7.  
  8. int main ()
  9. {
  10.     int n1, n2;
  11.  
  12.     cout << "Enter the size of the first array:\n";
  13.     cin >> n1;
  14.     cout << "Enter the size of the second array:\n";
  15.     cin >> n2;
  16.  
  17.     int arr1[n1];
  18.     int arr2[n2];
  19.  
  20.     cout << "Enter the elements of the first array:\n";
  21.     for (int i = 0; i < n1; i++)
  22.     {
  23.         cout << "Enter element " << i+1 << ":\t";
  24.         cin >> arr1[i];
  25.  
  26.     }
  27.  
  28.     cout << "\nEnter the elements of the second array:\n";
  29.     for (int i = 0; i < n2; i++)
  30.     {
  31.         cout << "Enter element " << i+1 << ":\t";
  32.         cin >> arr2[i];
  33.  
  34.     }
  35.  
  36.     int n3 = n1+n2;
  37.     int arr3[n3];
  38.  
  39.     for (int i = 0; i<n3; i++)
  40.     {
  41.         if (i < n1)
  42.             arr3[i] = arr1[i];
  43.         else
  44.             arr3[i] = arr2[i-n1];
  45.     }
  46.  
  47.     for(int i = 0; i<n3; i++)
  48.         cout << arr3[i] << endl;
  49.  
  50.     cout << "\nThe largest element in the merged array is:\t" << largest(arr3, n3);
  51.     cout << "\nThe smallest element in the merged array is:\t" << smallest(arr3, n3);
  52. }
  53.  
  54. int largest (int arr[], int n)
  55. {
  56.     int big = arr[0];
  57.  
  58.     for (int i = 0; i<n-1; i++)
  59.     {
  60.         if(arr[i] > big)
  61.             big = arr[i];
  62.     }
  63.     return big;
  64. }
  65.  
  66. int smallest (int arr[], int n)
  67. {
  68.     int small = arr[0];
  69.  
  70.     for (int i = 0; i<n; i++)
  71.     {
  72.         if(arr[i] < small)
  73.             small = arr[i];
  74.     }
  75.     return small;
  76. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top