C Implmentation:Error in Pointer implementation for even size O/p array


  • 0
    A

    Hello,
    My below solution works fine using Arrays for BOTH Odd & Even numbered output array.

    However, my solution using Pointers works fine for Odd numbered output array; but NOT for EVEN numbered Output array.

    QUERY:
    Please opine what component is causing error in line:
    return((double)((*nums3Ptr + *(nums3Ptr+1))/2.));

    Thank you in advance.

    1. ARRAY SOLUTION - PASS:

    double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {

    int nums3Size = nums1Size+nums2Size;
    int nums3[nums3Size];
    int i = 0,j = 0,k = 0;
    double median = 0.0;
    
    while((i < nums1Size)||(j < nums2Size))
    {
        
        if((i < nums1Size)&&(j < nums2Size)) {
            
    
        if(nums1[i] < nums2[j]) {
            nums3[k] = nums1[i];
            i++;
            //printf("i = %d, nums3[%d]  = %d\n",i,k,nums3[k]);
        }
        else {
            nums3[k] = nums2[j];
            j++;
            //printf("j = %d, nums3[%d] = %d\n",j,k,nums3[k]);
        }
        k++;
        continue;
        }
    
        if((i < nums1Size)&&(j >= nums2Size)) {
            nums3[k] = nums1[i];
            i++;
            //printf("i = %d, nums3[%d] = %d\n",i,k,nums3[k]);
        }
        
        if((i >= nums1Size)&&(j < nums2Size)) {
            nums3[k] = nums2[j];
            j++;
            //printf("j = %d, nums3[%d] = %d\n",j,k,nums3[k]);
        }        
        k++;
    }
    
    return((nums3Size % 2)?nums3[(nums3Size - 1)/2]:(double)(nums3[nums3Size/2 - 1] + nums3[nums3Size/2])/2.);
    

    }


    1. POINTER SOLUTION - FAIL:

    double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {

    int nums3Size = nums1Size+nums2Size;
    int nums3[nums3Size];
    //int i = 0,j = 0,k = 0;
    int *nums1Ptr = nums1;
    int *nums2Ptr = nums2;
    int *nums3Ptr = nums3;
    
    double median = 0.0;
    
    while((nums1Ptr < nums1 + nums1Size)||(nums2Ptr < nums2 + nums2Size))
    {
        
        if((nums1Ptr < nums1 + nums1Size)&&(nums2Ptr < nums2 + nums2Size)) {
            
    
        if(*nums1Ptr < *nums2Ptr) {
            *nums3Ptr = *nums1Ptr;
            nums1Ptr++;
            printf("nums3Ptr %d\n",*nums3Ptr);
        }
        else {
            *nums3Ptr = *nums2Ptr;
            nums2Ptr++;
            printf("nums3Ptr %d\n",*nums3Ptr);
        }
        nums3Ptr++;
        continue;
        }
    
        if((nums1Ptr < nums1 + nums1Size)&&(nums2Ptr >= nums2 + nums2Size)) {
            *nums3Ptr = *nums1Ptr;
            nums1Ptr++;
            printf("nums3Ptr %d\n",*nums3Ptr);
        }
        
        if((nums2Ptr >= nums2 + nums2Size)&&(nums2Ptr < nums2 + nums2Size)) {
            *nums3Ptr = *nums2Ptr;
            nums2Ptr++;
            printf("nums3Ptr %d\n",*nums3Ptr);
        }        
        nums3Ptr++;
    }
    
    if(nums3Size % 2) {
        nums3Ptr = nums3 + (nums3Size - 1)/2;
        return(*nums3Ptr);
    }
    else {          
          return((double)((*nums3Ptr + *(nums3Ptr+1))/2.));
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.