Compare two arrays for same value in same position. Use function to test

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Newbie318
    New Member
    • Oct 2011
    • 4

    Compare two arrays for same value in same position. Use function to test

    Test two arrays to determine if they contain the same value in the same position.

    Write a main program that test function isEqual.

    Prototype: bool isEqual(int A[], int B[]);
    Code:
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    // Function prototypes
    bool isEqual(int list[], int list2[]);
    
    int main()
    {
        // Local Identifiers
        int list[100];
        int list2[100];
        int a, b, i, j;
    
       
        // Ouputs if arrays are the same of not.
        cout << endl << "Please input a sequence of numbers: " << endl;
        cin >> list[a];
        cout << endl << "Please input a second sequence of numbers: " << endl;
        cin >> list2[b];
        
           {
          for (i = 0; i < 100; i++)
          {
          for (int j = 0; j< 100; ++j)
           {
          if (list[i] == list2[j]) 
          {
          return true;
          }
          else 
          return false;    
         }  
        
    
           system("pause");
           return 0;
                       
    }    // Functions to compare arrays.
    bool isEqual(int a[], int b[])
    {     
             if (a[] == b[])
             {
             return true;
             }
             else return false;
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Your isEqual function can't work because you are not comparing the two arrays. Instead you are comparing the addresses of the arrays and these addresses wll always be unequal since the arrays are in two different locations.

    Plus there is no way to tell how big the arrays are from inside the function. That means you need another argumeent for the number of elements to compare.

    Comment

    • johny10151981
      Top Contributor
      • Jan 2010
      • 1059

      #3
      your program is suppose to make compilation error.

      Anyway: giving you logical help

      Array A;
      Array B;

      if they both have to have same data in same position

      then it would be like this:

      Code:
      A[0]==B[0];
      A[1]==B[1];
      ...
      A[n]==B[n];
      you do not need to check A[0] and B[1];

      so one for loop will be enough for your problem. and you do not need to send the entire array, just send the value and compare.

      This technique is used for string comparing function such as strcmp

      Comment

      Working...