When I try to add the two array together it show me 0?
`

#include <iostream>
using namespace std;

int add_arrays(int a[], int b[], int n);

int main ()
{
    int numbers[5]={1,2,3,4,5};
    int numb [5] = {6,7,8,9,10};
    int total= add_arrays(numbers, numb, 5);
    cout << total << endl;
}
int add_arrays(int a[], int b[], int n)
{
    for (int i = 0; i < n; i++)
    {
        n = a[i] + b[i];
    }
    return n;
}

`

Recommended Answers

All 3 Replies

n = a[i] + b[i];

replace it with this :

n+=a[i]+b[i]

In your function you need to declare a local variable to hold the sum of the array elements and then return that. You do not want to store the sum in n.

In your function you need to declare a local variable to hold the sum of the array elements and then return that. You do not want to store the sum in n.

yes, in you function n is not declared . so decalre it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.