Beansolder
Beansolder

Reputation: 145

Take positive numbers from two arrays and create third from it in

Establish two arrays A[] and B[] of m and n elements. Write a function which forms the third array C[] of two given series so that all the positive elements of arrays A and B become the elements of the third string. Memory for a set of C is allocated within the function. A pointer to a dynamically allocated string back as a result of the function. As an argument to a function to return the cursor using pointer dimension string.

This is what I've done so far. I am not sure how to make the third array with pointer from first two.

#include <stdio.h>

#define max_duzina 1000

int main(){
    int A[max_duzina];
    int B[max_duzina];
    int C[max_duzina];
    int m, n;
    //put array dimension
    printf("Uneti dimenzije niza A:\n");
    scanf("%d", &m);
    //put elements of array
    printf("Uneti elemente niza A:\n");
    for(int i = 0; i < m; i++){
        scanf("%d", &A[i]);
    }

    printf("Uneti dimenzije niza B:\n");
    scanf("%d", &n);

    printf("Uneti elemente niza B:\n");
    for(int j = 0; j < n; j++){
        scanf("%d", &B[j]);
    }
    //array A have next elements...
    printf("Niz A se sastoji iz sledecih elemenata:\n");
    for(int i = 0; i < m; i++){
        printf("%3d", A[i]);
    }

    printf("\nNiz B se sastoji iz sledecih elemenata:\n");
    for(int j = 0; j < n; j++){
        printf("%3d", B[j]);
    }
}

Upvotes: 1

Views: 64

Answers (1)

chux
chux

Reputation: 154075

...not sure how to make the third array with pointer from first two.

Some pseudo code for OP

Form function signature
  int *BeanAdd(pointer `const int *`, number of A elements, 
               pointer `const int *`, number of B elements)
Count number of positive numbers in `A[]`
Count number of positive numbers in `B[]`
Allocate memory for `C[]`
  int *C = malloc(sizeof *C * (A positive number count + B positive number count))
Was allocation successful?
Copy `A[]` positive elements to C.
Append `B[]` positive elements to C.
Return C

Upvotes: 3

Related Questions