Search code examples
carraysdynamicallocation

C - using dynamic arrays


I have a problem in C. This is the question:

Develop a C function ADDER that adds two integer arrays together. ADDER should have only two parameters, which are the two arrays to be added. The second array argument will hold the sum of arrays on exit. Both parameters should be passed by reference.

Write a C program to test the ADDER function with the call ADDER (A, A) where A is an array to be added to itself. Array A can be of any size with any values. Write, compile, and execute the program.

Explain the results of the program.


So far I have solved it this way and it works just fine:

#include <stdio.h>

// using namespace std;

const int SIZE = 5;

/* Adds two arrays and saves the result in b
 * Assumes that b is larger than or equal to a in size
 */

void ADDER(int (&a)[SIZE], int (&b)[SIZE]) {
    int aSize, bSize, i; /* variable declaration */
    /* find out the sizes first */
    aSize = sizeof (a) / sizeof (int);
    bSize = sizeof (b) / sizeof (int);
    /* add the values into b now */
    for (i = 0; i < aSize; i++) {
    b[i] = b[i] + a[i];
    }
    /* we have the sum at the end in b[] */
}

/* Test program for ADDER */

int main() {
int i; /* variable declaration */
int a[] = {1, 2, 3, 4, 5}; /* the first array */

/* add them now */
ADDER(a, a);
/* print results */
printf("\nThe sum of the two arrays is: ");
for (i = 0; i < SIZE; i++) {
    printf("%d ", a[i]); /* print each element */
}
return 0;
}

The problem is, I have to use dynamic arrays and use malloc and realloc in the program to compute the size of the array on the fly. Instead of specifying the array size and the elements itself, I want the program to ask the user for input and the user enters the array and the size is determined there. It should all be dynamic. I do not know how this is done. Can anyone please help me out! thanks!

Also I have to explain how the array is added to itself the result is saved in "a" and the original array is lost replaced by the sum. how can I explain this?


Solution

  • Here is how your program would look like

    int size; //global variable
    
    void ADDER(int *a, int *b) {
        int i;
        for (i = 0; i < size; i++) {
            b[i] += a[i];
        }    
    }
    
    int main(){
        //ask the user to input the size and read the size
        int *a = (int *)malloc(size*sizeof(int));
        int *b = (int *)malloc(size*sizeof(int));
    
        //fill up the elements
        Adder(a,b);
        //print out b
        free(a->array);
        free(b->array);
    }
    

    ALthough its not wise to use globals, the bottom line here is that adder somehow needs to know the size of the array and somehow you need to convey the size to the ADDER function. If that can't be done through parameters, you have to use globals.

    Another option would be to use structures.

    typedef struct myArray{
        int *array;
        int length;
    }ARRAY;
    
    void ADDER(ARRAY *a, ARRAY *b) {
        int i;
        for (i = 0; i < b->length; i++) {
            b->array[i] += a->array[i];
        }    
    }
    
    int main(){
        int size; //local variable
        //ask the user to input the size and read into the 'size' variable
        ARRAY *a, *b;
        a->array = (int *)malloc(size*sizeof(int));
        b->array = (int *)malloc(size*sizeof(int));
        a->length = b->length = size;
    
        //fill up the elements
        Adder(a,b);
        //print out b.array
        free(a->array);
        free(b->array);
    }