I am trying to write an OLS regression in C++. I want the user to specify how many observations and variables goes into the X (independent variable matrix). I want to write this as a function outside the int main() function. But everytime I return the array, I get a type error. How do I specify that I want function input_x to return a matrix/array?
/******************************************************************************
Online C++ Compiler.
*******************************************************************************/
#include <iostream>
using namespace std;
int input_x(int num_rows, int num_cols){
int mat[num_rows][num_cols];
int user_input;
for (int i=0; i<num_rows; i++){
for (int j=0; j<num_cols; j++){
cout<<"Enter the value X:";
cin>>user_input;
mat[i][j]=user_input;
}
}
return mat;
}
int main()
{
int mat[1000][1000];
int input;
int rows;
int cols;
cout<<"How many row?";
cin>> rows;
cout<<"How many columns?";
cin>> cols;
//Enter the X data
input_x(rows,cols);
return 0;
}
matisint**. But you can't return a function local c-array like that. You need to use something that has proper copy semantics, like astd::vector<std::vector<int>>.input_xis not declared to return a 2D integer array, it's declared to return an integer. And you can't actually return a native array, per se. See Return array in a function.int man[1000][1000]-- That's C, not C++, and using this C construct in C++ is really bad practice. Check outstd::vector. Or Boost.uBLAS if you want to go into matrices hardcore. But I guess before you go there, you need to understand basic data structures and function calls...matisint [num_rows][num_cols]and notint**which you incorrectly claimed.