0

I have an array, which is now static. This are the operations I do with it. Firstly I create a two-dimensional array. Then I fill it in, using cycles. And then I send it to function, where there are also cycles which are used. Here I 'd like to post some sample code, which is similar to mine.

bool picture[20][20]; //here's my array right now. Pretty ugly. Just for testing.
for (int y=0;y<Height;y++)
{
    for (int x=0;x<Width;x++)
    {
        if (treshold<middle)
        {
            picture[x][y]=1;
        }
        else
        {
            picture[x][y]=0;
        }
     }
}
//Here's an example of filling an array

leftk = left(picture,widthk, heightk); //That's how I use a function

int left(int picture[200][200],int row,int col)
{
    for (int x = 0;  x <=row-1; x++)
    {
        for (int y = 0; y <=col-1 ;y++)
        {
            if (picture1[x][y]==1)
            {
                return x;
            }
        }
    }
}
//And that's the function itself

So here I need to switch my array to a dynamic one. That's how I declare my dynamic array

bool** picture=new bool*[size];
    for(int i = 0; i < size; ++i)
        picture[i] = new bool[size];

//size is just a variable. 

As for statically declared cycles, everything is very simple. Sending this array as a parameter to function.

I've already managed to create a dynamic array, it's simple. Then I fill it in with numbers. No problems here too. But I can't understand, how to send an array to function and moreover how to use it there. Could you give me an exaple of modifying two-dimensional arrays in functions. Sorry for such a newbie question. Hope someone will help.

By the way, class wrapping would be a bit confusing here, I think.

0

3 Answers 3

4

A function such as:

Process2DArray(int **pArray, int rowCount, int colCount)

Should suffice the needs assuming its a 2D array that is being operated on. Also, consider using std::vector<std::vector<int>> instead of a multidimensional array allocated manually. This approach will help prevent leaks. The second approach also lets you have jagged arrays.

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

15 Comments

This depends on how the array has been defined. I'd generally prefer wrapping it in a class, because there is no simple solution otherwise.
@JamesKanze : Personally, I'd wrap it in a class or struct if its needed in multiple places or used frequently in the program. Otherwise for one-place uses a vector<vector<int>> would do fine don't you think?
Well, thank you very much, but I've already mentioned that wrapping it in class would be inconvenient. What's more, the array is used only once, so I hope I will escape classes. The first way you showed seems to be quite simple, but needs some explanation. Firstly, I'll post my static C code right now to describe the problem better. Then I will post the declaration of my array. Hope, this will help too. But unfortunately, then I need your help. The action of sending an array and recieving it in the function is still not bright enough.
@BhargavBhat Just a bit awkward to write. std::vector<std::vector<double> > a2d( rows, std::vector<double>( columns ) );
@OP : Please change the function signature to int left(int **picture,int row,int col). Rest of your code looks fine. It should continue to work as it does now. Also, you should use the bool type if thats what you acutally want. I used int just as an example.
|
2

The usual solution is to wrap the array in a class; C doesn't handle arrays very well, and C++ doesn't have any real support for 2D arrays in its library either. So you define either:

class Array2D
{
    std::vector<double> myData;
    int myColumnCount;
    int myRowCound;
    //  ...
};

with accessors which convert the two indexes using i * myColumnCount + j, or:

class Array2D
{
    std::vector<std::vector<double> > myData;
    //  ...
};

with initialization logic ensure that all of the rows have the same length. The first is generally simpler and easier to understand; if you want to increase the number of columns, however, the second is significantly easier.

5 Comments

Unfortunately, I have five arrays to manage, so these methods may be quite a bit "bigger". So I hope to find a solution which will be simplier and smaller. But nevertheless, thanks for your support, I'll keep that method in mind.
I'm not sure what you mean by "bigger". Using std::vector instead of a double* will add a little bit, but typically only about the equivalent of 2 or 3 pointers. Regardless of the size of the array.
I'm sorry, but I'm new to c++. So the std::vector means nothing to me. Of course, I can read about it, but if there's any other ways (except class wrapping), it'd be great
@user1131662 If you're new to C++, what other way do you know to create an array. std::vector is the simplest and the safest, so it's the one which will be taught first in any decent C++ pedagogic text. Long before new or T[].
It seems we use different teaching styles. I haven't been told yet, what's happening in the heart of a compiler, but the things I have already used were new and malloc. But I've never heared of std::vector before.
1

You have several options:

  1. an array of arrays. For example, for int would be int **a which should be able to hold n arrays new int *[n], then go with a for through them and initialized them a[i] = new int[elems_per_line]
  2. a "packed" 1D array int *a = new int[n * elems_per_line], where element (i, j) - 0-based is actually a[i * elems_per_line + j].
  3. you can refine point 1, and have the 2D matrix be "curly" - with lines of different lengths, but you'll need an array to hold each length.

Hope this helps.

1 Comment

Thank you a lot. I've heared few things about the first way. It seems quite complicated to me, sorry. The second one is rather simple. I even tried to use it. But still the same problems with function. I've never hewared about the last one. So if I don't find a perfect solution, I'll read about this one more

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.