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.
Printable View
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.
Z.Code:int c = 0;
for(int i = 0; i < arrSize; ++i)
if(arr1[i] != arr2[i]) ++c;
Assuming they're the same size.
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
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!
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.
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!
Jim, do you have a PhD in cs?
I only have one in counter-strike(cs).......hehe........;)
It's not specifically Linux.Quote:
Originally posted by [praetorian]
Ok, I didn't know winzip could open linux-packed files.....
.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.
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?
So, in arr1, we have:Code:int arr1[] = {1, 3, 2, 4, 2, 5, 3, 1};
int arr2[] = {1, 5, 2, 2, 4, 3, 3, 1};
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.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;
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.
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!
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.
Thanks!
I'm quite a newbie at c++ so I don't know anything about STL, but thanks for the advice anyway!
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?
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;
}
}
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
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:
So, instead of the expected 1 mismatch, you get 7.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;
Z.
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........
Corned Bee -
ubound
You can use _msize(ptr)/sizeof(datatype)
_msize works for heap memory.
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.
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.
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.
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!
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++;
}
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!
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;
}
_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.
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);
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
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)
Nope... It's not there
malloc.h
MSDN says that.
works :) thanks