please help in C language and would successfully run in VM E

please help in C language and would successfully run in VM.

Exercise 3.6. Write a function to compute the sum of two n-dimensional Vectors. The sum of two vectors 1, 2, ...,an) and y (y 1, y2, yn) is the vector an yn). The function should implement the following specification: 1 Computes the sum of two n dimensional vectors, z and y 2 and stores it in vector sum. Returns 0 if successfu or sum is NULL and 2 if n o 3 -1 if any of T, y, i 5 int vectorsum Cint x CJ, int y CJ, int n int sum CJ)

Solution

#include<assert.h>
#include<stdlib.h>

int vectorSum(int x[], int y[], int n, int sum[]){
  
    if(x == NULL || y == NULL || sum == NULL)
        return -1;
    if(n <= 0)
        return -2;

    // compute the sum
    int i;
    for(i = 0;i < n;i++)
        sum[i] = x[i] + y[i];

    return 0;
}

int main(void){
    int n = 5, x[n], y[n], sum[n], error;

    int i;
    for(i = 0;i < n;i++){
        x[i] = i;
        y[i] = i - 2;
    }
    error = vectorSum(x, y, n, sum);
    assert(!error);
    for(i = 0;i < n;i++)
        assert(sum[i] == 2 * i - 2);

    error = vectorSum(NULL, y, n, sum);
    assert(error);

    error = vectorSum(x, NULL, n, sum);
    assert(error);

    error = vectorSum(x, y, n, NULL);
    assert(error);

    error = vectorSum(x, y, 0, sum);
    assert(error);

    return 0;
}

please help in C language and would successfully run in VM. Exercise 3.6. Write a function to compute the sum of two n-dimensional Vectors. The sum of two vecto

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site