-1

This is intended to be a function to add the elements of two same-sized integer arrays and return pointer to a third array.

this is required /

int *addTwoArrays(int *a1, int *b1, int size);

The function should follow the following rules:

  • If the sum for any element is negative, make it zero.
  • If a1 and b1 point to the same array, it returns a NULL
  • If any input array is NULL, it returns a NULL.

and I need to call this function with the following arrays and print the sums (print sum array elements in separate lines for cases i, ii below).

case i.

int a1[] = {1, -15, 2, 14, 3, -13, 0};
int b1[] = {0, 16, 2, -15, -3, 10, 0};

case ii.

int a2[] = {100, 101, 200, -3011};
int b2[] = {1000, 1010, -300, 10000};

i also cant use any external libraries (other than the default stdio.h)

#include<stdio.h>

int sum[];

int * addTwoArrays(int * a1, int * b1, int size) {
    if (a1 == b1) {
        return NULL;
    }
    if ((a1 == NULL) || (b1 == NULL)) {
        return NULL;
    }
    for (int i = 0; i < size; i++) {
        sum[i] = 0;
    }
    for (int i = 0; i < size; i++) {
        sum[i] = a1[i] + b1[i];
    }
    printf("Sum is: ");
    for (int i = 0; i < size; i++) {
        if (sum[i] < 0) {
            sum[i] = 0;
        }
        printf("\t%d\t", sum[i]);
    }

}
int main() {
    int i;

    //for the execution of case1
    int a1[] = {1, -15, 2, 14, 3, -13, 0};
    int b1[] = {0, 16, 2, -15, -3, 10, 0};
    int size = 7;
    printf("\ncase 1:\n");
    addTwoArrays(a1, b1, size);

    //for the execution of case2
    int a2[] = {100, 101, 200, -3011};
    int b2[] = {1000, 1010, -300, 10000};
    size = 4;
    printf("\n\ncase 2:\n");
    addTwoArrays(a2, b2, size);
}

I'm sure there are better ways as this (more compact ways) but I'm new to c, and I need to compress this as best as I can.

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
  • what simplification are you looking here? looks fine to me, however, you can make code compact by following best practices. – roottraveller Oct 20 '20 at 05:33
  • 6
    `int sum[];` This obviously won't compile, and you can't define a global array of unknown size. The function `addTwoArrays` will need to dynamically allocate the array for results and return a pointer to it. – dxiv Oct 20 '20 at 05:36
  • See https://stackoverflow.com/questions/5378768/returning-arrays-pointers-from-a-function – Martheen Oct 20 '20 at 05:46
  • 2
    Can't use `stdlib.h`? – rohit89 Oct 20 '20 at 06:04
  • The error handling of this & that `== NULL` could through documentation be left to the caller. The printing (any form of I/O) _should_ be done by the caller. – Lundin Oct 20 '20 at 06:13
  • I would suggest to use `calloc` inside `addTwoArrays`. If you use [GCC](http://gcc.gnu.org/) compile with all warnings and debug info (so `gcc -Wall -Wextra -g`). See also [clang-analyzer](https://clang-analyzer.llvm.org/). Read also [*Modern C*](https://modernc.gforge.inria.fr/) and [this C reference](https://en.cppreference.com/w/c) – Basile Starynkevitch Oct 20 '20 at 06:18
  • "If a1 and b1 point to the same array, it returns a NULL" and `if (a1 == b1) { return NULL; }` is unnecessary. No good reason for it. Better to allow `a1 == b1` and "compress". – chux - Reinstate Monica Oct 22 '20 at 10:32
  • As calling does not use the "pointer to a third array" and printing happens inside `addTwoArrays()`, there is no requirement about the return pointer's value. Could change global `int sum[];` --> `int sum[1];` and return the meaningless `return sum;` – chux - Reinstate Monica Oct 22 '20 at 11:02

3 Answers3

1

your function is wrong because you didn't return any object

first thing you must know is if you wanna build a array dynamically you had to use

malloc

function but in simpler way you can just get sum array as input parameter if i wanna write your example i prefer write like this

#include <stdio.h>

int* addArray(int* sum, int* a, int* b, int len) {
    int i;
    // check input params
    if (sum == NULL ||
        a == NULL ||
        b == NULL ||
        a == b) {
        return NULL;
    }
    // calculate sum
    for (i = 0; i < len; i++) {
        sum[i] = a[i] + b[i];
        if (sum[i] < 0) {
            sum[i] = 0;
        }
    }
    return sum;
}

int main(){
    int i;
    int arr1[5] = {10, 20, 30, 40, 50};
    int arr2[5] = {-10, 100, 20, -200, 10};
    int sum[5];
    int len;

    len = sizeof(sum) / sizeof(sum[0]);

    addArray(sum, arr1, arr2, len);
    printf("Sum: ");
    for (i = 0; i < len; i++) {
        printf("%d,", sum[i]);
    }

}

and with malloc function for create dynamic array you can see the following code unfortunately you had to use stdlib.h

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

int* addArray(int* a, int* b, int len) {
    int i;
    int* sum;
    // check input params
    if (a == NULL ||
        b == NULL ||
        a == b) {
        return NULL;
    }
    sum = (int*) malloc(sizeof(int) * len);
    // calculate sum
    for (i = 0; i < len; i++) {
        sum[i] = a[i] + b[i];
        if (sum[i] < 0) {
            sum[i] = 0;
        }
    }
    return sum;
}

int main(){
    int i;
    int arr1[5] = {10, 20, 30, 40, 50};
    int arr2[5] = {-10, 100, 20, -200, 10};
    int* sum;
    int len;

    len = sizeof(arr1) / sizeof(arr1[0]);

    sum = addArray(arr1, arr2, len);
    printf("Sum: ");
    for (i = 0; i < len; i++) {
        printf("%d,", sum[i]);
    }

}
Ali Mirghasemi
  • 462
  • 2
  • 7
0

Yep, but in the original code, in the main function, it doesn't get the return value of addTwoArrays() function. so we can change the code like this.

#include<stdio.h>
    
    void addTwoArrays(int * a1, int * b1, int size) {
        if (a1 == b1 || a1 == NULL || b1 == NULL) {
            return NULL;
        }
        printf("Sum is: "); 
        int sum;
        for (int i = 0; i < size; i++) {
           sum = a1[i] + b1[i];
            if (sum < 0) {
                sum = 0;
            }
            printf("\t%d\t", sum);
        }
    
    }
SweetDream
  • 49
  • 2
0

Why does the "addTwoArrays" function has a return value "int *", if we can't use a library other than stdio.h?

Lets say that the return value is only for checking. (if the function has succeeded or not.)

#include<stdio.h>

int * addTwoArrays(int * a1, int * b1, int *c1, int size) {
    if ((a1 == NULL) || (b1 == NULL)||(a1 == b1)) return NULL;
    
    int i;

    for (i = 0; i < size; i++) {
        c1[i] = a1[i] + b1[i];

        if (c[i] < 0) c[i] = 0;
    }

    return c1;
}

int main() {
    int i, size;

    //for the execution of case1
    int a1[] = {1, -15, 2, 14, 3, -13, 0};
    int b1[] = {0, 16, 2, -15, -3, 10, 0};
    int c1[7];
    size = 7;

    printf("Case 1:\n\tSum1:"); 
    if (addTwoArrays(a1, b1, c1, size)!=NULL) for (i=0; i<size; i++) printf(" %d", c1[i]);
    puts("");

    //for the execution of case2
    int a2[] = {100, 101, 200, -3011};
    int b2[] = {1000, 1010, -300, 10000};
    int c2[4];
    size = 4;

    printf("Case 2:\n\tSum2:"); 
    if (addTwoArrays(a2, b2, c2, size)!=NULL) for (i=0; i<size; i++) printf(" %d", c2[i]);
    puts("");

    return 0;
}

The answer with the stdlib.h:

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

int * addTwoArrays(int * a1, int * b1, int size) {
    if ((a1 == NULL) || (b1 == NULL)||(a1 == b1)) return NULL;
    
    int i, *sum=malloc(size*sizeof(int));

    if (sum==NULL) return NULL;

    for (i = 0; i < size; i++) {
        sum[i] = a1[i] + b1[i];

        if (sum[i] < 0) sum[i] = 0;
    }

    return sum;
}

int main() {
    int i, size, *sum;

    //for the execution of case1
    int a1[] = {1, -15, 2, 14, 3, -13, 0};
    int b1[] = {0, 16, 2, -15, -3, 10, 0};
    size = 7;

    printf("Case 1:\n\tSum1: ");
    if ((sum=addTwoArrays(a1, b1, size))!=NULL){
       for (i=0; i<size; i++) printf(" %d", sum[i]);
       free(sum);
    }
    puts("");

    //for the execution of case2
    int a2[] = {100, 101, 200, -3011};
    int b2[] = {1000, 1010, -300, 10000};
    size = 4;

    printf("Case 2:\n\tSum2: ");
    if ((sum=addTwoArrays(a2, b2, size))!=NULL){
       for (i=0; i<size; i++) printf(" %d", sum[i]);
       free(sum);
    }
    puts("");

    return 0;
}