Moving an array to another function?

Here is my code

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<iostream>
using namespace 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  matrix [rows][rows];
 int s=0;
  
/////////////////////////////////////////////////////
for ( int r=0; r <= rows && s <= rows ; r++)
    {
   
      cout << " Please enter the value of " << r+1 <<"," << s+1 << endl;
      cin >>  matrix [r][s];
       

      if (r==rows)
	{
	  r=-1;
        s=s+1;
       
      }
    }
////////////////////////////////////////////////////
 return &matrix[0][0];
}
//--------------------------------------------------

int inversion()
{
  return 0;
}
//--------------------------------------------------


int main()
{
  int s=input(); // size of the matrix
  cout << calc(s) << endl;
  return 0;
}



What I'm trying to do is move the array named matrix to main() and then move it to inversion() although i have no idea how to do it.

Any help would be much appreciated.
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.
I was wanting to move a pointer from int calc to in main but i have no idea how to do that. :( Could anyone show me please?
Last edited on
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().
When you are dealing with large blocks of memory you should use poitners to avoid moving overhead and of course save resources.
Made some progress
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include<iostream>
using namespace 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.
Last edited on
Topic archived. No new replies allowed.