Return Array From Function

Hi Friends. I wrote a program to pass three arrays to a function and added them. Now i am trying to return the added array back to main function .. i am not getting any idea.. i did this code... kindly help me... Thanks

[#include<iostream>
using namespace std;
float AddArrays(float x[5],float y[5],float z[5]);
void main()
{
float a[5],b[5],c[5];
for(int i=0;i<5;i++)
{
cin>>a[i];
}
for(int i=0;i<5;i++)
{
cin>>b[i];
}

float r=AddArrays(a,b,c);
cout<<"Addition = "<<r<<endl;
system("pause");
}
float AddArrays(float x[5],float y[5],float z[5])
{
for(int i=0;i<5;i++)
{
z[i]=x[i]+y[i];
return z[i];
}]
Last edited on
Function main() must return an int. void main() is not valid.

Function AddArrays() does not need to return anything. Make it type void.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<iostream>

using namespace std;

void AddArrays(float x[5], float y[5], float z[5]);

int main()
{
    float a[5], b[5], c[5];
    
    for (int i=0; i<5; i++)
    {
        cin >> a[i];
    }
    
    for (int i=0; i<5; i++)
    {
        cin >> b[i];
    }

    AddArrays(a,b,c);
    
    cout << "Addition = ";
    for (int i=0; i<5; i++)
    {
        cout << c[i] << "  ";
    }
    cout << endl;
    
    system("pause");
}

void AddArrays(float x[5], float y[5], float z[5])
{
    for (int i=0;i<5;i++)
    {
        z[i] = x[i] + y[i];
    }
}
Last edited on
closed account (E0p9LyTq)
http://stackoverflow.com/questions/3473438/return-array-in-a-function
Arrays work just like pointers
Topic archived. No new replies allowed.