Points for Axter

lion-g
lion-g used Ask the Experts™
on
Here's the points for the 2D dynamic Array template you are guiding me.

For any further questions, I will post it here.

Cheers,
C++ Beginner
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
>>Here's line 39 within my code:-
>>SomefFunction(MYARRAY_T);
>>From my understanding, here's how I activate the function.
>>Do enlighten me in this area
Can you post the complete code that you have now?

Author

Commented:
Hi Axter,

Here you go.

From my basic knowledge, you need to activate the function by calling it so I called it in my MAIN.


#include <iostream>
#include <stdlib.h>
#include <windows.h>
#include <c:\my documents\fyp\dynamic_2d_array.h>
using namespace std;

void SomefFunction(MYARRAY_T &Src)
{
for (int i = 0;i < 3;++i)
   {for (int i2 = 0;i2 < 20;++i2)
       {std::cout << Src[i][i2];}        
               std::cout << std::endl;}}

void Testdynamic_2d_array()
{    dynamic_2d_array < char > My_dynamic_2d_array(3, 20);        
MYARRAY_T TestData = {"hello world", "bla bla","more bla"};    
int i;    
for (i = 0;i < 3;++i)    
{for (int i2 = 0;i2 < 20;++i2)        
{My_dynamic_2d_array[i][i2] = TestData[i][i2];}}        
for (i = 0;i < 3;++i)    
{for (int i2 = 0;i2 < 20;++i2)
{std::cout << My_dynamic_2d_array[i][i2];}        
std::cout << std::endl;}        
SomefFunction(TestData);    
SomefFunction(*((MYARRAY_T*)&My_dynamic_2d_array[0][0]));    
char *data1 = TestData[1];    
const char *data2 = My_dynamic_2d_array[1];        
std::cout << data1 << std::endl;    
std::cout << data2 << std::endl;}

int main()
{
void SomefFunction(char, char);
void Testdynamic_2d_array();


typedef char MYARRAY_T[3][20];

SomefFunction(MYARRAY_T);
Testdynamic_2d_array();

return 0;
}

Thanks and Rrgards
C++ Beginner
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Commented:
void SomefFunction(char, char);//This does not match the function
void Testdynamic_2d_array();//This is a forward declartion, and if required, it needs to be declared outside of the function.

typedef char MYARRAY_T[3][20];// This needs to be in the begging of your code, so that it can be used by other functions.

Author

Commented:
Hi Axter,

I managed to get the template up and working. Thanks a lot for your guidance and very useful code.

Well, I do have a few concept questions about this template:-

1) With this template, I can create as many dynamic arrays as I want, right ?

2) With reference to this line:-
     typedef char MYARRAY_T[3][20];
     How come the array is defined in the first place ? Do I need to define an array 1st before NEWing another array ?
     Where does the dynamic part comes in when all the if values are using the 3 and 20, which is the dimension of the array declared ?

3) How can I use it for Double data type ?

I am sorry if these questions sounds trvial, but i really want to know more about this template.

Please help.

Thanks and regards
C++ Beginner

Commented:
>>With this template, I can create as many dynamic arrays as I want, right ?
Yes

>>typedef char MYARRAY_T[3][20];

This is not needed.  It's part of the example program.  The example program used it to show how the dynamic_2d_array class can interact with a function that takes a C-Style argument.


>>Where does the dynamic part comes in when all the if values are using the 3 and 20, which is the dimension of the array declared ?
MYARRAY_T is a static array type, and it's not part of the dynamic_2d_array class.

>>3) How can I use it for Double data type ?
Use double inside the < > brackets.
dynamic_2d_array < double > My_dynamic_2d_array(3, 20);  

The dynamic_2d_array class is a dynamic array in that it's size does not need to be defined at compile time.  
The size can be determined at runtime.  However, you must still initialize the array with a specific size.

Example:
int main(int argc, char* argv[])
{
      int x,y;
      cout << "Enter the X dimension: ";
      cin >> x;
      cout << "Enter the Y dimension: ";
      cin >> y;

      dynamic_2d_array<int> xx(x,y);
      
      system("pause");
      return 0;
}



Author

Commented:
Wow, there's a quick reply and very informative too.

>>The dynamic_2d_array class is a dynamic array in that it's size does not need to be defined at compile time.  
>>The size can be determined at runtime.  However, you must still initialize the array with a specific size.

Example:
int main(int argc, char* argv[])
{
    int x,y;
    cout << "Enter the X dimension: ";
    cin >> x;
    cout << "Enter the Y dimension: ";
    cin >> y;

    dynamic_2d_array<int> xx(x,y);
   
     system("pause");
    return 0;
}

I don't really understand this part, like I need to create the array before hand, that is create any array with a minimum size before running the program is it ?

Commented:
>>I don't really understand this part, like I need to create the array before hand, that is create any array with a minimum size before running the program is it ?

Before initializing the array, you need to determine it's size.  You can not initialize the dynamic_2d_array class without giving it the size arguments.

Author

Commented:
Hi Axter,

My problem now is I try to import a data file into the array.
I don't know the size of the file except for it's a 2D array file, or at most I 'll know only 1 dimension.

So can I still use this template ?

Or do you have a better suggestion ?

Thanks
C++ Beginner

Commented:
>>My problem now is I try to import a data file into the array.
At what point do you know the dimension?

If you have to determine the dimension of the array after it has been initialize, then you're better of using a vector object.

Example:

std::vector< std::vector< double > > MyDoubleVect_2D_Array;

Which dimension are you going to know when you're object is created?

my_data_of_doubles[FIRST_DIM][SECOND_DIM]  ?????

In order for the logic to work right in any 2D-Array object, the second dimension must be the same for all the objects in the array.
So the second dimension must be predetermined before filling the objects in your array.
If you're using a vector object, you can determine the first-dimension after initializing and after starting to fill the array.
As you determine that the array needs to increase insize, you just use the push_back function.


Author

Commented:
Hi there,

I will know SECOND_DIM.

So does that mean I can use your template?

I am kind of lost for vector, care to share more ?

Points increased to 90.

Thanks
C++ BEGINNER

Commented:
>>So does that mean I can use your template?
No.  In order to use my template, you need to know both dimensions when the object is initialized.

>>I am kind of lost for vector, care to share more ?
With the vector method, you can determine the first_dimension as you go along.

Commented:
Here's an example that uses the vector method.

#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;


int main(int argc, char* argv[])
{
      vector< vector< double > > MyDoubleVect_2D_Array;

      int SizeOfSecondDim;
      cout << "Enter Second Dimension: ";
      cin >> SizeOfSecondDim;

      double A_double = 99;
      while(A_double != 0)
      {
            //Add to the first dimension
            MyDoubleVect_2D_Array.push_back(vector< double >(SizeOfSecondDim));
            int NewlyAddedDim = MyDoubleVect_2D_Array.size() -1;
            //Fill in the second dimesion that is part of the newly added dimension.
            for (int i = 0;i < SizeOfSecondDim;++i)
            {
                  cout << "Enter item " << i << ": ";
                  cin >> A_double;
                  MyDoubleVect_2D_Array[NewlyAddedDim][i] = A_double;
            }
            cout << "Enter 0 to quit, or any other number to continue: ";
            cin >> A_double;
      }

      for (int FirstIndex = 0;FirstIndex < MyDoubleVect_2D_Array.size();++FirstIndex)
      {
            cout << endl;
            for(int SecondIndex = 0;SecondIndex < SizeOfSecondDim;++SecondIndex)
            {
                  cout << MyDoubleVect_2D_Array[FirstIndex][SecondIndex] << ", ";
            }
      }
      cout << endl;
      
      system("pause");
      return 0;
}

Author

Commented:
Hi Axter,

THe code works perfectly !!!! and I managed to incoperate it with my file operations.
Thanks a lot.

Just 2 questions to sum up the day:-

>> int main(int argc, char* argv[])

what argc and argv ?

Can I declare more than 1 dynamic array within the int main(...) ?

Lots of thanks,
C++ Beginner
Commented:
>>what argc and argv ?

argc is the number of arguments that was pass to your program from the command line, and argv is an array containing these arguments.

myprogram.exe /arg1 /arg2 /arg3

>>Can I declare more than 1 dynamic array within the int main(...) ?

You can delcare as many as you need.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial