-1

I am struggling to figure out how to declare a 2D Array as a global variable so i can use it in all of my methods. So far it is only declared in a single method hence why it cannot be used in other methods.I have figured out how to declare a normal string array by just typing string* array = new string[1] at the start of my code before the methods (i then alter the size of this array later on based of a variable) but i am unsure how to do it with a 2D array:

void WordSearch::ReadSimplePuzzle()

int columns = 9;

int rows = 9;


string **simple2DArray = new string*[columns];

for (int i = 0; i < columns; i++)

         simple2DArray[i] = new string[rows];

//code that populates the array too long to post but not relevant.

I then have a method later on where i need to access the simple2DArray but i cannot figure out how to define it at the start of the code any help would be appreciated.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
Peter Feneley
  • 13
  • 1
  • 2
  • What isn't working with your current code? – Galik Mar 31 '18 at 19:14
  • simple2DArray is only defined in the scope of ReadSimplePuzzle() and i wish to use this 2d array in a different method however i am unsure how to declare it as a global variable. – Peter Feneley Mar 31 '18 at 19:26
  • 1
    *how to declare a 2D Array* -- A `string**` is not an actual 2D array of `std::string`. In addition, your allocation method is highly inefficient and error prone if something happens while allocating the array. You can make use of [this method to do the allocation](https://stackoverflow.com/questions/21943621/how-to-create-a-contiguous-2d-array-in-c/21944048#21944048) – PaulMcKenzie Mar 31 '18 at 19:29
  • 1
    When tempted to use the word "simple" in conjunction with programming, ask yourself, "Who am I trying to fool?" Then correct the syntax error by adding an "m" to the end of "Who." Using `new` and C-style arrays[] is not simple. Those are useful to library writers (although allocators are displacing `new` in practice). Kids, don't try this at home, yet. Use `std::vector`, `std::string` and other standard containers. – Jive Dadson Mar 31 '18 at 19:32
  • I was going to write some example code, but I see the green check mark. I guess it was too simple. :-) – Jive Dadson Mar 31 '18 at 19:41

4 Answers4

2

If you columns and rows variables never change, you can do this:

const int columns = 9;
const int rows = 9;

string simple2DArray[columns][rows];

By statically allocating the memory, you now don't have to worry about freeing it.


Since you clarified that the size is not known until run-time, you will not be able to allocate the memory statically. A very simple solution would be:

std::vector<std::vector<std::string>> simple2DArray; // This will have size 0 at start

Then, in your initialization step, just do this:

simple2DArray.resize(rows);
for (auto& row : simple2DArray)
{
    row.resize(columns);
}

There are other ways to do this, of course, such as allocating all the memory in one block of size rows*columns and then exposing it as if it were a 2-d matrix but that might be overkill for your purposes.

JimPri
  • 1,278
  • 8
  • 17
  • the rows and columns are defined by working out the rows and columns of a text file so yes they dont change once found out but can also be different values based of size the text file used. – Peter Feneley Mar 31 '18 at 19:25
1

My suggestion is hide the array behind a functional interface.

std::string const& getElement(size_t m, size_t n);
void setElement(size_t m, size_t n, std::string const& val);

The calling functions have the abstractions of a 2D array but they don't need to know how the it is represented in code.

In the implementation, you have various options:

  1. Use a 1D array. Map the 2D indices to the right index in the 1D array.
  2. Use a std::vector. Still need to map the indices.
  3. Use a 2D array. No mapping of indices needed.
  4. Use a std::vector<std::vector<std::string>>. No mapping of indices needed.
R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

I am struggling to figure out how to declare a 2D Array as a global variable so i can use it in all of my methods.

As with any global var, you need to declare your pointer in global space:

string **simple2DArray;

and then you can assign to it from inside your method

simple2DArray = new string*[columns];
Killzone Kid
  • 6,171
  • 3
  • 17
  • 37
  • I guess I'm getting downvoted for not condemning the use of raw pointers in OP's example. I condemn! Use vectors or at least smart pointers! There... – Killzone Kid Mar 31 '18 at 19:55
0

If you are asking this for making it easier to solve competitive programming problems, then look at the constraints given in the question.

For example if the matrix can be an N*N with 1 <= N <= 1000 Then you can globally declare
int arr[1000][1000];
Here's some code for a better idea.

//global declarations 
int N;
int arr[1000][1000];

int functionA()
{
  // some code
}

int functionB()
{
   // some code
} 

int main()
{
   // Get the input of both N and your array arr
   // Now you can use them in any where in your code 
    
}