1

What I'm trying to do:

User inputs two numbers. Array is declared using those numbers as dimensions. Function outside main() is filling the array. Array is accessed in main() for further thingies.

What I have problem with:

Function + array combination doesn't seem to work as I think.

What I did:

void tablica1(int h, int w)
{
    int m,n;

    for(m=0; m<h; m++)
        for(n=0; n<w; n++) 
        {
            arr[h][w]=1;
        }
}

What happens:

array arr is inaccessible in tablica1() because it has not been declared in that function.

Of course, when I declare the array in tablica1() it becomes inaccessible in main().

Possible solutions:

  • Passing arr to tablica1() as a reference - no idea how to do that
  • Declaring arr in tablica1() and somehow passing it to main() - no idea how to do that

Other possible solutions?

5
  • 1
    If you want dynamic sizing, you're using the wrong data structure. Consider using a nested std::vector<std::vector<int>> as one possible solution. Commented Nov 17, 2013 at 9:32
  • How would I go about making a multidimensional array with vector? std::vector < int > tab; would be one dimension std:: vector < int, int > tab; ?? Commented Nov 17, 2013 at 9:38
  • Since you are using C++, I would suggest you use std::array cplusplus.com/reference/array/array if you use a C++11 based compiler or cplusplus.com/reference/valarray/valarray/valarray if you have a slightly older compiler. Commented Nov 17, 2013 at 9:39
  • @yasouser and std::array<> will dynamically size... how? In the question: "Array is declared using those numbers as dimensions" Commented Nov 17, 2013 at 9:41
  • @MatthewBlackwind It seems that you have declared arr in the main function. Please post that declaration (even though it doesn't work). This will clarify your question and help you get better answers. Please also specify your compiler (gcc/other); this may be important. Commented Nov 17, 2013 at 10:03

4 Answers 4

5

You can declare the array outside of both, at compilation unit level:

int arr[10][10];

void func() {
    for (int i=0; i<10; i++) {
        for (int j=0; j<10; j++) {
            arr[i][j] = i + j;
        }
    }
}

int main(int argc, const char *argv[]) {
    func();
    std::cout << arr[3][4] << "\n"; // Output will be 7
    return 0;
}

If you want handle a dynamically-sized matrix the most common pattern is to use an std::vector of std::vectors (it's a little more general and therefore a little less efficient than a 2d matrix because each row can have a different length, but in most cases the cost difference is not a big issue)

#include <vector>

std::vector< std::vector< int > > arr;

void func() {
    int height = arr.size();
    int width = arr[0].size();
    for (int i=0; i<height; i++) {
        for (int j=0; j<width; j++) {
            arr[i][j] = i + j;
        }
    }
}


int main() {
    int height = 13;
    int width = 7;

    arr = std::vector< std::vector<int> >(height, std::vector<int>(width));

    func();

    ...
}
Sign up to request clarification or add additional context in comments.

5 Comments

Yes, but I cannot have dynamic sizing when declaring as global
@MatthewBlackwind This answer was updated. The second part is specifically what I was referring to, and should be considered for your solution. (and +1 to the answer, btw).
After the declaration, I handle the array 'arr' in the same way I would handle a normally declared array? as in arr[1][5]=5 etc?
arr = std::vector<int>(height, std::vector<int>(width)); gives me "no matching function for call to std::vector(...)" error
@MatthewBlackwind: Sorry, there was a typo. See edited version.
1

the two solutions you mentioned

1、Passing arr to tablica1() as a reference

void tablica1(int h, int w,int **arr)
{
    int m,n;

    for(m=0; m<h; m++)
        for(n=0; n<w; n++) 
        {
            arr[h][w]=1;
        }
}

void main()
{
  const int h=100,w=100;
  int arr[h][w];
  tablica1(h,w,arr);
}

2、Declaring arr in tablica1() and somehow passing it to main()

int **tablica1(int h, int w)
{
    int m,n;
    int **arr=new int*[h];
    for(int i=0;i<h;i++)
    {
       arr[i]=new int[w];
    }
    //it is best to initialize arr by setting each element 0
    for(m=0; m<h; m++)
        for(n=0; n<w; n++) 
        {
            arr[h][w]=1;
        }
   return arr;

}

void main()
{
  const int h=100,w=100;
  int **arr=tablica1(h,w);
  //do somting

  //delete arr
  for(int i=0;i<h;i++)
  {
    delete []arr[i];
  }
  delete []arr;
}

Comments

0

If you want to declare a dynamic multidimensional array you can do that with the template give below.

     #include<iostream.
     #include <vector>
     using namespace std;
     typedef vector<int> vi;

     vector<vi> arr; // arr is a dynamic two dimensional array.
     vi.assign(10,vi());//10 rows of type vi .

If you want to enter values in arr you can do that by

    vi[0].push_back(a);
    vi[0].push_back(b);  // a,b,c are some example values..
    vi[1].push_back(c);

1 Comment

Looks nice, but i haven't a slightest clue as to how to use it. How would I use it to make a H x W sized array?
0

You can understand using this code

     #include <iostream>
      #include <cstdio>
     #include <vector>
     using namespace std;

     typedef vector <int> vi;

     vector<vi> arr;// dynamic multidimensional array

     int main(){
     cout<<"enter array size\n";
     int h,w,temp;;
     cin>>h>>w;

     arr.assign(h,vi());
     int i, j;
     for(i=0; i < h ;i++)
      for(j=0; j < w; j++)
       {
       cin>>temp;
       arr[i].push_back(temp);
       }

       // for printing
       for(i=0; i < h ;i++){
       for(j=0; j < w; j++)
        {
        cout<<arr[i][j]<<" ";

        }
     cout<<endl;
   }
    return 0;

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.