A
adinda
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(double *, 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.0009987};;
double *gradpointeroud,*gradpointernieuw;
double gradnieuw[nx*ny*nz*6];
double *Vnulpoint[nx*ny*nz];
double *Vnulpoint2[nx*ny*nz];
gradpointeroud=gradoud;
gradpointernieuw=gradnieuw;
int counter=1;
double *Varray;
Varray=calloc(1*(nx*ny*nz*6),sizeof(double));
while(counter<100)
{
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(countergetal,sizeof(double));
Vnulpoint2[i+j*nx
+s*nx*ny]=calloc(countergetal,sizeof(double));
for(kl=0;kl<countergetal;kl++)
{
*(Vnulpoint[i+j*nx+s*nx*ny]+kl)=0.12369*kl;
*(Vnulpoint[i+j*nx+s*nx*ny]+0)=1;
printf("%f\n ",*(Vnulpoint[i+j*nx+s*nx*ny]
+kl));
}
int maxindex;
double Vmax;
max_array(Vnulpoint[i+j*nx+s*nx*ny],
4,&maxindex,&Vmax);
printf("Het maximum volume is %f op locatie %d
\n",Vmax,maxindex);
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",*(Vnulpoint2[i+j*nx+s*nx*ny]),*(Vnulpoint2[i+j*nx+s*nx*ny]
+1),*(Vnulpoint2[i+j*nx+s*nx*ny]+2));
int teller;
double Vsort[countergetal];
for(teller=0;teller<countergetal;teller++)
{
Vsort[teller]=*(Vnulpoint[i+j*nx+s*nx*ny]+teller);
}
for(kl=0;kl<countergetal;kl++)
{
printf("Vsort %d \t %f\n ",kl,Vsort[kl]);
}
qsort(Vsort,countergetal,sizeof(double),(int(*)(const void*,const
void*))compare_doubles);
for(kl=0;kl<countergetal;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(Varray,counter*(nx*ny*nz*6)*sizeof(double));
int tel;
for (tel=0;tel<6;tel++)
{
Varray[(counter-1)*(i+j*nx+s*nx*ny+tel*nx*ny*nz)]=*(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);
gradpointernieuw=gradpointeroud;
}
}
}
counter++;
}
}
void max_array(double *a, int num_elements,int *indexptmax,double
*maxpt)
{
int i;
*indexptmax=0;
*maxpt=a[0];
for (i=1; i<num_elements; i++)
{
if (a>*maxpt)
{
*maxpt=a;
*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,int *index)
{ int nk,nn;
for(nk=0;nk<counternummer;nk++)
{ if ( *(V+nk)!=0)
{for (nn=0;nn<counternummer;nn++)
{
if (*(Vl+nn-1)==*(V+nk))
{
*index=nn;
}
}
break;
}
}
}
void vindmaxnieteen(double *V,double *Vl,int counternummer,int*index)
{ int nk,nn;
for(nk=0;nk<counternummer;nk++)
{ if ( *(V+(counternummer-1)-nk)!=1)
{for (nn=0;nn<counternummer;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(double *, 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.0009987};;
double *gradpointeroud,*gradpointernieuw;
double gradnieuw[nx*ny*nz*6];
double *Vnulpoint[nx*ny*nz];
double *Vnulpoint2[nx*ny*nz];
gradpointeroud=gradoud;
gradpointernieuw=gradnieuw;
int counter=1;
double *Varray;
Varray=calloc(1*(nx*ny*nz*6),sizeof(double));
while(counter<100)
{
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(countergetal,sizeof(double));
Vnulpoint2[i+j*nx
+s*nx*ny]=calloc(countergetal,sizeof(double));
for(kl=0;kl<countergetal;kl++)
{
*(Vnulpoint[i+j*nx+s*nx*ny]+kl)=0.12369*kl;
*(Vnulpoint[i+j*nx+s*nx*ny]+0)=1;
printf("%f\n ",*(Vnulpoint[i+j*nx+s*nx*ny]
+kl));
}
int maxindex;
double Vmax;
max_array(Vnulpoint[i+j*nx+s*nx*ny],
4,&maxindex,&Vmax);
printf("Het maximum volume is %f op locatie %d
\n",Vmax,maxindex);
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",*(Vnulpoint2[i+j*nx+s*nx*ny]),*(Vnulpoint2[i+j*nx+s*nx*ny]
+1),*(Vnulpoint2[i+j*nx+s*nx*ny]+2));
int teller;
double Vsort[countergetal];
for(teller=0;teller<countergetal;teller++)
{
Vsort[teller]=*(Vnulpoint[i+j*nx+s*nx*ny]+teller);
}
for(kl=0;kl<countergetal;kl++)
{
printf("Vsort %d \t %f\n ",kl,Vsort[kl]);
}
qsort(Vsort,countergetal,sizeof(double),(int(*)(const void*,const
void*))compare_doubles);
for(kl=0;kl<countergetal;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(Varray,counter*(nx*ny*nz*6)*sizeof(double));
int tel;
for (tel=0;tel<6;tel++)
{
Varray[(counter-1)*(i+j*nx+s*nx*ny+tel*nx*ny*nz)]=*(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);
gradpointernieuw=gradpointeroud;
}
}
}
counter++;
}
}
void max_array(double *a, int num_elements,int *indexptmax,double
*maxpt)
{
int i;
*indexptmax=0;
*maxpt=a[0];
for (i=1; i<num_elements; i++)
{
if (a>*maxpt)
{
*maxpt=a;
*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,int *index)
{ int nk,nn;
for(nk=0;nk<counternummer;nk++)
{ if ( *(V+nk)!=0)
{for (nn=0;nn<counternummer;nn++)
{
if (*(Vl+nn-1)==*(V+nk))
{
*index=nn;
}
}
break;
}
}
}
void vindmaxnieteen(double *V,double *Vl,int counternummer,int*index)
{ int nk,nn;
for(nk=0;nk<counternummer;nk++)
{ if ( *(V+(counternummer-1)-nk)!=1)
{for (nn=0;nn<counternummer;nn++)
{
if (*(Vl+nn)==*(V+(counternummer-1)-nk))
{
*index=nn;
}
}
break;
}
}
return;
}