C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 10th, 2012, 07:23 AM
x.sophie.x x.sophie.x is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Location: Bristol
Posts: 13 x.sophie.x User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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; }

Reply With Quote
  #2  
Old September 10th, 2012, 09:17 AM
MrFujin's Avatar
MrFujin MrFujin is offline
Lord of the Dance
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Oct 2003
Posts: 3,130 MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 23 h 46 m 38 sec
Reputation Power: 1736
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.

Reply With Quote
  #3  
Old September 10th, 2012, 09:27 AM
EEmaestro EEmaestro is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Posts: 58 EEmaestro User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 17 h 52 m 2 sec
Reputation Power: 2
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 open-braces "{" and close-braces "}" in your program. They must be the same.
(2) Do the same thing for open-paren's "(" and close-paren's ")".

Reply With Quote
  #4  
Old September 12th, 2012, 03:13 AM
x.sophie.x x.sophie.x is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Location: Bristol
Posts: 13 x.sophie.x User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 14 m 32 sec
Reputation Power: 0
Quote:
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 open-braces "{" and close-braces "}" in your program. They must be the same.
(2) Do the same thing for open-paren's "(" and close-paren's ")".


Thanks guys! Fixed it!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Do while loops

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap