#include<stdio.h>
#include<math.h>
double distance(double a ,double b,double c,double d);
int main()
{
double x1,y1,x2,y2;
double result;
printf("Enter four numbers:");
scanf("%f%f%f%f",&x1,&y1,&x2,&y2);
result=distance(x1,y1,x2,y2);
printf("The distancd is %.1f",result);
return 0;
}
double distance(double a,double b,double c,double d)
{
return sqrt((a-c)*(a-c)+(b-d)*(b-d));
}
>
> why it print wrong result? I can't find the wrong place.
>
>
> #include<stdio.h>
> #include<math.h>
>
> double distance(double a ,double b,double c,double d);
>
> int main()
> {
> double x1,y1,x2,y2;
> double result;
>
> printf("Enter four numbers:");
> scanf("%f%f%f%f",&x1,&y1,&x2,&y2);
Print the values of x1, x2, y1, and y2 here.
Are they, what you expect?
> result=distance(x1,y1,x2,y2);
>
> printf("The distancd is %.1f",result);
>
> return 0;
> }
> double distance(double a,double b,double c,double d)
> {
Or, print the values of a, b, c, and d here. E.g.:
std::cout << a << " " << b << " " << c << " " << d << "\n";
> return sqrt((a-c)*(a-c)+(b-d)*(b-d));
>
> }
Best
Kai-Uwe Bux
> why it print wrong result? I can't find the wrong place.
>
>
> #include<stdio.h>
> #include<math.h>
>
> double distance(double a ,double b,double c,double d);
>
> int main()
> {
> double x1,y1,x2,y2;
> double result;
>
> printf("Enter four numbers:");
> scanf("%f%f%f%f",&x1,&y1,&x2,&y2);
The penalties for lying to the I/O routines are often severe.
The reason why it prints the wrong result is that you're using
scanf. Try doing it the C++ way:
#include <iostream>
#include <cstddef>
#include <cmath>
extern double distance( double a, double b, double c, double d ) ;
int
main()
{
std::cout << "Please enter four numbers: " ;
double x1 ;
double y1 ;
double x2 ;
double y2 ;
if ( std::cin >> x1 >> y1 >> x2 >> y2 ) {
std::cout << "The distance is: "
<< distance( x1, y1, x2, y2 )
<< std::endl ;
} else {
std::cerr << "Those weren't four legal numbers"
<< std::endl ;
}
return EXIT_SUCCESS ;
}
double
distance(
double x1,
double y1,
double x2,
double y2 )
{
return std::sqrt( ((x1-x2) * (x1-x2)) + ((y1-y2)*(y1-y2)) ) ;
}
You should just forget about stdio.h (except for a few things
like remove or rename); the interface is horribly broken, and
extremely error prone and unnatural. (The iostream interface
isn't without problems either, but it's still an order of
magnitude better than stdio.h.)
--
James Kanze (GABI Software) email:james...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
A question of style, but is it *really* necessary to be so verbose?
I honestly don't think this is any less clear (if anything, it's the
contrary):
double x1, y1, x2, y2;
Using %f in scanf doesn't read a double, it reads a float.
Best not to use scanf, it's a dangerous function (as you're
finding out...)
Your style is only usable when declaring several uninitialized
variables of the same type. That makes it very unusual.
In all other cases, James' version is much better. Using it even for
this corner case, is at least more consistent.
Bo Persson
So you call this "unusable"?
double x1 = 0, y1 = 0, x2 = 0, y2 = 0;
"unusual"
--
Ian Collins
I said unusual, but you are close. Try this one, and you are already
in trouble:
double* x1 = 0, y1 = 0, x2 = 0, y2 = 0;
Bo Persson
Bo Persson wrote:
> I said unusual, but you are close. Try this one, and you are already
> in trouble:
>
> double* x1 = 0, y1 = 0, x2 = 0, y2 = 0;
That's why I'd use:
double *x1, *y1, ...;
However, this leads us to another pointless discussion which coding
style is the best and why each other is so horribly ugly. I think that
each style is good as long as you can easily read and understand the
code. There are better things to discuss, aren't there?
Pawel Dziepak
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org
iEYEARECAAYFAkkM1DkACgkQPFW+cUiIHNo1uQCfXpFefGkMSReJUkeiecJdFuCZ
RcIAn2QE9Fgf+lb4IzvLkkKojFHqTx50
=CXW2
-----END PGP SIGNATURE-----
Well, I guess the reference is the words "only usable when ...", which is
refuted successfully by the example.
Best
Kai-Uwe Bux
But, if you do:
double* x1 = 0;
double *\y1 = 0;
double x2 = 0;
double y2 = 0;
It doesn't matter where you put the * does it? Which is the whole point.
Something getting "broken" by where you happen to put a space is not very
good IMO.
Jim Langston wrote:
> It doesn't matter where you put the * does it? Which is the whole
> point. Something getting "broken" by where you happen to put a space is
> not very good IMO.
I see your point, but I am used to writing such kind of things mostly in
one line. However, I'd write this in the following style:
double x1, y1;
double x2, y2;
A few variables in one line, but somehow connected which each other.
That's how *I* write my code. I don't know if it is better or worse
style, because I don't think that it really matters. All three styles
are quite easy to read and understand (unless you don't know C/C++).
I really think that this discussion is pointless. You prefer your style
- - use it, but I'll use mine.
Pawel Dziepak
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org
iEYEARECAAYFAkkM5fAACgkQPFW+cUiIHNo+4ACgsBoGN87QXXgLd9uv8YP44B8m
wwIAoLD48tMNxBRSiafulhClqk+ScLYI
=tCL7
-----END PGP SIGNATURE-----
You are basically saying that because pointers use a special syntax,
that means that you should put each variable declaration in a separate
line even if those variables are not pointers?
You might argue consistency of style. Well, you might have difficult
reading code like this:
double x = 0, y = 0;
double* xp = 0;
double* yp = 0;
but I don't.
I am saying that as some of the declarators apply only to the first
declared object, and some to all of them, it is confusing. Especially
if you also add a bunch of initializers.
Defining a bunch of uninitialized variables is unusual, and should not
use a special syntax just bacause you can.
>
> You might argue consistency of style. Well, you might have
> difficult reading code like this:
>
> double x = 0, y = 0;
> double* xp = 0;
> double* yp = 0;
>
> but I don't.
Good for you. :-)
Bo Persson
Yes and no. Obviously, even better would be something like:
Point p1 ;
Point p2 ;
The verbosity is largely due to a lack of higher level
abstractions. But I didn't want to get into that in my answer,
so I just left it.
Just as obviously, it's pretty much forbidden to declare more
than one variable in a single statement. This particular case
is probably at the limit of where the rule is applicable, but in
well designed code, you probably don't run into such cases often
enough to justify allowing exceptions.
> >> double x1 = 0, y1 = 0, x2 = 0, y2 = 0;
> > I said unusual, but you are close. Try this one, and you are
> > already in trouble:
> > double* x1 = 0, y1 = 0, x2 = 0, y2 = 0;
> You are basically saying that because pointers use a special
> syntax, that means that you should put each variable
> declaration in a separate line even if those variables are not
> pointers?
That's not really the only point.
> You might argue consistency of style.
That's the real point. There are cases where not following the
rule won't really cause problems; the original code is probably
one of them. But they aren't frequent enough to warrent making
an exception for them. (And how would you formulate a rule
defining the exceptions?)