Array Comparisons

This is a discussion on Array Comparisons within the C++ Programming forums, part of the General Programming Boards category; I'm self learning C++ to make myself more valuable to my bosses. I'm using Microsoft Visual C++ 5.0. I am ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    3

    Array Comparisons

    I'm self learning C++ to make myself more valuable to my bosses.
    I'm using Microsoft Visual C++ 5.0.
    I am trying to create random numbers in multiple arrays, comparing the new array elements to previously set array elements to make sure there are no two elements with the same value. This gets really long and hard to follow using 'if' and comparison statements. My question is: Is there a function to compare the elements of 2 arrays (type int for this example)?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Is there a function to compare the elements of 2 arrays (type int for this example)?
    Yes. But you'll have to write it yourself. It shouldn't be too hard though:
    Code:
    bool acmp ( const int array1[], const int array2[], int size )
    {
      for ( int i = 0; i < size; i++ ) {
        if ( array1[i] != array2[i] )
          return false;
      }
      return true;
    }
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    3
    I have done that, but it appears to only check elements of equal position, meaning if array1[0] == array2[5], it wouldn't get checked and I would not know about it.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >meaning if array1[0] == array2[5], it wouldn't get checked and I would not know about it.
    Then use two loops. One goes over array1 and the other goes over array2. Something like this:
    Code:
    bool intersect ( const int array1[], const int array2[], int size )
    {
      for ( int i = 0; i < size; i++ ) {
        for ( int j = 0; j < size; j++ ) {
          if ( array1[i] == array2[j] )
            return true; // Item in array1 found in array2
        }
      }
      return false;
    }
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    3

    Smile

    Thank You kindly, I got it working, and I managed to get the function to sort the arrays in ascending order without further help, maybe there's hope for me yet?

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    2

    Comparing 3 numbers and returning largest

    I need to figure out how you would compare 3 numbers and sort from smallest to largest. I'll be using an overloaded operator, < to do this, but that part I can figure out. I just need to know the syntax for sorting and returning them in order from smallest to largest.
    Thanx.
    DD.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    How would you do this by hand?

    One way is to say is A < B and A < C? If so then A is smallest. Else is B < A and B < C? if so then B is smallest. Else is C < A and C < B? if so then C is smallest. etc.

    Another way, is create a temporary variable called smallest. Assign A to smallest. Compare smallest to B. If B is smaller than smallest then assign B to smallest. Then compare smallest with C. If C is smaller than smallest then assign C to smallest. etc.

    Another way is to put all three values in an array. Then compare array[2] with array[1]. If array[2] < array[1], swap values. Then compare array[1] with array[0]. If array[1] < array[0], then swap. etc.

    Lot's of ways to do this. Depends on what you know already, and what you are learning about now. Give it your best shot and come back with more precise questions if you need to.

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    2
    Ok, I can understand that, but that will just give me the smallest...how do I go about printing out the smallest to largest?
    DD

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    version 1.  if a < b and a < c
                         if b < c 
                            cout a b c
                         else
                             cout  a c b
                      else if b < a and b < c
                          if a < c
                             cout b a c
                          else 
                              cout b c a
                      etc.
    
    version 2.   if a = = smallest
                          if b < c
                      etc
    
    version 3.  look up bubble sort by searching the board or the web or your instructional book.  its a general sort algorhythm that will work for array/list of any size and not specific to just three values, like version 1 and 2.  It can also be used to sort largest to smallest.  It's not the most sophisticated sorting algorhythm, and for larger groups of values it's not the fastest, but it is among the easiest code to write.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 11:33 AM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21