September 10th, 2012, 07:23 AM

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 pseudorandom number between +1 and 1
y=(((rand()/(double)RAND_MAX)*2)1);//generates a pseudorandom number between +1 and 1
do
{
z= ((x*x)+(y*y));//squares the two pseudorandom numbers and adds the squares together i++; }
while(z>1);
printf("%lf\n",z);//prints the result z to screen }
return 0; }
September 10th, 2012, 09:17 AM

Look at you condition for z, you have z>1 but it should be z<1.
Part 1 (creating number) is before the loop, should be included.
September 10th, 2012, 09:27 AM

The first thing you should do is compile it. If it doesn't compile, then you have to critically examine the syntax of each statement and ask yourself (1) what is the proper syntax and (2) does this line have every element of proper syntax ?
Two easy checks are (1) Count the number of openbraces "{" and closebraces "}" in your program. They must be the same.
(2) Do the same thing for openparen's "(" and closeparen's ")".
September 12th, 2012, 03:13 AM

Originally Posted by EEmaestro
The first thing you should do is compile it. If it doesn't compile, then you have to critically examine the syntax of each statement and ask yourself (1) what is the proper syntax and (2) does this line have every element of proper syntax ?
Two easy checks are (1) Count the number of openbraces "{" and closebraces "}" in your program. They must be the same.
(2) Do the same thing for openparen's "(" and closeparen's ")".
Thanks guys! Fixed it!