473,387 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

functions and pointers

8
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

✓ answered by donbock

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 @weaknessforcats 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.

3 1551
weaknessforcats
9,208 Expert Mod 8TB
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 Expert 2GB
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 @weaknessforcats 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 Expert Mod 8TB
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
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...
1
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...
3
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
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....
2
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
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...
23
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...
38
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...
26
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
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...

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.