Two Dimensional Array

Hi
Can any one help me how to pass a two dimensional array.I have written the program.

Thanks

----------------------

#include <iostream>
#include <string>

int read();

void createMatrix(int);

int drawTree(int, double[][]);

int main()
{
read();

return 0;
}

int read()
{
int input = 0; // no of sequences

//int seqLen = 0; // length of sequence

//int seqSame = 0;

int totalClusters = 0;

cout << "Enter The Number Of Sequences : ";

// keeps on going till an integer is entered
while(!(cin >> input))
{
string buffer;
cin.clear(); //clear failbit
cin >> buffer; //flush stream
cout << "Invalid Input.. Please Enter An Interger Only !!!" << endl;
cout << "Enter The Number Of Sequence : ";

}

// total clusters
totalClusters = input ;

// initialise matrix
createMatrix(totalClusters);

return 0;

}

void createMatrix(int totalClusters)
{
double matrix [totalClusters][totalClusters];

// initialise matrix with 0
for (int i = 0; i < totalClusters; i++)
{
for (int j = 0; j < totalClusters; j++)
{
matrix[i][j] = 0;
}// inner 2nd

} //outer most

// display matrix
for (int m = 0; m < totalClusters ; m++)
{
for (int n = 0; n < totalClusters ; n++)
{
cout << " " << matrix[m][n];
}
//next line
cout << "\n";
}

// pass by reference matrix
drawTree(totalClusters,matrix);
}

int drawTree(int totalClusters, double newMatrix[][])
{
cout << "Inside the proc " << endl;
// display matrix
for (int i = 0; i < totalClusters ; i++)
{
for (int j = 0; j < totalClusters ; j++)
{
cout << " " << newMatrix[i][j];
}
//next line
cout << "\n";
}

}

----------
[2194 byte] By [Sarika73] at [2007-11-18 3:47:35]
# 1 Re: Two Dimensional Array
I think that you have more than a problem with this code.

The first thing is that you cannot establish dynamically the dimensions of "matrix" in createMatrix this way.

However, can you be a little more specific in the problem you are trying to solve?

You compile your code, and what do you obtain?
Caronte at 2007-11-8 8:50:13 >
# 2 Re: Two Dimensional Array
Yes you right.
I want to know how to establish the dimensions of a matrix at runtime.

Pinto
Sarika73 at 2007-11-8 8:51:12 >
# 3 Re: Two Dimensional Array
Well. The question is that when you do double matrix[sizea][sizeb]; you are making a static allocation of memory in the stack...

In order to do dynamic allocations, you have to use heap memory. How? Using malloc or new (remember that the you have to free the memory with free or delete...)

Are you familiar with this?
Caronte at 2007-11-8 8:52:17 >
# 4 Re: Two Dimensional Array
Not Really.
If you don't mind Can you pls update my program code.

Thanking you in anticipation.

Thanks
Sarika73 at 2007-11-8 8:53:20 >
# 5 Re: Two Dimensional Array
Since you're using C++, you really ought to use std::vector
instead of dynamically-allocated arrays. Look up std::vector's
documentation. Here's one link [of many]:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vciostrm/html/_iostream_alphabetic_microsoft_iostream_class_library_reference.asp

--Paul
PaulWendt at 2007-11-8 8:54:20 >
# 6 Re: Two Dimensional Array
The FAQ has the start of a 2D dyanamic array class at :

http://www.dev-archive.com/forum/showthread.php?s=&threadid=231046

I took that class, added a size() member variable and wrote a
simple test program which shows how to pass the array to a
function. You will notice there is no need for new/delete
using std::vector.

#include <iostream>

#include <vector>

template <typename T>
class dynamic_array
{
public:
dynamic_array(){};
dynamic_array(int rows, int cols)
{
for(int i=0; i<rows; ++i){
data_.push_back(std::vector<T>(cols));
}
}
// other ctors ...
std::vector<T> & operator[](int i)
{
return data_[i];
}
const std::vector<T> & operator[] (int i) const
{
return data_[i];
}
// other accessors, like at() ...

// other member functions, like reserve()...

const std::vector<T>::size_type size() const
{
return data_.size();
}

private:
std::vector<std::vector<T> > data_;

};

void init(dynamic_array<int>& arr)
{
for (int i=0; i<arr.size(); ++i)
{
for (int j=0; j<arr[i].size(); ++j)
{
arr[i][j] = i + j;
}
}
}

int main()
{
dynamic_array<int> a(30, 20);
init(a);
std::cout << a[10][12] << std::endl;
return 0;
}
Philip Nicoletti at 2007-11-8 8:55:16 >