There are a few ways to do this. By far the easiest (read as sloppiest) is to make the matrix array a global variable, do this by declaring it outside of any function. The classier, and by popular opinion better way, is to pass a pointer to the matrix array to the function. For the later option I would declare the matrix in main() and pass it's pointer as needed to the other functions but that's just me.
That's why I suggested moving the matrix array to the main function, this way you are passing a pointer to the calc() function so you can still work with the array but you don't have to worry about recursivley moving each piece of data back to main().
#include<iostream>
usingnamespace std;
#include <iomanip>
//-------------------------------------------------
int input()
{
int rows;
cout << "please enter the amount of rows " << endl;
cin >> rows;
rows=rows-1;
return rows;
}
//--------------------------------------------------
int calc (int rows, int * d)
{
int s=0;
for ( int r=0;r <= rows && s <= rows; r++)
{
cout << " Please enter the value of " << r+1 <<"," << s+1 << endl;
cin >> d[r,s];
cout << d[r,s] << endl;
if (r==rows)
{
r=-1;
s=s+1;
}
}
return 0;
}
//--------------------------------------------------
int inversion()
{
return 0;
}
//--------------------------------------------------
int main()
{
int s=input();
int matrix [s][s];
//matrix [0][1]=2;
//cout << &(matrix) << endl;
calc(s, *(matrix));
cout << matrix [0][0] << endl;
return 0;
}
I managed to get it mostly working but for some reason when I do matrix[0][0] it doesn't output 0,0 but seems to output 2,0 which to me makes no sense. Anyone able to shine some light on this?
am guessing its something to do with d[r][s] part.