
September 10th, 2012, 07:23 AM
|
|
Registered User
|
|
Join Date: Aug 2012
Location: Bristol
Posts: 13
Time spent in forums: 2 h 14 m 32 sec
Reputation Power: 0
|
|
|
Do while loops
Here is the start of the task put to me:
1: Create two random numbers x and y which are between +1 and -1. To do this just double the
random number (which is between 0 and 1) and subtract 1.
2: calculate: z=x^2+y^2
3: Repeat 1 and 2 until z<1.0 using, for example, a while(){} loop.
This is what I've come up with, it runs no problem but doesn't print a result to screen. (At least not in my attention span.) Am I doing something wrong here?
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
double x,y,
z; i=0;
{ x=(((rand()/(double)RAND_MAX)*2)-1);//generates a pseudo-random number between +1 and -1
y=(((rand()/(double)RAND_MAX)*2)-1);//generates a pseudo-random number between +1 and -1
do
{
z= ((x*x)+(y*y));//squares the two pseudo-random numbers and adds the squares together i++; }
while(z>1);
printf("%lf\n",z);//prints the result z to screen }
return 0; }
|