![]() |
| | #1 |
| Registered User Join Date: Dec 2009
Posts: 9
| Code: # include <stdio.h>
# include <conio.h>
int main()
{
/* Variables */
float root;
float guess;
float answer;
answer = guess*guess;
printf ("Please enter a number that you wish to guess the square root of ");
scanf ("%f",&root);
printf ("\nPlease enter your guess ");
scanf ("%f", &guess);
answer = guess*guess;
while (answer == root)
printf ("\nWell done. your guess is exactly the square root");
if (answer < root)
answer = guess*guess;
{
printf ("\nYour guess is lower than the square root of %f. Please enter a higher guess ", root );
scanf ("%f", &guess);
if (answer > root)
answer = guess*guess;
printf ("\nYour guess is higher than the square root of %f. Please enter a lower guess ", root );
scanf ("%f", &guess);
if (answer + 0.05 == root)
answer = guess*guess;
printf ("\nYour guess of %f is within 0.5 of the square root of %f ", guess, root);
scanf ("%f", &guess);
if (answer - 0.05 == root)
answer = guess*guess;
printf ("\nYour guess of %f is within 0.5 of the square root of %f ", guess, root);
scanf ("%f", &guess);
}
getch();
}
|
| Lisa_townsend is offline | |
| | #2 | |
| Registered User Join Date: Sep 2006
Posts: 3,720
| Quote:
Then your while() loop never executes, because your condition is wrong. It should be while(answer != root), instead. After the guess is correct, you need a "continue" statement to stop further looping RIGHT NOW. A "break" statement, would have the same effect. Change those and you'll be very close. | |
| Adak is offline | |
| | #3 |
| Registered User Join Date: Dec 2009
Posts: 9
| We have to give a number we want to find the square root of and guess what it is by a refinement method. I tried putting while(answer != root) instead and now when I enter the root then the guess it says well done I have guessed it correctly for any number. I have 3 different books in front of me and I cant find where I'm going wrong. |
| Lisa_townsend is offline | |
| | #4 |
| Registered User Join Date: Sep 2006
Posts: 3,720
| first things first - stop calling the number you're inputting "root". It is NOT a root. That is the number that you need to find the square root OF. |
| Adak is offline | |
| | #5 |
| Registered User Join Date: Dec 2009
Posts: 9
| The variable is now been changed from root to number, now what? |
| Lisa_townsend is offline | |
| | #6 |
| Registered User Join Date: Sep 2006
Posts: 3,720
| change the inital declaration of answer = guess * guess. Just int answer is fine. Then it's "while(guess * guess != answer)" |
| Adak is offline | |
| | #7 |
| Robot Join Date: Mar 2009
Posts: 372
| This is probably not what you intended: Code: if (answer < root)
answer = guess*guess;
{
|
| Memloop is offline | |
| | #8 |
| Registered User Join Date: Sep 2006
Posts: 3,720
| Remove all instances of this line of code: Code: answer = guess*guess;
/* and add this line right after the start of the while() loop: */
scanf ("%f", &guess);
//remove all other instances of that line from inside the loop
|
| Adak is offline | |
| | #9 |
| Registered User Join Date: Dec 2009
Posts: 9
| I have made the changes, when I enter a guess of 3 for the square of 9 it says correct but I have entered the number I wanted to guess for of 9 again then entered a guess of 2 which came back saying its too low which is correct but when I enter the guess of 1 after it says the guess is too high. Then once another number is entered it says the new guess is within the square of 9 which is wrong so can you tell me why this is and how do I correct it. |
| Lisa_townsend is offline | |
| | #10 |
| Registered User Join Date: Sep 2006
Posts: 3,720
| Make the changes for post #8 in this thread. You posted at the same time. |
| Adak is offline | |
| | #11 |
| Registered User Join Date: Dec 2009
Posts: 9
| I dont know what you mean by instances, do you mean delete all the lines of scanf ("%f", &guess); because I have tried this and it wont print anything after the first 2 numbers are entered. Could ya take my code and correct it for me please or advise me of a site which can correct it? I may as well be trying to correct chinese without any basis knowledge of it or something thats the best way I can explain trying to make this code out put what I want. Sorry to be a pain in the butt! |
| Lisa_townsend is offline | |
| | #12 |
| Registered User Join Date: Sep 2006
Posts: 3,720
| Yes, I mean delete all the scanf() lines, inside the while loop, except one. That one, goes right under the while() condition: Please post your revised program so I can refer to the latest version. |
| Adak is offline | |
| | #13 |
| Registered User Join Date: Dec 2009
Posts: 9
| I have left the variables as they were so I know what they are, but you can take the code and change it to what you think is right. I called the first var root because its the square root I am guessing if ya know what I mean Code: # include <stdio.h>
# include <conio.h>
int main()
{
/* Variables */
float root;
float guess;
float answer;
answer = guess*guess;
printf ("Please enter a number that you wish to guess the square root of ");
scanf ("%f",&root);
printf ("\nPlease enter your guess ");
scanf ("%f", &guess);
answer = guess*guess;
while (answer == root)
scanf ("%f", &guess);
answer = guess*guess;
printf ("\nWell done. your guess is exactly the square root");
if (answer < root)
answer = guess*guess;
{
printf ("\nYour guess is lower than the square root of %f. Please enter a higher guess ", root );
if (answer > root)
answer = guess*guess;
printf ("\nYour guess is higher than the square root of %f. Please enter a lower guess ", root );
if (answer + 0.05 == root)
answer = guess*guess;
printf ("\nYour guess of %f is within 0.5 of the square root of %f ", guess, root);
if (answer - 0.05 == root)
answer = guess*guess;
printf ("\nYour guess of %f is within 0.5 of the square root of %f ", guess, root);
}
getch();
}
|
| Lisa_townsend is offline | |
| | #14 | |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 11,292
| Quote:
Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? | |
| quzah is offline | |
| | #15 |
| Registered User Join Date: Dec 2009
Posts: 9
| Heres what I want to do: 1). Choose a number to find the square root of lets say 9 2). make a guess at what the square root could be, I know its 3 but lets say 2 3). take the guess of 2 multiply it by itself then compare it to the square of 9 4) If your guess is correct in this case if you choose 3 you would be correct and a well done message would come up 5). If your guess is within 0.05 higher or lower than the square of 9 say you are close enough 6). If your guess is less or more than 0.05 print message your guess is lower or higher than the square root please enter a higher or lower number so as I picked 2 which multiplied is 4 the message would say its lower than the root please enter a higher guess etc till you guess correctly or come within 0.05 of the square. All guesses should be displayed underneath each other until you find the number. I know this would be a simple program to a programmer but to someone who isnt computer literate or code literate Its like a foreign language. |
| Lisa_townsend is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Code not working? | Elysia | C++ Programming | 12 | 04-06-2009 01:57 AM |
| please help me with this code... arrays arent working. | ominub | C Programming | 3 | 02-24-2009 08:49 PM |
| C code not working | D3ciph3r | C Programming | 2 | 05-27-2005 04:13 PM |
| Trying to eject D drive using code, but not working... :( | snowfrog | C++ Programming | 3 | 05-07-2005 07:47 PM |
| Linked List Working Code | Linette | C++ Programming | 9 | 01-24-2002 12:00 PM |