0

This code is supposed to recieve to arrays and then call function to return them in 1 array but I don't know how to print the last array returned from the function thanks in advance ???

and now I write anything because it says that the post is mostly code :D :D

#include <stdio.h>
#include <stdlib.h>

int join_arrays(int *array1, int *array2, int arr1_size, int arr2_size);

int main() {
    int size_arr1, size_arr2, i, num1 = 1, s;

    printf("Please enter the size of the first array: ");
    scanf("%d", &size_arr1);
    int arr1[size_arr1];

    printf("start fill your first array: \n");
    for (i = 0; i < size_arr1; i++) {
        printf("enter element number %d: ",num1);
        scanf("%d", &arr1[i]);
        num1++;
    }
    num1 = 1;

    printf("Please enter the size of the second array: ");
    scanf("%d", &size_arr2);
    int arr2[size_arr2];
    int *ptr1_last;

    printf("start fill your second array: \n");
    for (i = 0; i < size_arr2; i++) {
        printf("enter element number %d: ", num1);
        scanf("%d", &arr2[i]);
        num1++;
    }

    ptr1_last = join_arrays(arr1, arr2, size_arr1, size_arr2);

    printf("sorted array= \n");

    for (s = 0; s < (size_arr1 + size_arr2); s++) {
        printf("%d\n", ptr1_last);
    }

    return 0;
}

int join_arrays(int *array1, int *array2, int arr1_size, int arr2_size) {
    int counter_arr1, counter_arr2, m = 0;
    int last_arr[arr1_size + arr2_size];

    for (counter_arr1 = 0; counter_arr1 < arr1_size; counter_arr1++) {
        last_arr[counter_arr1]=array1[counter_arr1];
    }
    for (counter_arr2 = counter_arr1; counter_arr2 < (arr1_size + arr2_size); counter_arr2++) {
        last_arr[counter_arr2] = array2[m];
        m++;
    }

    return last_arr[0];
}
3
  • 2
    last_arr array is local to join_arrays. It will no longer exist when join_arrays returns. You should create a large array to hold the result in the calling function and then pass a pointer to it to join_arrays. Commented Nov 22, 2015 at 11:47
  • I assigned a pointer to the function so it can receive the last arr is it okay? it is in the coed int *ptr1_last; Commented Nov 22, 2015 at 11:50
  • 1
    Your problem is that join_arrays does not in any way return an array, it only returns the first integer of an array. As user1320881 says you could pass an output array from the calling function. Or you could use malloc to make room for an array. Commented Nov 22, 2015 at 11:50

2 Answers 2

1

Modified the code to create the receiving array in main and pass a pointer to it to the merge function because the local array last_arr would no longer exist when the function returned in your code.

#include <stdio.h>
#include <stdlib.h>

//Prototype changed to include a pointer to the receiving array, also no longer returns a value.
void join_arrays(int *last_arr, int *array1,int *array2,int arr1_size,int arr2_size);

int main()
{
    int size_arr1,size_arr2,i,num1=1,s;

    printf("Please enter the size of the first array: ");
    scanf("%d",&size_arr1);
    int arr1[size_arr1];

    printf("start fill your first array: \n");
    for(i=0; i<size_arr1; i++)
    {
        printf("enter element number %d: ",num1);
        scanf("%d",&arr1[i]);
        num1++;
    }
    num1=1;

    printf("Please enter the size of the second array: ");
    scanf("%d",&size_arr2);
    int arr2[size_arr2];
    int *ptr1_last;

    printf("start fill your second array: \n");
    for(i=0; i<size_arr2; i++)
    {
        printf("enter element number %d: ",num1);
        scanf("%d",&arr2[i]);
        num1++;
    }

    int last_arr[size_arr1 + size_arr2];    //Create receiving array here
    join_arrays(last_arr, arr1,arr2,size_arr1,size_arr2);  //And pass it to the function.

    printf("merged array= \n");

    for(s=0;s<(size_arr1+size_arr2);s++)
    {
        printf("%d\n", last_arr[s]);
    }

    return 0;
}

void join_arrays(int *last_arr, int *array1,int *array2,int arr1_size,int arr2_size)
{
    int counter_arr1, m=0;
    for(counter_arr1=0; counter_arr1<arr1_size; counter_arr1++)
    {
        last_arr[counter_arr1]=array1[counter_arr1];
    }
    for(; counter_arr1<(arr1_size+arr2_size); counter_arr1++)
    {
        last_arr[counter_arr1]=array2[m];
        m++;
    }   
}
Sign up to request clarification or add additional context in comments.

Comments

1

With that function you return only the first element of the last_array, you should create a global array so it's visible in all functions, or return a pointer of the last_array[0] position in memory

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.