GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-May-2008, 23:27
contherun contherun is offline
New Member
 
Join Date: Apr 2008
Posts: 18
contherun is on a distinguished road
Question

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:
#include <stdio.h>
#include <math.h>

void main()
{
            int input, lastFactor
            printf("%s", "Enter a number:");
            scanf("%d", &input);

	lastFactor = sqrt(input); //Returns zero all the time
	printf("%d", lastFactor);
	printf("\n");
}

How do I fix this problem?
  #2  
Old 04-May-2008, 06:19
L7Sqr L7Sqr is offline
Member
 
Join Date: Jul 2005
Location: constant limbo
Posts: 103
L7Sqr will become famous soon enough

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  
Old 04-May-2008, 07:42
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,627
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: sqrt() always returns zero????


Quote:
Originally Posted by contherun
tried to calculate...
how do i fix this problem please help????

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:
Enter a number:123 11
That's normal.

So is
Code:
Enter a number:0.123 0

Now, if you really had something in mind like
Code:
Enter a number:123 11.090536

or

Code:
Enter a number:0.123 0.350714

Then change your variable declarations to make their type be float, instead of int

Regards,

Dave
  #4  
Old 04-May-2008, 12:47
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,627
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: sqrt() always returns zero????


Quote:
Originally Posted by davekw7x
Then change your variable declarations to make their type be float, instead of int

And, of course, use %f instead of %d format specifiers in scanf() and printf() statememts.

Regards,

Dave
 
 

Recent GIDBlogToyota - 2008 July Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Forum Jump

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

All times are GMT -6. The time now is 23:39.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.