Hi guys,

I am trying to pass a two dimensional array into a constructor. I am having hard time with it. Can anyone please tell me how to pass arrays into a parameter. the code is something like this :

testCase::testCase(int i, int *matrix[][],float m)

Your help is appreciated

Khalid

You can either pass the array considering as a sparse array i.e. by doing void someFunc(int** myArray) or you can pass it as a normal array by leaving off at the max the first dimension i.e. void someFunc(int myArray[][COLS]) . Also read this.

Consider constructing your 2D array using vectors since it is more flexible that way.

I have a parametrised constructor :

testCase::testCase(int i,int *matrix2[][2],float mFit)
		{
			this->caseId=i;
			this->matrix[3][2]=*matrix2[3][2];
			this->fitn=mFit;
		

		}

and I need to call it in the main.

void main()
{
testCase *NewCase[1000];
 for (int i=0;i<10;i++){
		  NewCase[i]= new testCase(i,Test.population(), Test.fitness());
}
}

I get the error :

cannot convert parameter 2 from 'int *' to 'int *[][2]'

Function population returns :

return &matrix[0][0];

I just don't know what am I doing wrong. Please see if you know what is wrong with this code. Your help is appreciated.

may we see how you structured your Test class?

This is the code for class testCase of which Test is an instance :

const int cellId=3;
const int freq=2;
int neighbourcells[3][3]={{0,1,0},{1,0,1},{0,1,0}};
 	static int totalCalls=0;
class testCase 
{

	public:
		int caseId;
		int matrix[cellId][freq];

		float fitn;
		

		testCase()
		{
		}
	
    testCase::testCase(int i,int *matrix2[][2],float mFit)
		{
		this->caseId=i;
		this->matrix[cellId][freq]=*matrix2[][2];
		this->fitn=mFit;
		

		}
	

		
	int *testCase::population()
		{
		srand ( time(NULL) );

		for(int i=0;i<cellId;i++)
		{
				
		for(int j=0;j<freq;j++){
		matrix[i][j]=rand()%2;
					//cout<<"==="<<matrix[i][j]<<" ";
		cout<<matrix[i][j]<<" ";
		if(matrix[i][j]==1){

						totalCalls++;
						
					}
			

				}
				
			}
		

			
	
cout<<"The total number of calls in the Network is "<<totalCalls<<endl;

			return &matrix[0][0];
		}

	
		float testCase::fitness()
		{

			static int noFit=0;
			
		
			
			for(int i=0;i<cellId;i++)
			{
			for(int k=0;k<freq;k++)
			{
	
		if ((matrix[i][k]==1)&&(matrix[i+1][k]==1)){
		
				
			if(neighbourcells[i][i+1]==1)
				{

		//	cout<<"Not fit"<<endl;
			noFit++;
		cout<<"Numbers not fit="<<noFit<<endl;
				}
				else
				{
				
					
				}

			}

			else {

				
			}
			}
			}
	float fitness=100-(noFit*100/totalCalls);
	cout<<"My Fitness is ="<<fitness<<"%"<<endl;

		return fitness;

		}
		

};

Why do you want to return a 2D array which is a member of the class when all you are doing in the 'population()' method is make changes to the same array? You can just call the 'population()' function in your constructor and your 2D array would have the updated/required values.

Your class design looks weird, but I will leave that up to you to figure out.

class testCase 
{
  public:
    int caseId;
    int matrix[cellId][freq];
	
    testCase::testCase(int i, int *matrix2[][2], float mFit)
    {
      this->caseId=i;
      this->matrix[cellId][freq]=*matrix2[][2];
      this->fitn=mFit;

I see no reason to be using the scope operator in the above snippet
since the definition you are coding is occuring within the
declaration.

Using the this pointer is unnecessary in the above snippet.

In the following line:

matrix[cellId][freq]=*matrix2[][2];

the indexes cellId and freq are out of bounds and hopefully cause
your program to crash. In addition, naming specific indexes in this
context refers to a specific element of matrix, not to matrix itself.

It is incorrect syntax to use an empty [] operator in the context of
assignment as in the above line in reference to matrix2.

In C/C++ you can't assign one array to another. I also prefer not to try to assign a pointer to an array to another pointer to an array.

To assign the values of an array passed to this constructor I would
use loops to assign each element separately from one array to the
other.

In summary, I'd try this:

class testCase 
{
  public:
    int caseId;
    int matrix[cellId][freq];
	
    testCase(int i, int matrix2[cellId][freq], float mFit)
    {
      caseId=i;
      for(int j = 0; j < cellId; ++j)
        for(int k = 0; k < freq; ++k)
           matrix[j][k] = matrix2[j][k];
This question has already been answered. Start a new discussion instead.