Mina Nagy
Mina Nagy

Reputation: 41

How to print array returned from function

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];
}

Upvotes: 0

Views: 672

Answers (2)

Davide Ambrosi
Davide Ambrosi

Reputation: 37

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

Upvotes: 1

Unimportant
Unimportant

Reputation: 2096

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++;
    }   
}

Upvotes: 1

Related Questions