Advertisement
khaiwen1111

Tutorial 8 Question 3

Mar 30th, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. #include <iostream>
  2. #define ARRAY_SIZE 5
  3. using namespace std;
  4.  
  5. bool compare(int x[], int y[], int size);
  6.  
  7. int main(void)
  8. {
  9.     int a1[ARRAY_SIZE] = {1, 2, 3, 4, 5};
  10.     int a2[ARRAY_SIZE] = {1, 2, 3, 4, 5};
  11.     int a3[ARRAY_SIZE] = {1, 1, 1, 1, 1};
  12.  
  13.     if ( compare(a1, a2, ARRAY_SIZE) )
  14.         cout<<"Arrays a1 and a2 are equal\n";
  15.     else
  16.         cout<<"Arrays a1 and a2 are not equal\n";
  17.  
  18.     if ( compare(a1, a3, ARRAY_SIZE) )
  19.         cout<<"Arrays a1 and a3 are equal\n";
  20.     else
  21.         cout<<"Arrays a1 and a3 are not equal\n";
  22.  
  23.     return 0;
  24. }
  25.  
  26. bool compare(int x[], int y[], int size)
  27. {
  28.     bool flag= true; // assume 2 arrays are equal
  29.  
  30.     //version 1
  31.     /* for(int i = 0; i < size; i++)
  32.         if(x[i] ==y[i])
  33.             ; // DO NOTHING SINCE ASSUME 2 ARRAYS ARE EQUAL INITIALLY
  34.         else
  35.             flag = false;
  36.     */
  37.  
  38.     //version 2
  39.     /*for(int i = 0; i < size; i++)
  40.         if (x [i] != y[i])
  41.             flag = false;
  42.     */
  43.  
  44.     //version 3
  45.     int i= 0;
  46.     while ( i < size && flag != false)
  47.     {
  48.         if if (x [i] != y[i])
  49.             flag = false;
  50.         i++;
  51.     }
  52.  
  53.     return flag;
  54.    
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement