473,586 Members | 2,776 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

functions and pointers

8 New Member
im have to write a program that calculates the square root of a number by using functions and pointers. If the number is less then 0, the function squareRoot should return 0 and print in main that the number cant be calculated. If the number is larger than 0, the function squareRoot should return 1 and print the answer in main


#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int squareRoot(int *number)
{
if (number < 0)
{
return 0;
}
else if (number >= 0)
{
*number = sqrt(*number);
return 1;
}
}


int main()
{
int *num;
printf("Type in a number: ");
scanf("%d", &num);
squareRoot(&num );
printf("Square root = %d\n", num);
return 0;
}
Sep 29 '18 #1
3 1565
weaknessforcats
9,208 Recognized Expert Moderator Expert
Floating point numbers do not have square roots. Only integers do this.

The relational operators don't work with floating point. When you compare two floats you need also supply a tolerance of error and this is done using special calls.


Please remove floating point and replace with integer.



float type sould never be used uness you have low accuracy scientific results supplied by out-of-date software. the replacement for float is double.


If you don't have the above, you use int. Or long. But not float.
Sep 30 '18 #2
donbock
2,426 Recognized Expert Top Contributor
In squareRoot, comparison of number to 0 should be comparing *number; I suggest replacing else if with else.

In main, you should not print square root value if squareRoot returns an error. Definition of num has wrong type - there should be a compiler error.

I don’t know what @weaknessforcat s means regarding square root not working for floating point values. Actually, the function prototype for sqrt requires you to use type double. It is your decision whether you want the program to compute square roots of integer or floating point user inputs. If the input is integer then you have to assign that value to a double variable before calling sqrt.
Oct 4 '18 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
If an integer, like 25 has an integer, like 5, that can be multiplied by itself, then the 5 is called a square root and the 25 is called a perfect square.

Since floating point numbers cannot be expressed as the ratio of two integers, they are called irrational. These numbers, not being perfect square, do not have a square root. Now you can approximate, apply rounding rules, etc and guess a "square root". But unless the original number is the ratio of two integers, and a perfect square, this guess is not a square root.


In addition both 0 and negative numbers don't have square roots either.


A good example is trying to find the square root of 2.
Oct 4 '18 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
4520
by: _ivan | last post by:
Hello, So I think the answer to this question is going to be rather simple. Basically I have a pointer to a class, and I want to have a function to call to initialize it. After this function is called, the pointer is still NULL. Here is a simplified version, but basically the same:
1
6713
by: Alex | last post by:
Is there any problem with sending function pointers through in a variable argument list? I have a function like the following: typedef (*ptr2FuncType1)( int ); typedef (*ptr2FuncType2)( double ); void SomeClass::DoSomething( unsigned int num_something, unsigned long num_something_else, ptr2FuncType1 handler_one, ... ) The functions I...
3
1604
by: lallous | last post by:
Hello #include <iostream> #define CALL_MEMBER_FN(object,ptrToMember) ((object).*(ptrToMember)) class Fred; typedef int (Fred::*FredMemFn)(char x, float y); class Fred {
3
1666
by: Filipe Valepereiro | last post by:
I all. I need to write a function that convert one string into a vector. This string represent a serialized form of the vector. So i come up with this piece of code, that compile just fine. The problem is linking, VC6++ give me this error: unresolved external symbol "class std::vector<double,class std::allocator<double> > __cdecl...
2
2239
by: kelvSYC | last post by:
What's the difference between arrays and pointers to arrays, in terms of passing them as parameters to a function? -- I am only a mirage.
5
2587
by: Rolf Wester | last post by:
Hi, I want to pass a C-function as a function parameter but I don't know how to that correctly. In the example below how would I have to declare the function argument in the my_sort function definition? Thank you in advance for any help. Regards Rolf
23
3983
by: Timothy Madden | last post by:
Hello all. I program C++ since a lot of time now and I still don't know this simple thing: what's the problem with local functions so they are not part of C++ ? There surely are many people who will find them very helpfull. gcc has them as a non-standard option, but only when compiling C language code, so I'm afraid there might be some...
38
2576
by: James Brown | last post by:
All, I have a quick question regarding the size of pointer-types: I believe that the sizeof(char *) may not necessarily be the same as sizeof(int *) ? But how about multiple levels of pointers to the same type? Would sizeof(char **) be the same as sizeof(char *)? And if it is, would the internal representation be the same in both cases? ...
26
25578
by: tnowles00 | last post by:
Hi friend, what is the use of function pointer in c language and where it is useful? tell with simple example...? plz help me.
2
1798
by: asd | last post by:
Hello All, I am going back to C++ after 3 years and I am struggling with it. FYI, This topic has 2 questions. Here is my problem. I have class, which must have a function which returns a pointer to a function. Here is how I did it.
0
7911
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7839
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8200
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8338
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8215
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6610
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1448
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1179
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.