Thread: Help with my code returning an array created by the function and it's size

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    2

    Help with my code returning an array created by the function and it's size

    hello everyone,

    So I was requsted coding a program which percieves two arrays and their size from the user,
    the function supposes to create new array which contains all the similiar occurance of arrays in an increasing order the function need to return the size of new array with a pointer and of course the array itself, running tome should be nlogn+mlogn.
    for exmaple two arrays A: 2 5 3 1 2 4 6 2 4 3 5 2
    B: 7 5 2 5 8 1 2 5 9 2
    and the new array is 1 2 2 2 5 5 and it's size is 6.
    So what I did was to write two assists function one that percieves into them elements from the user the second sorts each of them with quick sort. The function itself create new array C and percieves into C numbers from the elements using merging and each time the size of C increases with realloc. My problem is that the program does not work I am trying to return the size of arrays and each time it returns me 2. Also I cannot print C when returning to the main,I will be very glad if someone can help me with that

    here is the code

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include <assert.h>
    int *dyn_array_input(int num);
    void quicksort(int *arr, int first, int last);
    int *similiar_elements(int *A, int *B, int n, int m);
    void main() {
        int *A, *B, *C, n, m;
        printf("Please enter the size of array A n\n ");
        scanf("%d", &n);
        printf("Please enter the size of array B m\n ");
        scanf("%d", &m);
        printf("Please enter numbers into array A\n");
        A = dyn_array_input(n);
        printf("Please enter numbers into array B\n");
        B = dyn_array_input(m);
        C = similiar_elements(A, B, n, m);
        printf("The new array is %d",*C);
        for (int j = 0;j <*C;j++) {
    
    
            printf("%d", C[j]);
    
    
        }
    }
    
    
    int *dyn_array_input(int num) {
        int i = 0, *arr;
        arr = (int*)malloc(num * sizeof(int));
        if (!arr) {
            printf("There is not enough space in the memory\n");
            exit(1);
        }
        for (int i = 0;i < num;i++) {
    
    
            scanf("%d", &arr[i]);
        }
        return arr;
    
    
    
    
    }
    void quicksort(int *arr, int first, int last) {
        int i, j, pivot, temp;
    
    
    
    
        if (first<last) {
            pivot = first;
            i = first;
            j = last;
    
    
            while (i<j) {
                while (arr[i] <= arr[pivot] && i<last)
                    i++;
                while (arr[j]>arr[pivot])
                    j--;
                if (i<j) {
                    temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
            temp = arr[pivot];
            arr[pivot] = arr[j];
            arr[j] = temp;
    
    
            quicksort(arr, first, j - 1);
            quicksort(arr, j + 1, last);
    
    
        }
    }
    int *similiar_elements(int *A, int *B, int n, int m) {\\This is the function
        quicksort(A,0,m);
        quicksort(B, 0,n);
        int *C, physical_size = 0, logical_size = 2, i = 0, j = 0, size_of_array=2;
        C = (int*)calloc(logical_size, sizeof(int));    
    
    
        while (i !=n && j != m) {
    
    
            if (logical_size == physical_size) {
                size_of_array *=2;
                C = (int*)realloc(C,logical_size*sizeof(int));
            }
    
    
            if (A[i] == B[j]) {
    
    
                C[i] = A[i++];
                C[i+1] = B[j++];
            }
            if (A[i] > B[j]) {
                j++;
    
    
            }
            if (B[j] > A[i])
                i++;
    
    
        }
        return &logical_size;
    
    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,319
    I suggest that you declare the function like this:
    Code:
    int *similiar_elements(int *a, int *b, int a_size, int b_size, int *result_size);
    The idea is that you will return a pointer to the result dynamic array that has memory allocated by malloc (or calloc, or realloc), and then you will update *result_size with the size of the result array. Currently, you return &logical_size, which is wrong as you shouldn't be returning a pointer to a non-static local variable.

    Also, it would be good to comment as to what "logical_size" and "physical_size" mean. I assumed that "logical size" would be the number of elements in use an "physical size" would be the number of elements allocated, but from what I see you used the opposite meanings... yet you don't seem to be changing either of them. You also have "size_of_array". I suggest that you choose two, and make sure that you do change them accordingly. In the end, *result_size will be the number of elements in use by the result array, so you need to update that too.

    Note that you do not need to cast the return value of calloc or realloc. By right, you should also take care to handle the case when they return a null pointer, although you probably won't encounter that.
    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

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    2
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 09-25-2012, 01:31 AM
  2. Replies: 8
    Last Post: 09-06-2012, 11:49 AM
  3. Determining size of array returning strange values?
    By edddo in forum C++ Programming
    Replies: 13
    Last Post: 07-28-2011, 03:37 AM
  4. size of array - why function gives size ONE only
    By noob123 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2009, 05:20 PM
  5. Replies: 6
    Last Post: 11-09-2006, 03:28 AM

Tags for this Thread