How to initialize a variable with a line from a bidimensional array in C? -
can me problem: have line i, unidimensional array p , bidimensional array(matrix) q. so, problem must initialize unidimensional array p line q without using for-loop. how can that?
void simvardiscr(int m, int *x, double *p){//m number of elements of x , p int i; for(i=0;i<m;i++){ x[i]=i; init_genrand(254);//is function mersenne-twister algorithm p[i]=genrand_int32()%m; } } int lantmarkov(int m,double *pi0,float **q){ int *s,k,j;double *p; s=(int *)malloc(n*sizeof(int)); p=(double *)calloc(m,sizeof(double)); simvardiscr(m,s,pi0); j=s[0]; for(k=0;k<n;k++){ memcpy(p, q[j], m); simvardiscr(m,s,p); j=s[k]; }
the answer depends on interpretation of question: "initialize unidimensional array p line q without using for-loop" - because code doesn't initialize p
line q
using for
loop.
a
for
loop can replacedwhile
loop or recursion.if change type of either
p
orq
compatible (e.g. bothfloat
), can usememcpy
copyq
p
; not sure helps because havememcpy
(which has 2 bugs, doesn't work) in code.you can remove memory allocation
p
, , have point row inq
; doesn't require loop. example:
-
void simvardiscr(int m, int *x, float *p) // changed double float { ... // no change in code } int lantmarkov(int m,double *pi0,float **q) { ... for(k=0;k<n;k++){ p = q[j]; // following code changes p , j'th row of q in same manner ... } }
Comments
Post a Comment