So what i need is this; (I'm very new at this,, programming in C I
mean...)
In matlab I had a while loop, and after each loop was done I added my
resulting matrix to an object. Seeing the loop is conditional to my
results I do not know in advance how many loops I need. The matrices
are Nx * Ny* Nz*6.
In C I put my matrices into 1D arrays. So I thought that if I use a
counter for each time i do a loop and the calloc and realloc function
that would work...But it doesnt seem to be working...Hope one of you
can help me.
This is a sample text which i use to test my programs...
Vnulpoint is my result matrix for which i used pointers. So Varray is
supposed to contain each of the Vnulpoint matrices...
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define nx 2
#define ny 2
#define nz 2
void max_array(doubl e *, int,int *,double *);
int compare_doubles (double *, double *);
void vindminnietnul( double *,double *,int,int* );
void vindmaxnieteen( double *,double *,int ,int*);
main()
{ int i,j,s,kl;
int countergetal=6;
double
gradoud[nx*ny*nz*6]={5.569,6.9987, 0.00987,98.7743 ,1009.8876,0.00 09987};;
double *gradpointeroud ,*gradpointerni euw;
double gradnieuw[nx*ny*nz*6];
double *Vnulpoint[nx*ny*nz];
double *Vnulpoint2[nx*ny*nz];
gradpointeroud= gradoud;
gradpointernieu w=gradnieuw;
int counter=1;
double *Varray;
Varray=calloc(1 *(nx*ny*nz*6),s izeof(double));
while(counter<1 00)
{
for(i=0; i<nx; i++)
{ for(j=0; j<ny ; j++)
{ for(s=0; s<nz ; s++)
{
Vnulpoint[i+j*nx
+s*nx*ny]=calloc(counter getal,sizeof(do uble));
Vnulpoint2[i+j*nx
+s*nx*ny]=calloc(counter getal,sizeof(do uble));
for(kl=0;kl<cou ntergetal;kl++)
{
*(Vnulpoint[i+j*nx+s*nx*ny]+kl)=0.12369*kl ;
*(Vnulpoint[i+j*nx+s*nx*ny]+0)=1;
printf("%f\n ",*(Vnulpoi nt[i+j*nx+s*nx*ny]
+kl));
}
int maxindex;
double Vmax;
max_array(Vnulp oint[i+j*nx+s*nx*ny],
4,&maxindex,&Vm ax);
printf("Het maximum volume is %f op locatie %d
\n",Vmax,maxind ex);
Vnulpoint2[i+j*nx+s*nx*ny]=Vnulpoint[i+j*nx
+s*nx*ny];
printf("Het toegewezen volume is %f \t %f \t %f
\n",*(Vnulpoint 2[i+j*nx+s*nx*ny]),*(Vnulpoint2[i+j*nx+s*nx*ny]
+1),*(Vnulpoint 2[i+j*nx+s*nx*ny]+2));
int teller;
double Vsort[countergetal];
for(teller=0;te ller<counterget al;teller++)
{
Vsort[teller]=*(Vnulpoint[i+j*nx+s*nx*ny]+teller);
}
for(kl=0;kl<cou ntergetal;kl++)
{
printf("Vsort %d \t %f\n ",kl,Vsort[kl]);
}
qsort(Vsort,cou ntergetal,sizeo f(double),(int( *)(const void*,const
void*))compare_ doubles);
for(kl=0;kl<cou ntergetal;kl++)
{
printf("Vsort %d \t %f\n ",kl,Vsort[kl]);
}
int indexmaxnieteen ;
vindmaxnieteen( Vsort,Vnulpoint[i+j*nx
+s*nx*ny],countergetal,& indexmaxnieteen );
printf("De index is %d\n", indexmaxnieteen );
Varray=(double
*)realloc(Varra y,counter*(nx*n y*nz*6)*sizeof( double));
int tel;
for (tel=0;tel<6;te l++)
{
Varray[(counter-1)*(i+j*nx+s*nx *ny+tel*nx*ny*n z)]=*(Vnulpoint[i
+j*nx+s*nx*ny]+tel*nx*ny*nz);
}
printf("Varray is %f \t %f \t %f \n %f \t %f \t %f \n en counter
is %d \n", Varray[(counter-1)*(i+j*nx+s*nx *ny)],Varray[(counter-1)*(i
+j*nx+s*nx*ny+1 )],Varray[(counter-1)*(i+j*nx+s*nx *ny
+2)],Varray[(counter-1)*(i+j*nx+s*nx *ny+3)],Varray[(counter-1)*(i+j*nx
+s*nx*ny+4)],Varray[(counter-1)*(i+j*nx+s*nx *ny
+5)],Varray[(counter-1)*(i+j*nx+s*nx *ny+6)],counter);
gradpointernieu w=gradpointerou d;
}
}
}
counter++;
}
}
void max_array(doubl e *a, int num_elements,in t *indexptmax,dou ble
*maxpt)
{
int i;
*indexptmax=0;
*maxpt=a[0];
for (i=1; i<num_elements ; i++)
{
if (a[i]>*maxpt)
{
*maxpt=a[i];
*indexptmax=i;
}
}
return;
}
int compare_doubles (double *x, double *y)
{
if (*x *y)
{
return 1;
}
else
{
if (*x < *y)
{
return -1;
}
else
{
return 0;
}
}
}
void vindminnietnul( double *V,double *Vl,int counternummer,i nt *index)
{ int nk,nn;
for(nk=0;nk<cou nternummer;nk++ )
{ if ( *(V+nk)!=0)
{for (nn=0;nn<counte rnummer;nn++)
{
if (*(Vl+nn-1)==*(V+nk))
{
*index=nn;
}
}
break;
}
}
}
void vindmaxnieteen( double *V,double *Vl,int counternummer,i nt*index)
{ int nk,nn;
for(nk=0;nk<cou nternummer;nk++ )
{ if ( *(V+(counternum mer-1)-nk)!=1)
{for (nn=0;nn<counte rnummer;nn++)
{
if (*(Vl+nn)==*(V+ (counternummer-1)-nk))
{
*index=nn;
}
}
break;
}
}
return;
}
mean...)
In matlab I had a while loop, and after each loop was done I added my
resulting matrix to an object. Seeing the loop is conditional to my
results I do not know in advance how many loops I need. The matrices
are Nx * Ny* Nz*6.
In C I put my matrices into 1D arrays. So I thought that if I use a
counter for each time i do a loop and the calloc and realloc function
that would work...But it doesnt seem to be working...Hope one of you
can help me.
This is a sample text which i use to test my programs...
Vnulpoint is my result matrix for which i used pointers. So Varray is
supposed to contain each of the Vnulpoint matrices...
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define nx 2
#define ny 2
#define nz 2
void max_array(doubl e *, int,int *,double *);
int compare_doubles (double *, double *);
void vindminnietnul( double *,double *,int,int* );
void vindmaxnieteen( double *,double *,int ,int*);
main()
{ int i,j,s,kl;
int countergetal=6;
double
gradoud[nx*ny*nz*6]={5.569,6.9987, 0.00987,98.7743 ,1009.8876,0.00 09987};;
double *gradpointeroud ,*gradpointerni euw;
double gradnieuw[nx*ny*nz*6];
double *Vnulpoint[nx*ny*nz];
double *Vnulpoint2[nx*ny*nz];
gradpointeroud= gradoud;
gradpointernieu w=gradnieuw;
int counter=1;
double *Varray;
Varray=calloc(1 *(nx*ny*nz*6),s izeof(double));
while(counter<1 00)
{
for(i=0; i<nx; i++)
{ for(j=0; j<ny ; j++)
{ for(s=0; s<nz ; s++)
{
Vnulpoint[i+j*nx
+s*nx*ny]=calloc(counter getal,sizeof(do uble));
Vnulpoint2[i+j*nx
+s*nx*ny]=calloc(counter getal,sizeof(do uble));
for(kl=0;kl<cou ntergetal;kl++)
{
*(Vnulpoint[i+j*nx+s*nx*ny]+kl)=0.12369*kl ;
*(Vnulpoint[i+j*nx+s*nx*ny]+0)=1;
printf("%f\n ",*(Vnulpoi nt[i+j*nx+s*nx*ny]
+kl));
}
int maxindex;
double Vmax;
max_array(Vnulp oint[i+j*nx+s*nx*ny],
4,&maxindex,&Vm ax);
printf("Het maximum volume is %f op locatie %d
\n",Vmax,maxind ex);
Vnulpoint2[i+j*nx+s*nx*ny]=Vnulpoint[i+j*nx
+s*nx*ny];
printf("Het toegewezen volume is %f \t %f \t %f
\n",*(Vnulpoint 2[i+j*nx+s*nx*ny]),*(Vnulpoint2[i+j*nx+s*nx*ny]
+1),*(Vnulpoint 2[i+j*nx+s*nx*ny]+2));
int teller;
double Vsort[countergetal];
for(teller=0;te ller<counterget al;teller++)
{
Vsort[teller]=*(Vnulpoint[i+j*nx+s*nx*ny]+teller);
}
for(kl=0;kl<cou ntergetal;kl++)
{
printf("Vsort %d \t %f\n ",kl,Vsort[kl]);
}
qsort(Vsort,cou ntergetal,sizeo f(double),(int( *)(const void*,const
void*))compare_ doubles);
for(kl=0;kl<cou ntergetal;kl++)
{
printf("Vsort %d \t %f\n ",kl,Vsort[kl]);
}
int indexmaxnieteen ;
vindmaxnieteen( Vsort,Vnulpoint[i+j*nx
+s*nx*ny],countergetal,& indexmaxnieteen );
printf("De index is %d\n", indexmaxnieteen );
Varray=(double
*)realloc(Varra y,counter*(nx*n y*nz*6)*sizeof( double));
int tel;
for (tel=0;tel<6;te l++)
{
Varray[(counter-1)*(i+j*nx+s*nx *ny+tel*nx*ny*n z)]=*(Vnulpoint[i
+j*nx+s*nx*ny]+tel*nx*ny*nz);
}
printf("Varray is %f \t %f \t %f \n %f \t %f \t %f \n en counter
is %d \n", Varray[(counter-1)*(i+j*nx+s*nx *ny)],Varray[(counter-1)*(i
+j*nx+s*nx*ny+1 )],Varray[(counter-1)*(i+j*nx+s*nx *ny
+2)],Varray[(counter-1)*(i+j*nx+s*nx *ny+3)],Varray[(counter-1)*(i+j*nx
+s*nx*ny+4)],Varray[(counter-1)*(i+j*nx+s*nx *ny
+5)],Varray[(counter-1)*(i+j*nx+s*nx *ny+6)],counter);
gradpointernieu w=gradpointerou d;
}
}
}
counter++;
}
}
void max_array(doubl e *a, int num_elements,in t *indexptmax,dou ble
*maxpt)
{
int i;
*indexptmax=0;
*maxpt=a[0];
for (i=1; i<num_elements ; i++)
{
if (a[i]>*maxpt)
{
*maxpt=a[i];
*indexptmax=i;
}
}
return;
}
int compare_doubles (double *x, double *y)
{
if (*x *y)
{
return 1;
}
else
{
if (*x < *y)
{
return -1;
}
else
{
return 0;
}
}
}
void vindminnietnul( double *V,double *Vl,int counternummer,i nt *index)
{ int nk,nn;
for(nk=0;nk<cou nternummer;nk++ )
{ if ( *(V+nk)!=0)
{for (nn=0;nn<counte rnummer;nn++)
{
if (*(Vl+nn-1)==*(V+nk))
{
*index=nn;
}
}
break;
}
}
}
void vindmaxnieteen( double *V,double *Vl,int counternummer,i nt*index)
{ int nk,nn;
for(nk=0;nk<cou nternummer;nk++ )
{ if ( *(V+(counternum mer-1)-nk)!=1)
{for (nn=0;nn<counte rnummer;nn++)
{
if (*(Vl+nn)==*(V+ (counternummer-1)-nk))
{
*index=nn;
}
}
break;
}
}
return;
}
Comment