0

I was trying to write a program that reads two arrays (and their sizes), writes the numbers that appear in both in a new array and returns the new array and it's size.

it throws me out after reading the arrays. I think I did a few things wrong with the dynamic memory allocation, still very confused by the whole thing.

I'd appreciate it if you could take a look and tell me what's wrong. thanks!

#include <stdio.h>
#include <stdlib.h>
int andArrays(int*, int*, int, int, int*);
void main()
{
    int size1, size2, i, j, s, num1, num2, newsize;
    printf("enter the size of the array >>> ");
    scanf_s("%d", &size1);
    int* arr1 = (int*) malloc(size1*sizeof(int));
    printf("enter %d numbers: ", size1);
    for (i = 0; i < size1; i++)
    {
        scanf_s("%d\n", &num1);
        arr1[i] = num1;
    }
    printf("enter the size of the array >>> ");
    scanf_s("%d", &size2);
    int* arr2 = (int*)malloc(size2*sizeof(int));
    printf("enter %d numbers: ", size2);
    for (j = 0; j < size1; j++)
    {
        scanf_s("%d\n", &num2);
        arr2[j] = num2;
    }
    int newarray = andArrays(&arr1, &arr2, size1, size2, &newsize);
    printf("size of new array is: %d", newsize);
    for (s = 0; s < newsize; s++)
        printf("%d", newarray[s]);
    free (newarray);
    free(arr1);
    free(arr2);
}

int andArrays(int* arr1, int* arr2, int size1, int size2, int* newsize)
{
    int i = 0, j = 0, num, f = 0;
    int* newarr = (int*)malloc(size1 * sizeof(int));
    while (1)
    {
        if (arr1[i] == arr2[j])
        {
            num = arr1[i];
            newarr[f] = num;
            f++;
        }
        i++;
        j++;
        if (i == size1)
            break;
    }
    *newsize = f;
    }
}
3
  • This can't be your actual code. It does not even compile. Commented Jan 7, 2021 at 18:48
  • You can't define andArrays inside main. Commented Jan 7, 2021 at 19:11
  • I did accidently put andArrays in the main at first but it isn't anymore Commented Jan 7, 2021 at 19:18

1 Answer 1

2

Issue is with second read loop, the value is read for "size2", but the for loop is check for "size1".

    scanf_s("%d", &size2);
    int* arr2 = (int*)malloc(size2*sizeof(int));
    printf("enter %d numbers: ", size2);
    for (j = 0; j < size1; j++)   >>>> for (j = 0; j < size2; j++)
    {
        scanf_s("%d\n", &num2);
        arr2[j] = num2;
    }

also the function "andArrays" should return a array of integers, not one single int.

    int andArrays(int*, int*, int, int, *int);   >>>>>   int * andArrays(int*, int*, int, int, int*); //declaration of function

code:

    #include <stdio.h>
    #include <stdlib.h>
    int * andArrays(int*, int*, int, int, int*); //declaration of function
    int main ()
    //void main()
    {
        int size1, size2, i, j, s, num1, num2, newsize;
        printf("enter the size of the array >>> ");

        scanf("%d", &size1);
        int* arr1 = (int*) malloc(size1*sizeof(int));

        printf("\nenter %d numbers: ", size1);

        for (i = 0; i < size1; i++)
        {
            scanf(" %d", &num1);
            arr1[i] = num1;
        }
        printf("\nenter the size of the array >>> ");
        scanf(" %d", &size2);

        int* arr2 = (int*)malloc(size2*sizeof(int));
        printf("\nenter %d numbers: ", size2);

        for (j = 0; j < size2; j++)
        {
            scanf(" %d", &num2);
            arr2[j] = num2;
        }

        int * newarray = andArrays(arr1, arr2, size1, size2, &newsize);
        printf("\nsize of new array is: %d\n Array : ", newsize);
        for (s = 0; s < newsize; s++)
            printf(" %d ", newarray[s]);
        //free (newarray);
        //free(arr1);
        //free(arr2);
        printf("\n");
        return 0;
    }
    int * andArrays(int* arr1, int* arr2, int size1, int size2, int* newsize)
    {
        int i = 0, j = 0, num, f = 0;
        int* newarr = (int*)malloc(size1 * sizeof(int));
        while (1)
        {
            if (arr1[i] == arr2[j])
            {
                num = arr1[i];
                newarr[f] = num;
                f++;
            }
            i++;
            j++;
            if (i == size1)
                break;
        }
        *newsize = f;
        return newarr;
    }
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your help! it still doesn't seem to work when the new array is more than two numbers...
please update your latest code in question. make sure size1 and size2 are used correctly. have you tried with the code which i hv shared ?
NISM-M-9168:dirtest nism$ ./a.out enter the size of the array >>> 4, enter 4 numbers: [1 2 3 4] enter the size of the array >>> 5, enter 5 numbers: [1 2 3 4 5] size of new array is: 4, Array : [1 2 3 4]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.