I want to send a 2D array from the main function to another function, and I want to return a 1D array. I do not know how to return an array from a C++ function. When I do it for a scalar value (not a vector or array), it works fine. But with arrays, I have problems. Here is my code:
#include <iostream>
using namespace std;
float display(float n[3][2]); // declare my function
int main() // main function
{
float num[3][2] = { // a dummy 2D array
{3.3, 4.3},
{9.3, 5.3},
{7.3, 1.3}
};
float a[3];
a = display(num); // send array to display function // line 13
for(int i = 0; i < 3; ++i)
{
cout << "reurned array is : " << a[i] << endl;
}
return 0;
}
float display(float n[3][2]) // define my function
{
float b[3];
cout << "Displaying Values: " << endl;
for(int i = 0; i < 3; ++i)
{
b[i] = n[i][0];
for(int j = 0; j < 2; ++j)
{
cout << n[i][j] << " ";
}
}
cout << endl;
for(int i = 0; i < 3; ++i)
{
cout << "actual array is : " << b[i] << endl;
}
return b; // line 39
}
And this is the error I am getting:
/main.cpp||In function ‘int main()’
/main.cpp|13|error: incompatible types in assignment of ‘float’ to ‘float [3]
/main.cpp||In function ‘float display(float (*)[2])’
/main.cpp|39|error: cannot convert ‘float*’ to ‘float’ in return