Jan 6, 2017 at 2:32pm UTC
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 Jan 6, 2017 at 2:32pm UTC
Jan 6, 2017 at 2:48pm UTC
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 Jan 6, 2017 at 2:51pm UTC
Jan 6, 2017 at 2:55pm UTC
http://stackoverflow.com/questions/3473438/return-array-in-a-function
Jan 7, 2017 at 11:17am UTC
Arrays work just like pointers