473,938 Members | 20,004 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

The problem with square and sqrt function

Hi ,I think the follow program is right in logical But why the
compiler output :"square:declar ed identifier"
#include<stdio. h>
#include<math.h >

int main()
{
double x1;
double x2;
double y1;
double y2;
double x;
double y;
double distance;

printf("Enter the local of points: ");
scanf("%1f%1f%1 f%1f",&x1,&x2,& y1,&y2);

x=fabs(x1-x2);
y=fabs(y1-y2);
distance=sqrt(s quare(x)+square (y));

printf("The distance is %f",distance) ;

return 0;
}

Nov 4 '08 #1
6 4182
Blue sky wrote:
Hi ,I think the follow program is right in logical But why the
compiler output :"square:declar ed identifier"
Surely "/un/declared identifier".
distance=sqrt(s quare(x)+square (y));
Who is `square` when they're at home? (There's no `square` in
`<match.h>`.) Did you mean `sqrt`?

--
'Don't be afraid: /Electra City/
there will be minimal destruction.' - Panic Room

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Nov 4 '08 #2
Blue sky wrote:
Hi ,I think the follow program is right in logical But why the
compiler output :"square:declar ed identifier"
If that is the correct text of the error message, then your compiler is
defective. If an identifier is declared, that's never a good reason, in
itself, for issuing a diagnostic message. More to the point, the message
is not only pointless, but also inaccurate. The identifier "square" is
not declared anywhere in your program.

It's more likely that the compiler said "undeclared identifier". That is
a perfectly accurate description of your program, and more to the point,
it's precisely the biggest problem with your program. What you need is
to provide a declaration for square(). A definition is also needed, but
the definition could be in a different module.

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

int main()
{
double x1;
double x2;
double y1;
double y2;
double x;
double y;
double distance;

printf("Enter the local of points: ");
scanf("%1f%1f%1 f%1f",&x1,&x2,& y1,&y2);

x=fabs(x1-x2);
y=fabs(y1-y2);
You don't provide any definition for square(), so I can't be certain,
but I assume that square(x) calculates and returns the square of x. That
being the case, why are you bothering with the fabs() calls? The square
of (x1-x2) is always the same as the square of fabs(x1-x2), all you're
doing is wasting a small amount of space in your program and wasting a
small amount of time when it executes. Most importantly, you're creating
the potential for confusion when the next person who reads this code has
to waste time thinking "Is there some obscure reason why it might make
sense to call fabs() here?"
distance=sqrt(s quare(x)+square (y));

printf("The distance is %f",distance) ;

return 0;
}
Nov 4 '08 #3
Chris Dollin wrote:
Blue sky wrote:
>Hi ,I think the follow program is right in logical But why the
compiler output :"square:declar ed identifier"

Surely "/un/declared identifier".
> distance=sqrt(s quare(x)+square (y));

Who is `square` when they're at home? (There's no `square` in
`<match.h>`.) Did you mean `sqrt`?
(fx:later) Duh, I am the stupidz. No, Blue sky didn't mean `sqrt`.
Bad hedgehog. No slugs for you.

Blue sky -- you'll have to write your own `square` function,
or do the squaring inline. Since `x` and `y` are simple variables,

distance = sqrt( x * x + y * y );

will do handily.

--
'Don't be afraid: /Electra City/
there will be minimal destruction.' - Panic Room

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Nov 4 '08 #4
Chris Dollin wrote, On 04/11/08 11:49:
Blue sky wrote:
>Hi ,I think the follow program is right in logical But why the
compiler output :"square:declar ed identifier"

Surely "/un/declared identifier".
Agreed.
> distance=sqrt(s quare(x)+square (y));

Who is `square` when they're at home? (There's no `square` in
`<match.h>`.) Did you mean `sqrt`?
I doubt it. I suspect the OP it programming "by guess and by god" and
incorrectly guessed that there is a square function.

Other points you did not mention include:

The OP should flush stdout after outputting the prompt otherwise it
might not be displayed.

The value returned by scanf needs to be checked to see if the data was
correctly entered. Also scanf is not easy to use correctly.

Why on earth bother using fabs? The OP should thing about the result of
squaring a negative number.

There should be a newline at the end of the output. Otherwise it is not
guaranteed to be displayed and even if it is the result is harder to
read on many systems without it.
--
Flash Gordon
If spamming me sent it to sm**@spam.cause way.com
If emailing me use my reply-to address
See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/
Nov 4 '08 #5
On Nov 4, 7:36*pm, Flash Gordon <s...@spam.caus eway.comwrote:
Why on earth bother using fabs?
The OP should thin[k] about the result of
squaring a negative number.
Presumably, since he doesn't check that
scanf() sets its targets, he was concerned
about squaring an imaginary number. (g)

James
Nov 5 '08 #6
James Dow Allen wrote, On 05/11/08 04:48:
On Nov 4, 7:36 pm, Flash Gordon <s...@spam.caus eway.comwrote:
>Why on earth bother using fabs?
The OP should thin[k] about the result of
squaring a negative number.

Presumably, since he doesn't check that
scanf() sets its targets, he was concerned
about squaring an imaginary number. (g)
:-)

The OP was using fabs on the difference between two numbers and then
squaring the result of that. I don't think fabs will make the difference
NaNy less imaginary than it was :-)
--
Flash Gordon
If spamming me sent it to sm**@spam.cause way.com
If emailing me use my reply-to address
See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/
Nov 5 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
8711
by: cplusplus | last post by:
Hello, I have newbie question. I'm stuck on this current assignment. Write a program that prompts the user for two integer values, passes the values to a function where they are multiplied together and the square root of the product is returned and displayed for the user. The function should return a double. Hint: If you multiply an integer by 1.0 the result will be a double. For example:
7
7424
by: brian.digipimp | last post by:
Write a program that prompts the user to input a positive integer. It should then output a message indicating whether the number is a prime number. (Note: An even number is prime if it is 2. An odd integer is prime if it is not divisible by an odd integer less than or equal to the square root of the number.) I was given this assignment in my c++ class and I'm having trouble with it. I've looked at the professors lab solutions but I just...
2
3201
by: Protoman | last post by:
Can you help me? For 4, my square root funct gives 4 instead of 2; here's the code: #include <iostream> #include <cstdlib> using namespace std; template<class T> T Abs(T Nbr) {
32
5033
by: priyam.trivedi | last post by:
Hi! Could anyone tell me how to find the square root of a number without using the sqrt function. I did it by using Newton's Formula. How can it be done by using the Binomial Theorem/Taylor Series? Is there any other way of doing it rather than using series? Thank you, Priyam
0
1169
by: mastern200 | last post by:
I need to make a program where: In this program you MUST use the following functions and header file (library function): • #include<cmath > • fabs • pow • sqrt You run a company that ships satellite dishes to customers. The satellite dish is ordered based on the area of its opening (in square inches):
6
1715
by: StephQ | last post by:
I need to implement an algorithm that takes as input a container and write some output in another container. The containers involved are usually vectors, but I would like not to rule out the possibility of using lists. The problem is that I need two versions of it, depending if I'm adding the generated (by the algorithm) values to the target container or if I just modify pre-existing values of the target container. Efficiency is important...
0
1425
by: cognoid | last post by:
HI, I am completely new to DB2 and I am trying to get this user defined function to work. It doesn't seem to like the way I am using my input variables in the body of the function. I get the following error. Create user-defined function returns -206. DB15USER.random: 13: SQL0206N "V_A" is not valid in the context where it is used. LINE NUMBER=13. SQLSTATE=42703 Also, how do I set the default values of my parameters, I would...
3
2000
by: newbprogrammer | last post by:
I just started programming in c++ and i tried to explore and do programs on my own... So i wanted to do a program that would help me in my daily school work ... a program to solve quadratic equations.. I wrote the code correctly but errors are still detected and i have no idea why.. almost gave up and banged my head on the wall .. so i would really appreciate help.. This is the code so far.. #include <cmath> #include <iostream> using...
6
1558
by: pereges | last post by:
Ok, I have some problem with arrays which i want to use for storing rays in my ray tracing project. please have a little patience to read. I need to fire rays from a a rectangular plane. The rays are parallel to each other so their direction is same but they differ in their origin points or source location. I tried to determine the source location by creating a grid of the rectangular plane. The rays are going to be spaced from each...
0
10134
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9963
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11524
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
11289
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10657
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8218
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6075
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4905
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3501
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.