Question:
Why do the values change when I switch to the main function (C)?
Jacques
2013-05-03 12:03:56 UTC
Yep, I did use pointers. But it still doesn't work, whatever the **** I do.
Sometimes, I wonder whether there's a flaw in the language itself.

The problem is : In the function "initTermite", when I ask to print the "dir" of the termite, it prints a random number between 0 and 7 (just what I wanted)
but then, when I ask to print it again in the main function, I always get "0" or sometimes, weird numbers like "-179375438"...
why can't it just stay the way it was before in the initTermite function ??
Thanks in advance, id be really grateful if someone had the answer.




#include
#include
#include


// #define pour constantes, int x; pour variables globales

typedef struct {
int x;
int y;
} coord;

typedef struct {
coord pos;
int dir;
int brindille;
int TournerSurPlace;
} termite;

typedef struct {
coord c;
int brindille;
termite *termite;
} place;

place terrain[40][40];




int nbTermites;

termite termites[1600];

int proba(float p)
{
int x=rand()%1001;
return((x}

void placeVide(place *p)
{
p->brindille=0;
p->termite=NULL;
}

void poserBrindille(place *p)
{
p->brindille=1;
p->termite=NULL;
}

termite initTermite(termite *t, int a, int b)
{
int k=rand()%8;
t->pos.x=a;
t->pos.y=b;
t->dir=rand()%8;
printf("%d\n\n\n", t->dir);
t->brindille=0;
t->TournerSurPlace=0;
}

void poserTermite(place *p, termite *t)
{
p->termite=t;
p->brindille=0;
}

void affichePlace (place *p)
{
if (p->brindille==1)
printf("0");
else
if (p->termite==NULL)
printf(" ");
else
{
if(p->termite->dir==0 || p->termite->dir==4)
printf("|");
else
if(p->termite->dir==1 || p->termite->dir==5)
printf("/");
else
if(p->termite->dir==2 || p->termite->dir==6)
printf("-");
else
if(p->termite->dir==3 || p->termite->dir==7)
printf("T");
}
}

void afficheTerrain()
{int i;
int j;
int m;
for (j=0; j<=40; j++)
for(i=0; i<=40; i++)
{
affichePlace(&terrain[i][j]);
}
printf("\n");
}



void initTerrain()
{
int i;
int j;
int k;
k=0;
termite t;
for (j=0; j<=40; j++)
for(i=0; i<=40; i++)
{
placeVide(&terrain[i][j]);
if (proba(0.05))
poserBrindille(&terrain[i][j]);
else
if (proba(0.01))
{ termites[k]=initTermite(&t, i, j);
poserTermite(&terrain[i][j], &termites[k]);
k=k+1;}
}
nbTermites=k+1;
}

int main()
{int m;
srand(time(NULL));
initTerrain();
afficheTerrain();
printf("\n%d\n", nbTermites);
for (m=0; m<=nbTermites; m++)
printf("%d\n\n\n", termites[m].dir );
}
Three answers:
Jeroonk
2013-05-03 12:47:32 UTC
A few remarks:



1) Using "<=" in your loops causes the index to go out-of-bounds.

For example, you have an "int terrain[40][40];" variable. In C, array indices start at zero, so the valid indices are [0][0] trough [39][39].



But in your loop:

for (j=0; j<=40; j++)



You iterate the variable "j" from 0 trough 40. Use "j < 40" instead of "j <= 40".



The same thing happens at:

for(m=0; m<=nbTermites; m++).



Use "m < nbTermites" instead.





2) The function:

termite initTermite(termite *t, int a, int b)



Should be:

void initTermite(termite *t, int a, int b)



It doesn't return anything, because it already operates on the termite passed by a pointer "termite *t". This is also the reason why you see weird numbers, because of this line (in the "initTerrain") function:

termites[k] = initTermite(&t, i, j);



That should be:

initTermite(&termites[k], i, j);



In the above line, the variable "termite t" is always being initialized correctly, but never stored inside the array, since "initTermite" does not return anything.





3) In "afficheTerrain" you iterate over i and j, calling "affichePlace" for each grid location. Assuming that you want to print an actual 40x40 grid on the screen, include the "printf("\n");" line inside the outer loop (add braces around it):

for(j = 0; j < 40; j++) {

for(i = 0; i < 40; i++) {

affichePlace(&terrain[i][j]);

}

printf("\n");

}



Corrected code:

http://pastebin.com/223VXSDe



P.S. wouldn't a backslash: "prinft("\\");", look better than "printf("T");" for termites facing north-west (dir == 7) or south-east (dir == 3)?
Chris
2013-05-03 19:16:04 UTC
"Sometimes, I wonder whether there's a flaw in the language itself."



Yes, unfortunately that's the problem. There's really nothing you can do. C++ is flawed.

Just kidding, you've made an error and thinking that makes you look stupid and arrogant.
Laurence I
2013-05-03 19:20:30 UTC
you have let yourself down



what did you want this to do? ------ return((x


1000 is an INT p is a FLOAT the < will be an INT comparison


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...