Thursday 2 December 2010

Problem Statement: Comparing two arrays for equality or matching indexes You are required to write a program which will take input from user in two integer arrays. The program should compare both arrays for checking if both arrays are totally identical (Exactly same). If not, then program should determine the matching indexes of both arrays. Matching indexes means that first element of array1 should be equal to first element of array2 and so on.

Example of two identical (Exactly same) arrays:

Array1        Array2
1             1
2             2
3             3
5             5
7             7
9             9
11            11
13            13
15            15
17            17


Example of two arrays with some matching elements:

Array1        Array2
1             7
2             9
3             3
5             4
7             6
9             8
11            11
13            15
8             8
17            17

Here elements on index 3, 7, 9, and 10 of both arrays are same.



Detailed Description:

 The program should take 10 integer inputs from user in arrray1 and array2.
  • After taking inputs in both arrays, you should pass both arrays to a function named match.
  • If both arrays are exactly same, the program should display a message “Both arrays are identical”.
  • If both arrays are not exactly same, then program should match the indexes which have same elements in both arrays and display the matching indexes.
  • If there is no element matching in both arrays, the program should display a message
  “There is no matching element in both arrays”.
  • Implementation regarding comparison of arrays and displaying message should be inside the function match.

Sample Output for two exactly same arrays

Please enter 10 integers for array1:
Enter element 1 : 1
Enter element 2 : 3
Enter element 3 : 5
Enter element 4 : 7
Enter element 5 : 9
Enter element 6 : 11
Enter element 7 : 13
Enter element 8 : 15
Enter element 9 : 17
Enter element 10 : 19

Please enter 10 integers for array2:
Enter element 1 : 1
Enter element 2 : 3
Enter element 3 : 5
Enter element 4 : 7
Enter element 5 : 9
Enter element 6 : 11
Enter element 7 : 13
Enter element 8 : 15
Enter element 9 : 17
Enter element 10 : 19

Both arrays are identical


Sample Output for arrays with some matching indexes

Please enter 10 integers for array1:
Enter element 1 : 1
Enter element 2 : 3
Enter element 3 : 5
Enter element 4 : 7
Enter element 5 : 9
Enter element 6 : 11
Enter element 7 : 13
Enter element 8 : 15
Enter element 9 : 17
Enter element 10 : 19

Please enter 10 integers for array2:
Enter element 1 : 2
Enter element 2 : 4
Enter element 3 : 5
Enter element 4 : 6
Enter element 5 : 9
Enter element 6 : 12
Enter element 7 : 14
Enter element 8 : 16
Enter element 9 : 17
Enter element 10 : 20


Both arrays have same elements on
Index 3
Index 5
Index 9

/********************* ASSIGNMENT NO. 2  SOLUTION ****************/

#include <iostream.h>
#include <conio.h>

void match(int[], int[]);  // Declaration of match function for matching 2 arrays

main()
{
    int array1[10], array2[10];

// Taking 10 integer inputs from user in array 1
  cout << "Please enter 10 integers for array1:" << endl;
  for(int i=0; i<10; i++)
   {
       cout << "Enter element " << i+1 <<" : ";
       cin >> array1[i];
   }
  
// Taking 10 integer inputs from user in array 2          
  cout << endl<<"Please enter 10 integers for array2:" << endl;
  for(int i=0; i<10; i++)
   {
       cout << "Enter element " << i+1 <<" : ";
       cin >> array2[i];
   }

//Passing both arrays to function match
match(array1, array2);

getch();
} //End of main() function

//Definition of function match taking two arrays as arguments
void match(int Array1[], int Array2[])
 {

int check = 0;  //This variable will be used to checked whether arrays are identical, with some same elements or have no same element
 
/* In the following loop, we are comparing elements at same indexes of both arrays. If each
element of both arrays is same, then variable "check" will be incremented 10 times.*/
  for(int i=0; i<10; i++)
       {
         if(Array1[i] == Array2[i])
          {
            check++;
          }
       }
      
   
/* If variable check is incremented 10 times then it means that both arrays are exactly same*/
  if(check == 10)
     {
       cout << endl << "Both arrays are identical";
     }   
   

/* If variable check is less than 10 and more than 0, it means that there are some elements which
are same in both arrays at same indexes. So, here we will display matching indexes in both arrays*/    
  else if (check <10 && check > 0)
     {
       cout << endl<<"Both arrays have same elements on "<<endl;
           for(int j=0; j<10; j++)
             {
               if(Array1[j] == Array2[j])
                   {
                     cout << "index " << j+1 << endl; 
                   }
              }
        }
   
   
/* If variable check=0, it means that there is no matching element in the both arrays*/
  else if(check == 0)
     {
       cout << endl << "There is no matching element in both arrays";
     }   

}  

No comments:

Post a Comment