1
void interclas(int *ptr,int *vec, int *c, int n) {
    int i,j,tmp;
    tmp=0;
    for (i=0;i++;i<n)
        for (j=0;i++;j<n)
        {
            if (vec[j]<=ptr[i])
                c[tmp]=vec[j];
            else
                c[tmp]=ptr[i];
            tmp++;
        }
}

int main() {
    int i,n;
    int *ptr,*vec,*c;

    printf("Nr. of elements of initial arrays : 5 \n");
    n=5;
    vec=(int*)malloc( n * sizeof(int));
    ptr=(int*)malloc( n * sizeof(int));

    c=(int*)malloc( 2 * n * sizeof(int));
    for (i=0;i<n;i++) {
        scanf("%d",&ptr[i]);
    }
    for (i=0;i<n;i++) {
        scanf("%d",&vec[i]);
    }

    printf("\n");
    printf("Initial arrays are : ");
    for (i=0;i<n;i++) {
        printf("%d ",ptr[i]);
    }
    printf("\n");
    for (i=0;i<n;i++) {
        printf("%d ",vec[i]);
    }

    interclas(ptr,vec,&c,n);

    printf("Merged array is : ");
    for (i=0;i<10;i++) {
        printf("%d ",c[i]);
    }
    return 0;
}

So I'm trying to merge two sorted arrays into one new one using pointers with the function 'interclas'. I tried using the same method to sort an array with a pointer in a function and it worked just fine. Now as you can see, it stores the adress of the variable rather than the variable itself. If I run this, it stores the adresses of the arrays. How can I fix this? (I'm still new to pointers)

gsamaras
  • 71,951
  • 46
  • 188
  • 305
manubmxnsb
  • 151
  • 1
  • 14
  • 1
    `&c` is a pointer to the variable `c`, it's a pointer to the pointer and will have the type `int **`. Doesn't your compiler warn you about passing an `int **` to a function that expects an `int *` as argument? – Some programmer dude Jan 19 '20 at 13:02
  • In C you don't have to (and really shouldn't) [cast the result of `malloc`](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – Some programmer dude Jan 19 '20 at 13:04
  • Its because my teacher asked us to do programs these way. We could get it in the exam so I'm trying to accomodate with it. @Someprogrammerdude it only warns me that the for loops in the function are : "statement with no effect [-wunused value]" – manubmxnsb Jan 19 '20 at 13:07

1 Answers1

4

In your method's body, change:

for (i=0;i++;i<n)
    for (j=0;i++;j<n)

to this:

for (i=0; i<n; i++)
    for (j=0; j<n; j++)

and then change the call to your method, from this:

interclas(ptr, vec, &c, n);

to this:

interclas(ptr, vec, c, n);

since the prototype expects a pointer to an int, for the third parameter.


The logic of your method is also flawed, try to put some printfs (e.g. printf("here i = %d, j = %d, ptr[i] = %d, vec[j] = %d, tmp = %d\n", i, j, ptr[i], vec[j], tmp);) to see what values your variables have at its iteration - you only get the first two elements of the first array to be merged!

If you think about it, what you'd like to do is to go through the first element of array ptr and vec, and store the minimum of this two. If now that min was of array ptr, you'd like the next element of ptr to be taken into account, otherwise the next element of vec.

Take a pencil and a paper and sketch that algorithm - you'll see that it goes out nicely, but some leftover elements might be left behind, and not get inserted in the output array.

Driven from that observation, after traversing both the arrays and comparing elements, we will loop over the first array, if needed, to collect elements that were not visited. Similarly for the second array.

Coding that thought gives something like this:

void interclas(int *ptr,int *vec, int *c, int n) {
    int i = 0, j = 0, tmp = 0;
    // Traverse both arrays simultaneously,
    // and choose the min of the two current elements.
    // Increase the counter of the array who had
    // the min current element.
    // Increase the counter for the output array in
    // any case.
    while(i < n && j < n)
    {
         if(ptr[i] < vec[j])
        {
            c[tmp++] = ptr[i++];
        }
        else
        {
            c[tmp++] = vec[j++];
        }
    }
    // Store remaining elements of first array 
    while (i < n) 
        c[tmp++] = ptr[i++]; 

    // Store remaining elements of second array 
    while (j < n) 
        c[tmp++] = vec[j++]; 
}

Not the source of your problem, but Do I cast the result of malloc? No.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • I did that. I don't know how I didnt see it! Now it stores variables. Although it just stores "1" and "2". – manubmxnsb Jan 19 '20 at 13:18
  • So for ptr= 1 2 3 4 5 and vec = 3 4 5 6 7 , c = 1 1 1 1 1 2 2 2 2 2 – manubmxnsb Jan 19 '20 at 13:19
  • @manubmxnsb indeed, because the logic of your method is flawed, check my updated answer, and let me know what you think of this! :) machine_1 they're synonyms. – gsamaras Jan 19 '20 at 13:44
  • while (i < n) c[tmp++] = ptr[i++]; while (j < n) c[tmp++] = vec[j++]; Are you doing this because the last element of each array is neglected in the first loop ? I don't quite get it. – manubmxnsb Jan 19 '20 at 14:59
  • 1
    No worries! I runned this through a step by step execution with watches and figured out why. Basically, because the arrays are sorted, the lower one will be put in the 3rd array first, leaving some elements of the second one out, that have to be insterted as well. Thank you!! – manubmxnsb Jan 19 '20 at 15:08