Add Array Problem!

When i try to add it up, it show me a 0;
How fix this?;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

int add_arrays(int a[], int b[], int n);
int main ()
{
    int b [5]={1,2,3,4,5};
    int a [5] ={1,2,3,4,5};
    
    int total= add_arrays(a,b, 5);
    cout << total << endl;
}
int add_arrays(int a[], int b[], int n)
{
    int  z = 0;
    
    for (int i = 0; i < n; i++)
    {
        z+= a[i] + b[i];
    }
    return 0;
}


Can someone kinda check this one to see what wrong with it?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

int ture ( int a[], int b[], int d);
int main ()
{
    int b [5]={1,2,3,4,5};
    int a [5] ={1,2,3,4,5};
    
    int total= ture(b, a, 5);
    cout << total << endl;
}
int ture ( int a[], int b[], int d)
{
    const double NUM = 10;
    for (int d = 0; d < 10; d++)
    {
        if (a[d] != b[d])
        {
            return false;
        }
    }
    return true;
}
Last edited on
The initializer of total is zero because the value returned by the add_arrays function is always zero. Replace the return statement within add_arrays's compound statement with return z; to fix the problem.

1
2
3
4
5
6
7
8
9
10
11
int add_arrays(int a[], int b[], int n)
{
    int  z = 0;
    
    for (int i = 0; i < n; i++)
    {
        z+= a[i] + b[i];
    }

    return z;
}
Topic archived. No new replies allowed.