Results 1 to 33 of 33

Thread: compare arrays........

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    I'm mobile
    Posts
    166

    compare arrays........

    Is there any way to compare two arrays and calculate how many times they're not equal.

    Like, you've

    int vector[3] = { 1, 2, 3 };
    int vector2[3] = { 1, 2, 6 };

    and then you compare these two and report that they're not equal 1 time.
    [p r a e t o r i a n]

  2. #2
    Zaei
    Guest
    Code:
    int c = 0;
    for(int i = 0; i < arrSize; ++i)
         if(arr1[i] != arr2[i]) ++c;
    Z.

  3. #3
    Assuming they're the same size.

  4. #4
    jim mcnamara
    Guest
    If you really want to deal with this, goto www.gnu.org
    and locate diff - there is source code for it on an ftp site
    along with cmp source.

    Source code :
    ftp://ftp.gnu.org/pub/gnu/diffutils/...ils-2.7.tar.gz

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    I'm mobile
    Posts
    166
    Jim: I'll have a look at that page.......but I don't have linux, so I don't think I can open that file......

    Zaei: I don't think it works quite like that, because if I have an array with different values and another with some different and some same values and they compare each other, it'll not be right.

    int array1[5] = { 1984, 1985, 1986, 1987, 1988 };
    int array2[6] = { 1984, 1988, 1992, 1996, 2000, 2004};

    If i compare these two with your method, it'll report that they're only the same one time(1'st time).

    when the loop reach value 5 for array1 = 1988 and the same for array2 = 2000. The program will report that they're not the same, but I want it to report that they're the same, because both arrays contain 1988.

    I hope you understand what I mean!
    [p r a e t o r i a n]

  6. #6
    jim mcnamara
    Guest
    WinZip can open those files, and they can be converted over to VC++. They even used tell you how to compile them for VC++, and probably still do.

    Your observations about Z's methods are correct. It won't work.
    You have to correct when you have a miss or multiple misses.

    What I'm trying to tell you in a nice way - that algorithm is HEAVY DUTY. No detriment to our posters here, but I don't believe any of them is a PhD in CS. They are not gonna know.

    The only reason I know, is because I had to do this for files about 10 years ago. So I had to learn.

    If you don't believe me, try a google search on diff algorithm It is still an active area of research in Computer Science.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    I'm mobile
    Posts
    166
    Ok, I didn't know winzip could open linux-packed files.....

    I understand it's not an easy thing, but I didn't realise I'd hit something that difficult(for being a newbie......).......

    I'll check that page you talked about earlier sometime and look into it, but I managed to solve the problem concerning arrays in my day-program (Task: calculate days between different years) in another way!

    Thanks anyway for the help!
    [p r a e t o r i a n]

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    I'm mobile
    Posts
    166
    Jim, do you have a PhD in cs?

    I only have one in counter-strike(cs).......hehe........
    [p r a e t o r i a n]

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Originally posted by [praetorian]
    Ok, I didn't know winzip could open linux-packed files.....
    It's not specifically Linux.

    .tgz == .tar.gz

    .tar = Tape ARchive
    This is an uncompressed archive, suitable for writing a load of files to tape easily (it also has certain filesystem information in it, not just the files themselves). It's been around for a LONG time

    .gz = GZip
    Uses the same compression algorithm as zip files, so WinZip can uncompress them, and then analyse the .tar file inside.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  10. #10
    Zaei
    Guest
    Not enough info! Im innocent! =).

    So, what you are trying to do is calculate the number of times an element occurs in one list, and then check the second list to see how many data items are different?
    Code:
    int arr1[] = {1, 3, 2, 4, 2, 5, 3, 1};
    int arr2[] = {1, 5, 2, 2, 4, 3, 3, 1};
    So, in arr1, we have:
    Code:
    1: 2 times;
    2: 2 times;
    3: 2 times;
    4: 1 time;
    5: 1 time;
    
    and arr2:
    
    1: 2 times;
    2: 2 times;
    3: 2 times;
    4: 1 time;
    5: 1 time;
    So, the function or whatever should say that they are the same? That is fairly simple, just loop through list1, create 2 vectors of a size equal to the highest value, loop through list1 again, and do soemthing like "itemsList1[arr[i]]++;". Then loop thorugh list 2, and do the same thing for the second vector you created, then loop through both vectors and compare them.

    Now, to handle lists of different sizes. First, figure out the length of each list (sizeof(arr1) / sizeof(arr1[0]). Then, loop through, and find the highest value in each list (should do this for the above algo anyway), and create a vector of size equal to the highest value. Then repeat above, making sure to account for the difference in size (probably add that to the number of times different?).

    Jim, any comments?

    Z.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    I'm mobile
    Posts
    166
    I know you're innocent, I didn't provide all the necessary info......sorry

    I just want the program to compare two arrays and then report how many times they're equal....I haven't had a go at your code yet, but I'll look at it later.....thanx!
    [p r a e t o r i a n]

  12. #12
    Zaei
    Guest
    The one i posted (the long one) will give you the number of differences, but it wont be hard tochange it around =). Hope it works. You could also try using an STL map to save memory.

    Z.

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    I'm mobile
    Posts
    166
    Thanks!

    I'm quite a newbie at c++ so I don't know anything about STL, but thanks for the advice anyway!
    [p r a e t o r i a n]

  14. #14
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    praetorian,
    1. in your last sample you have two sorted arrays, are they always sorted?

    2. "...same, because both arrays contain 1988. " Do you count the numbers that occurs only in one of them or in either but not in both?
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  15. #15
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    well if you know the size:

    array 1: 5
    array 2: 10


    btw, how do you get the size of the array? like in VB you have UBound()


    Code:
    int count=0;
    int array1[6], array2[11];
    
    for(int i =0;i<=5;i++){ //first array
       for(int a=0;a<=10;a++){ //second array
          if(array1[i]!=array2[a])
             count+=1;
       }
    }
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  16. #16
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    nabeels: if you declared the array statically, you can use
    sizeof(array)/sizeof(array[0])
    However, if the array was passed to you using a pointer, there is no way to find out. This is why so many API functions (e.g. Polygon) require you to tell how many elements are in the array
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  17. #17
    Zaei
    Guest
    nabeels, The code you posted will give incorrect results. If for example, you have the arrays {1, 2, 3} and {1, 2, 4}, your code would do something like:
    Code:
    a1[0] == a2[0] count = 0;
    a1[0] != a2[1] count = 1;
    a1[0] != a2[2] count = 2;
    
    a1[1] != a2[0] count = 3;
    a1[1] == a2[1] count = 3;
    a1[1] != a2[2] count = 4;
    
    a1[2] != a2[0] count = 5;
    a1[2] != a2[1] count = 6;
    a1[2] != a2[2] count = 7;
    So, instead of the expected 1 mismatch, you get 7.

    Z.

  18. #18

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    I'm mobile
    Posts
    166
    Kedaman,

    1. If you mean that they're always sorted in increased order.....then yes.

    2. Don't really sure what you mean........sorry......please explain in english or swedish........
    [p r a e t o r i a n]

  19. #19
    jim mcnamara
    Guest
    Corned Bee -

    ubound

    You can use _msize(ptr)/sizeof(datatype)

    _msize works for heap memory.

  20. #20
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    praetorian,

    ska programmet räkna antalet tal i array1 som inte finns i array2, tvärtom eller tal som bara finns i endera? Om inte nån av dom kan du gärna förklara på svenska

    Jim,

    where's the data of size allocated on the heap stored? thanks for mentioning _msize(), i'll have use for it.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  21. #21
    jim mcnamara
    Guest
    Kedaman - I don't know. Runtime means to have to change the value everytime the pointer gets aimed somewhere else.
    I've used _msize, but only in a limited way. It seems to work.

    sizeof(x) is strictly a compile-time thing.

  22. #22
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    yeah i know sizeof doesn't exist in runtime, but the _msize data can't be stored with a pointer, it has to be stored somewhere near the allocated memory, like just 4 bytes behind or something, i guess delete and delete[] use the same data.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  23. #23

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    I'm mobile
    Posts
    166
    Kedaman,

    Programmet ska bara räkna antalet tal som finns i båda. Det ska alltså ta två vektorer och jämföra dem, sedan rapportera hur många gånger de innehåller samma nummer. I mitt exempel skulle alltså programmet returnera att vektorerna innehöll samma tal 2 ggr........hoppas du förstår!
    [p r a e t o r i a n]

  24. #24
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    praetorian
    Här är programmet. Den kör igenom båda arrayerna i loopen och ser till att ingendera iteratorerna sticker iväg utan att den andra hunnit ifatt. Ifall dom är lika, så incrementeras countern c. Loopen slutar när nån dera iteratorerna hoppat ur arrayn vilket betyder den andra har större värde än nån i första arrayn.
    PHP Code:
        int array1[5] = { 19841985198619871988 }; 
        
    int array2[6] = { 198419881992199620002004}; 
        
    bool t;
        
    int c=0;
        
    int *i1=array1,*i2=array2;
        
    int *l1=i1+sizeof(array1)/sizeof(int),*l2=i2+sizeof(array2)/sizeof(int);
        for(;; ){
            
    c+=*i1==*i2;
            if (
    i1==l1||i2==l2)break;
            
    t=*i1>=*i2;
            if (*
    i1<=*i2)i1++;
            if (
    t)i2++;
        } 
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  25. #25

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    I'm mobile
    Posts
    166
    Tack för hjälpen med programmet!

    Har inte testat det än, men ska göra det snart.......har några andra saker som måste göras först....., men jag återkommer senare och berättar om det fungerar och det gör det säkert.

    tack!
    [p r a e t o r i a n]

  26. #26
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    jim: but what if:
    Code:
    void func(int* ar)
    {
       // how to find out the size of the array?
    }
    
    int main()
    {
      int ar[3] = {1, 2, 3, 4, 5};
      func(ar);
      return 0;
    }
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  27. #27
    jim mcnamara
    Guest
    _msize works on heap memory-allocated datatypes.

    What you're asking is:
    Does it work in a function called from anywhere?
    And in the case where the pointer could reference any variable like (void *)?

    I don't know.

    In order for it to work at all (as Kedaman observed), pointers would have to be 64 bits (with the lower 32 bits "hidden") in order for this to work. The hidden 32 bit segment keeps the overall length of the object. I will research this and get back to you. If this is the case, _msize will always work regardless.

    I use it for things like getting the ubound of arrays on heap where I call realloc() or new an undefined number of times, all in the same module.

  28. #28
    jim mcnamara
    Guest
    Debug Heap Management on MSDN discusses this.

    The pointer will work in a function, AS LONG AS the item was allocated from heap. Otherwise it returns zero. If it worked on static declarations as well, you would never see api's with
    (lpzData as String, lpDataLen as Long) in arguments.


    As I understand it, _msize and _msize_dbg do use 32 bits of heap to indicate length of whatever the heap pointer is aimed at. malloc(), realloc(), calloc() _expand() and so on all maintain this information.

    _msize_dbg also maintains information about surrounding heap datatypes and sizes.

    It does not explicitly state where this infomation is located.
    However, this might give an answer:

    Code:
           int *i;
           i = (int*) malloc(200);
           i--;
           printf("%d\n",*i);

  29. #29
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    nope it didn't

    btw how does it recognize heap allocated data?
    what do i need to include to use _msize since it doesn't work
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  30. #30
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    kedaman: maybe memory.h

    jim: yes there are such functions. TextOut for example. Or Polygon (takes an array of POINT structures and the number of elements in the array)
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  31. #31
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Nope... It's not there
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  32. #32
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    malloc.h
    MSDN says that.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  33. #33
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    works thanks
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width