Thread: sorted two array and result array

  1. #1
    Registered User
    Join Date
    Jun 2019
    Posts
    21

    sorted two array and result array

    Hi
    I write function that take 5 parameters: pointer to array a, pointer to array b, size of array a, size of array b and pointer to size of result array
    My task is to sort first array remove duplicates, this same with second array and include this two array. My code below

    Code:
    #include <stddef.h>
    #include <stdlib.h>
    int compare(const void *a, const void *b)
    {
      return *(int*)a-*(int*)b;
    }
    
    
    int *testit(const int *a, const int *b, size_t az, size_t bz, size_t *cz) {
    int *res;
    size_t i,j;
    qsort(a,az,sizeof(int),compare);
    qsort(b,bz,sizeof(int),compare);
    
    
    res=(int*)malloc(sizeof(int)*(az+bz));
    for(i=0,j=0;i<az;)
    {
      if(a[i]!=a[i+1])
        res[j++]=a[i++];
      if(a[i]==a[i+1])
      {
        while((a[i]==a[i+1])&&(i<az))
          i++;
        res[j++]=a[i++];
      }
     }
    for(i=0;i<bz;)
    {
      if(b[i]!=b[i+1])
        res[j++]=b[i++];
      if(b[i]==b[i+1])
      {
        while((b[i]==b[i+1])&&(i<bz))
          i++;
        res[j++]=b[i++];
      }
     }
     *cz=j;
     qsort(res,*cz,sizeof(int),compare);
    return res;
    }
    I got some errors from server
    Code:
    ERROR < incorrect array length > 
    Array a:   {1} 
    Array b:   {2, 3, 4} 
    Expected:  {1, 2, 3, 4} 
    Submitted: {1, 1, 2, 3, 4}
    
    Can you tell me where is mistake

  2. #2
    Registered User
    Join Date
    Jun 2019
    Posts
    21
    I have changed the code but I still got message
    Test Crashed
    Caught unexpected signal: SIGSEGV (11). Invalid memory access.
    Code:
    #include <stddef.h>
    #include <stdlib.h>
    int compare(const void *a, const void *b)
    {
      return *(int*)a-*(int*)b;
    }
    
    
    int *testit(const int *a, const int *b, size_t az, size_t bz, size_t *cz) {
    int *res;
    size_t i,j;
    qsort(a,az,sizeof(int),compare);
    qsort(b,bz,sizeof(int),compare);
    
    
    res=(int*)malloc(sizeof(int)*(az+bz));
    for(i=0;
    for(i=0;i<bz;)
    {
      if(b[i]!=b[i+1])
        res[j++]=b[i++];
      if(b[i]==b[i+1])
      {
        while((b[i]==b[i+1])&&(i<bz))
          i++;
        res[j++]=b[i++];
      }
     }
     *cz=j;
     qsort(res,*cz,sizeof(int),compare);
    return res;
    }

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,776
    I suggest that you rename testit to distinct_merge or unique_merge or something like that. Before you implement unique_merge, implement two other functions, making sure you test them to be sure they work individually: remove_duplicates and merge.

    remove_duplicates takes a pointer to a sorted array and the size of the array, then removes the duplicates in-place, returning the final number of elements in use. This in-place removal can be done by overwriting the duplicates by copying the unique values in their place, then you just don't care about the remaining elements beyond the number you return.

    merge would take the same parameters as unique_merge, except that it assumes that the arrays are already sorted with the duplicates removed.

    You then implement unique_merge using qsort, remove_duplicates, and merge.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to check if an array of doubles is sorted?
    By Lina_inverse in forum C Programming
    Replies: 20
    Last Post: 10-06-2012, 03:12 PM
  2. How to detect if a array is sorted.
    By just_rookie in forum C++ Programming
    Replies: 14
    Last Post: 09-10-2012, 05:35 AM
  3. Selecting pivot in sorted array.
    By infantheartlyje in forum General Discussions
    Replies: 4
    Last Post: 10-14-2011, 09:43 AM
  4. how delete 1th element of a sorted array
    By vicky_4040 in forum C Programming
    Replies: 4
    Last Post: 10-11-2009, 06:12 AM
  5. finding max/min in a sorted array
    By Crcullen3916 in forum C++ Programming
    Replies: 9
    Last Post: 09-23-2008, 02:18 AM

Tags for this Thread