Function call with dynamic arrays

Hi,
I am writing a program to solve a set of simultanious equations by Cramer's rule,using dynamic arrays but when I compile I get the following error:
invalid conversion from 'int' to 'int**' initializing argument 1 of ínt determ(int**,int)'.
I can find the error but my Dev C++ refuses to compile.
Here's the code, please have a look.

#include <iostream>
#include <cmath> // to include sqrt(), etc.
#include <cstdlib> // for atoi() and atof()
#include <unistd.h> // for getopt()
#include <time.h> // time_t, struct tm, difftime, time, mktime
#include <iostream> // for basic file IO operations
#include <fstream>


using namespace std;
typedef double real;
FILE *file;
int myPow(int x, int p);
int determ(int ** d,int n);
int main(int argc, char *argv[])
{

//read mat from file
int cols,rows;
int deta;
if((file=fopen("D:/1Equ/number.txt","r"))!=NULL)
{ fscanf(file,"%d", &rows);
fscanf(file,"%d", &cols);

int** a = new int*[rows];
for(int j=0;j<cols;++j)
a[j]=new int[cols];

cerr<<cols<<","<<rows<<endl;
for(int i=0;i<rows;i++)
{for(int j=0;j<cols;j++)
{if(!fscanf(file,"%d",&a[i][j]))
break;
cerr<<"Mat a["<<i<<"]["<<"[" <<j<<"]= "<< a[i][j]<<endl;
}
}
determ(** a, rows);
cerr << "Determinant= " << deta << endl;
for (int j=0; j < cols; j++)
delete [] a[j];
delete [] a;
}
else
{cerr <<"File Open failed"<<endl;}

system("PAUSE");
return EXIT_SUCCESS;
}

int determ(int ** d,int n)
{
int det=0, p, h, k, i, j;
int** temp = new int*[n];
for(int j=0;j<n;++j)
temp[j]=new int[n];
if(n==1) {
return d[0][0];
} else if(n==2) {
det=(d[0][0]*d[1][1]-d[0][1]*d[1][0]);
return det;
} else {


for(p=0;p<n;p++) {
h = 0;
k = 0;
for(i=1;i<n;i++) {
for( j=0;j<n;j++) {
if(j==p) {
continue;
}
temp[h][k] = d[i][j];
k++;
if(k==n-1) {
h++;
k = 0;
}
}
}
det=det+d[0][p]*myPow(-1,p)*determ(temp,n-1);
}
for (int j=0; j < n; j++)
delete [] temp[j];
delete [] temp;
return det;
}
}
int myPow (int x, int p) {
int i = 1;
for (int j = 1; j <= p; j++) i *= x;
return i;
}
Use the code tags, please.

You have:
1
2
3
4
5
6
7
8
9
int determ( int ** d, int n );

int main()
{
  int rows = 7;
  int** a = new int*[rows];

  determ( ** a, rows );
}

Pointer math:
1
2
3
int * p;

*p  == *(p + 0) == p[0]

In other words:
1
2
3
4
5
6
7
**a == a[0][0]

// and
determ( ** a, rows );
// is same as
int tmp = a[0][0];
determ( tmp, rows );

Sure, will use tags form now on.
I fai; to see logic in your answer.
I assume you need to create a pointer for every sub dimension:

1
2
3
int** a = new int*[rows];
for(int j=0;j<cols;++j)
a[j]=new int[cols];


but you say that(rows known) will work?:
 
 int** a = new int*[rows];
No. I just picked the mostly relevant lines from your code.

You had syntax error:
invalid conversion from 'int' to 'int**' initializing argument 1 of ínt determ(int**,int)'.

You had it because you essentially write:
1
2
int tmp = a[0][0];
determ( tmp, rows ); // error 

What should you call the determ() with?


However, since you did mention, your attempt to create 2D array has a flaw too:
1
2
3
4
5
int** a = new int * [rows];
for ( int j=0; j<cols; ++j )
{
  a[j] = new int [cols];
}

The array pointed to by a has rows elements, but you assign to cols elements. Your delete has matching error.

There is an another way, with more contiguous memory allocation:
1
2
3
4
5
6
7
8
9
10
11
int** a = new int * [rows];
a[0] = new int [rows*cols];
for ( int j=1; j<rows; ++j )
{
  a[j] = a[j-1] + cols;
}

// use a

delete [] a[0];
delete [] a;

Topic archived. No new replies allowed.