8 Replies - 1231 Views - Last Post: 17 December 2011 - 10:29 PM Rate Topic: -----

#1 renu.rawat11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 17-December 11

invalid conversion from `void*' to `double**'

Posted 17 December 2011 - 02:45 AM

I have a revised simplex method code and it show some errors in the conversion of 'void*' to 'double**'


#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>

int bMATRIX[10][10];
void DETINV(int mSIZE, double **MATB);
void arg(int *a,int *b,int *n,int x,int y);
int det(int *p,int *n);
double **MATa;
double **MATB;
double *MATb;
double *MATc;
double *MATcBT;

int main()
{
  int nSIZE = 1;
  int mSIZE = 1;
  int n1SIZE = 1;

  int i, j;

      printf("REVERSE SIMPLEX METHOD \n\nEnter size [n] : ");
      scanf("%d", &nSIZE);
	  printf("Enter size [m] : ");
      scanf("%d", &mSIZE);

	  n1SIZE = nSIZE-mSIZE;
	  while ( n1SIZE <= 0 )
	  {
		printf("\n!!INVALID MATRIX SIZE!!\nPlease reenter size [n] : ");
		scanf("%d", &nSIZE);
		printf("Please reenter size [m] : ");
		scanf("%d", &mSIZE);
		n1SIZE = nSIZE-mSIZE;
	  }

////////////////////////////////////////////
     MATa = malloc(mSIZE*sizeof*MATa);


     
     
      for(i=0;i<mSIZE;++i)
	  {
		  MATa[i]=malloc(nSIZE*sizeof**MATa);
	  }
////////////////////////////////////////////
	  MATB = malloc(mSIZE*sizeof*MATB);

      for(i=0;i<mSIZE;++i)
	  {
		  MATB[i]=malloc(mSIZE*sizeof**MATB);
	  }

////////////////////////////////////////////	
	  MATc = malloc(nSIZE*sizeof*MATc);
////////////////////////////////////////////	
	  MATb = malloc(mSIZE*sizeof*MATb);
////////////////////////////////////////////

////////////////////////////////////////////
      printf("Please enter values of\n");
	  for(i=0;i<mSIZE;i++)
	  {
		for(j=0;j<nSIZE;j++)
		  {	
			printf("a%d,%d: ", i+1, j+1);
			scanf( "%lf", &MATa[i][j]);
		  }
	  }

      printf("Please enter values of\n");
	  for(i=0;i<mSIZE;i++)
	  {
		printf("b%d,1: ", i+1);
		scanf( "%lf", &MATb[i]);
	  }
	  printf("Please enter the coefficients of x in Zequation\n");
	  for(i=0;i<nSIZE;i++)
	  {
		printf("x%d: ", i+1);
		scanf( "%lf", &MATc[i]);
	  }
///////////////////////////////////////
	  printf("This is called matrix 'A'=\n");
	  for(i=0;i<mSIZE;i++)
		{
			for(j=0;j<nSIZE;j++)
				{
					printf("%1.0lf  ", MATa[i][j]);
				}
			printf("\n");
		}
	  printf("\n");
	  printf("This is called matrix 'b'=\n");
	  for(i=0;i<mSIZE;i++)
		{
			printf("%1.0lf", MATb[i]);
			printf("\n");
		}
	  	  printf("This is called matrix 'c'=\n");
	  for(i=0;i<nSIZE;i++)
		{
			printf("%1.0lf  ", MATc[i]);
			
		}
////////////////////////////////////////

////////////////////////////////////////
	  printf("\nThis gives matrix 'B':\n");
	  for(i=0;i<mSIZE;i++)
	  {
		for(j=n1SIZE;j<nSIZE;j++)
		{
			MATB[i][j] = MATa[i][j];
			printf("%1.0lf  ", MATB[i][j]);
		}
	    printf("\n");
	  }
	  printf("\n");	
//////////////////////////////////////////	  
	  printf("\nThis gives matrix 'K':\n");
	  for(i=0;i<mSIZE;i++)
	  {
		for(j=0;j<n1SIZE;j++)
		{
			printf("%1.0lf  ", MATa[i][j]);
		}
	    printf("\n");
	  }
	  printf("\n");
///////////////////////////////////////

///////////////////////////////////////

printf("\nNow to caculate matrix 'cB'/L=\n");
	  for(i=0;i<n1SIZE;i++)
		{
			printf("%1.0lf  ", MATc[i]);
			
		}
printf("\nTransposing matrix 'cB' gives 'cBT'=\n");
	  MATcBT = malloc(n1SIZE*sizeof*MATcBT);
	  for(i=0;i<n1SIZE;i++)
		{
			MATcBT[i] = MATc[i];
			printf("%1.0lf\n", MATcBT[i]);
		}

printf("\n\n\n!!ALL WORKING TO HERE!!\n\n\n");
DETINV(mSIZE, MATB);//this function call doesnt work :(/>


    //  for(i=0;i<mSIZE;++i) free(MATa[i]);
     // free(MATa);
    }
   
void DETINV(int mSIZE, double **MATB)
{
int a[10][10],b[10][10],c[10][10];
int n,n1,i,j,m,d,q;
n = mSIZE;
n1= mSIZE;
	  for(i=1;i<(mSIZE+1);i++)
	  {
		for(j=1;j<(mSIZE+1);j++)
		{
			q = (double)MATB[i][j];

		}
	    printf("\n");
	  }

	   printf("\setting up data for the functions\n");
	  for(i=1;i<(mSIZE+1);i++)
	  {
		for(j=1;j<(mSIZE+1);j++)
		{
			printf("[%d] [%d] ", i, j); 
			printf("%d ", a[i][j]);
			a[i][j] = q;

		}
	    printf("\n");
	  }

if(n==2)
{
c[0][0]=a[1][1];
c[1][1]=a[0][0];
c[0][1]=-a[0][1];
c[1][0]=-a[1][0];
d=a[0][0]*a[1][1]-a[0][1]*a[1][0];
printf("Determinant is:%d\n",d);
if(d==0)
{
getch();
exit(d-'0');
}

for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf(" %f",c[i][j]/(float)d);

}
}
else
{
m=n;
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
n=m;
arg(&a[0][0],&b[0][0],&n,i,j);
c[j][i]=pow(-1,(i+j))*det(&b[0][0],&n);
}
}
n=m;
d=det(&a[0][0],&n);
printf("Determinant is :%d\n",d);
if(d==0)
{
printf("INVERSE DOES NOT EXIST");
getch();
exit(d-'0');
}
printf("Inverting matrix 'B' gives 'B-1'=\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<m;j++)
printf(" %f",c[i][j]/(float)d);
}
}	 getch();
}

void arg(int *a,int *b,int *n,int x,int y)
{
int k,l,i,j;
for(i=0,k=0;i<*n;i++,k++)
{
for(j=0,l=0;j<*n;j++,l++)
{
if(i==x)
i++;
if(j==y)
j++;
*(b+10*k+l)=*(a+10*i+j);

}
}
*n=*n-1;
}

int det(int *p,int *n)
{
int d[10][10],i,j,m,sum=0;
m=*n;
if(*n==2)
return(*p**(p+11)-*(p+1)**(p+10));
for(i=0,j=0;j<m;j++)
{
*n=m;
arg(p,&d[0][0],n,i,j);
sum=sum+*(p+10*i+j)*pow(-1,(i+j))*det(&d[0][0],n);
}

return(sum);
}


:code:

This post has been edited by jimblumberg: 17 December 2011 - 07:16 AM
Reason for edit:: Added code tags, Learn to use them.


Is This A Good Question/Topic? 0
  • +

Replies To: invalid conversion from `void*' to `double**'

#2 nuclearfroggy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 82
  • View blog
  • Posts: 167
  • Joined: 04-August 08

Re: invalid conversion from `void*' to `double**'

Posted 17 December 2011 - 03:25 AM

Please use code tags for code, and some consistent indentation wouldn't hurt. If you don't do this it's unlikely anyone's going to even bother reading it.
Was This Post Helpful? 0
  • +
  • -

#3 Hezekiah  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 208
  • View blog
  • Posts: 552
  • Joined: 12-July 09

Re: invalid conversion from `void*' to `double**'

Posted 17 December 2011 - 04:06 AM

The error is probably with your malloc() calls. Try casting the return value to the required type:
MATa = (double**) malloc(mSIZE*sizeof*MATa);

Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3120
  • View blog
  • Posts: 19,163
  • Joined: 14-September 07

Re: invalid conversion from `void*' to `double**'

Posted 17 December 2011 - 10:20 AM

It's sizeof(item) not sizeof*item.

The only reason you'd need to cast that if you were compiling with a C++ compiler, as vanilla C does not require the cast when allocating.
Was This Post Helpful? 0
  • +
  • -

#5 GWatt  Icon User is offline

  • member icon

Reputation: 278
  • View blog
  • Posts: 3,079
  • Joined: 01-December 05

Re: invalid conversion from `void*' to `double**'

Posted 17 December 2011 - 12:27 PM

Actually, sizeof only need the parens when given a type. If you use an expression, which is more desirable anyway, you don't need parens and he was correct in using the asterisk, even though it was unnecessary in this case.
I tested with gcc and it does complain about the cast if you use C++, but not if you stick with C.
Was This Post Helpful? 1
  • +
  • -

#6 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3120
  • View blog
  • Posts: 19,163
  • Joined: 14-September 07

Re: invalid conversion from `void*' to `double**'

Posted 17 December 2011 - 12:32 PM

I prefer seeing the type, that way I don't have to go lookup the variable listed.
Was This Post Helpful? 0
  • +
  • -

#7 GWatt  Icon User is offline

  • member icon

Reputation: 278
  • View blog
  • Posts: 3,079
  • Joined: 01-December 05

Re: invalid conversion from `void*' to `double**'

Posted 17 December 2011 - 02:34 PM

If you use the variable for sizeof, but for some reason decide to change the type you won't have to change the malloc call. Can't your IDE show the type?

This post has been edited by GWatt: 17 December 2011 - 02:34 PM

Was This Post Helpful? 0
  • +
  • -

#8 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3120
  • View blog
  • Posts: 19,163
  • Joined: 14-September 07

Re: invalid conversion from `void*' to `double**'

Posted 17 December 2011 - 03:07 PM

I don't see why not. Without knowing the item in question is a pointer, at a quick glance it looks like multiplication rather then a dereference. Which is what I thought it was originally since I rarely see it used in this manner.

//usually this
SomeObject* obj = malloc(sizeof(SomeObject));
//not this
SomeObject* obj = malloc(sizeof*obj);



Tis' semantics though and my misread derailed the thread, my apologies.
Was This Post Helpful? 0
  • +
  • -

#9 GWatt  Icon User is offline

  • member icon

Reputation: 278
  • View blog
  • Posts: 3,079
  • Joined: 01-December 05

Re: invalid conversion from `void*' to `double**'

Posted 17 December 2011 - 10:29 PM

Yeah, that's why I use spaces to separate out parts of an expression and keep the deref asterisk right next to the variable.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1