רוצה להיות מתכנת
New member
כתבתי פונקציה שמחזירה מערך חיתוך של שני מערכים
ומעדכנת את גודלו. אין לי מושג מה לא בסדר, אבל כשאני מריץ את הפונקציה בתכנית שלי אני מקבל ערכי זבל. אשמח אם מישהו יביט ויסביר לי מה הבעיה.
אולי הבעיה היא ב reallocate?
ומעדכנת את גודלו. אין לי מושג מה לא בסדר, אבל כשאני מריץ את הפונקציה בתכנית שלי אני מקבל ערכי זבל. אשמח אם מישהו יביט ויסביר לי מה הבעיה.
אולי הבעיה היא ב reallocate?
קוד:
int* IntersectionOfArrays(int* arr1, int size1, int* arr2, int size2, int* sizeRes)
{
int* res=(int*)malloc(1*sizeof(int)); //res is the array of the resolution of intersection//
int i = 0, j = 0;
*sizeRes = 0;
merge_sort(arr1,0, size1-1); //sorting the arrays//
merge_sort(arr2,0, size2-1);
while (i < size1 && j < size2)
{
if (arr1[i] < arr2[j])
i++;
else if (arr1[i] > arr2[j])
j++;
else
{
res[*sizeRes] = arr1[i]; //getting the same elements of the two arrays - the intersection values//
i++;
j++;
(*sizeRes)++;
res = (int*)realloc(res, 1*sizeof(int)); //allocating more memory as required - according to the size of res(intersection)//
}
}
if (*sizeRes==0) //if the intersection is empty
return NULL;
return res;
}