Hi, I just started working with C and was working on an exercise where I needed to simulate a random 'walk' on a Cartesian grid. It is supposed to start on the origin, add a number between -5 - 5 one thousand times, and then calculate distance from the origin to the new value. I've been able to do everything, but I can't see how I'm supposed to get it to repeat the addition. If someone could point me in the right direction I would be very grateful.
Here's my current code for reference:
Also just in case it is important, I'm using xcode. Thank you in advance and thanks for having this kind of community; I wouldn't have got this far with this program without being able to use this board as a reference.Code:#include <stdio.h> #include <stdlib.h> #include <math.h> int main () { float x, p, y, yn, xn, product, distance; x= 0; y= 0; p= 5 - rand()%10; xn= x+p; yn= y+p; product= (xn*xn) + (yn*yn); distance= sqrt(product); printf("Change in x: %f\n",xn); printf("Change in y: %f\n",yn); printf("Distance from origin:%f\n",distance); return 0; }



LinkBack URL
About LinkBacks


