|
-
Oct 12th, 2001, 12:56 PM
#1
Thread Starter
Addicted Member
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.
-
Oct 12th, 2001, 01:48 PM
#2
Code:
int c = 0;
for(int i = 0; i < arrSize; ++i)
if(arr1[i] != arr2[i]) ++c;
Z.
-
Oct 12th, 2001, 02:00 PM
#3
Member
Assuming they're the same size.
-
Oct 12th, 2001, 02:24 PM
#4
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
-
Oct 13th, 2001, 04:21 PM
#5
Thread Starter
Addicted Member
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!
-
Oct 13th, 2001, 04:46 PM
#6
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.
-
Oct 14th, 2001, 10:24 AM
#7
Thread Starter
Addicted Member
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!
-
Oct 14th, 2001, 10:25 AM
#8
Thread Starter
Addicted Member
Jim, do you have a PhD in cs?
I only have one in counter-strike(cs).......hehe........
-
Oct 14th, 2001, 11:21 AM
#9
Monday Morning Lunatic
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
-
Oct 14th, 2001, 12:27 PM
#10
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.
-
Oct 15th, 2001, 05:14 AM
#11
Thread Starter
Addicted Member
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!
-
Oct 15th, 2001, 06:07 AM
#12
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.
-
Oct 15th, 2001, 06:28 AM
#13
Thread Starter
Addicted Member
Thanks!
I'm quite a newbie at c++ so I don't know anything about STL, but thanks for the advice anyway!
-
Oct 16th, 2001, 05:41 AM
#14
transcendental analytic
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.
-
Oct 16th, 2001, 10:15 AM
#15
Fanatic Member
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?
-
Oct 16th, 2001, 10:24 AM
#16
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.
-
Oct 16th, 2001, 01:49 PM
#17
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.
-
Oct 16th, 2001, 02:35 PM
#18
Thread Starter
Addicted Member
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........
-
Oct 16th, 2001, 02:46 PM
#19
Corned Bee -
ubound
You can use _msize(ptr)/sizeof(datatype)
_msize works for heap memory.
-
Oct 16th, 2001, 04:52 PM
#20
transcendental analytic
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.
-
Oct 16th, 2001, 04:58 PM
#21
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.
-
Oct 16th, 2001, 05:11 PM
#22
transcendental analytic
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.
-
Oct 17th, 2001, 09:02 AM
#23
Thread Starter
Addicted Member
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!
-
Oct 17th, 2001, 09:51 AM
#24
transcendental analytic
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] = { 1984, 1985, 1986, 1987, 1988 };
int array2[6] = { 1984, 1988, 1992, 1996, 2000, 2004};
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.
-
Oct 17th, 2001, 10:26 AM
#25
Thread Starter
Addicted Member
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!
-
Oct 17th, 2001, 10:52 AM
#26
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.
-
Oct 17th, 2001, 12:06 PM
#27
_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.
-
Oct 17th, 2001, 12:26 PM
#28
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);
-
Oct 17th, 2001, 01:54 PM
#29
transcendental analytic
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.
-
Oct 18th, 2001, 08:29 AM
#30
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.
-
Oct 18th, 2001, 09:35 AM
#31
transcendental analytic
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.
-
Oct 18th, 2001, 09:44 AM
#32
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.
-
Oct 18th, 2001, 09:47 AM
#33
transcendental analytic
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|