![]() |
|
#1
|
|||
|
|||
sqrt() always returns zero?I have tried to calculate the square root of an entered value but it always returns zero.
Here is the code CPP / C++ / C Code:
How do I fix this problem? |
|
#2
|
|||
|
|||
Re: sqrt() always returns zero????I dont know of a sqrt function that takes an int. You may want to view a man page on sqrt. (Hint: floating point numbers)
Also, are you checking the number being passed to sqrt? Are you sure input was entered correctly? scanf returns a value, you should see what that indicates and what you get. |
|
#3
|
|||
|
|||
Re: sqrt() always returns zero????Quote:
1. The code that you posted won't compile because there is no semicolon at the end line number 6. For best chances of helpful responses with minimum iterations, I respectfully suggest that you make sure to show us exactly what you are working on. You might also mention what compiler you are using. Sometimes it makes a difference to people who are trying to help. 2. After I installed the missing semicolon, my compiler rejects the code because it insists that the main() function be declared with an int return type. Some compilers are happy with void main(), but it is not standard usage. This is not a "Big Deal" with me personally, but I just thought that while you are learning, you might want to learn standard ways of doing things. 3. The program gives the correct square root of an integer, but if there is a fractional part, it is truncated, since you are using integer variables. 4. When you submit a question like this, it is a good idea to give us some details. (What input you gave it; what output it gave you; what you expected. Stuff like that.) So for example, if you see something like Code:
So is Code:
Now, if you really had something in mind like Code:
or Code:
Then change your variable declarations to make their type be float, instead of int Regards, Dave |
|
#4
|
|||
|
|||
Re: sqrt() always returns zero????Quote:
And, of course, use %f instead of %d format specifiers in scanf() and printf() statememts. Regards, Dave |
Recent GIDBlog
Toyota - 2008 July Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| DeviceIoControl returns 87 error code : Invalid parameter | TriveniPrabhu | MS Visual C++ / MFC Forum | 3 | 29-Jan-2008 20:21 |
| How Do I Create a Function that returns a usable array of pointers to structures? | UncleRic | C Programming Language | 2 | 03-Sep-2007 11:17 |
| The sqrt function | jlwelch | C Programming Language | 5 | 10-Oct-2006 19:27 |
| Check a number whether it is a sqrt of another number. | dendelion | C Programming Language | 10 | 07-Aug-2006 01:48 |
| C++ Linear Regression with Outliers | bartb | CPP / C++ Forum | 1 | 06-Mar-2005 08:27 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The