473,890 Members | 1,761 Online

Creating pythagorean triples from input.

A pythagorean triple is a triple <a,b,c> whose components are positive
integers satisfying a*a + b*b = c*c. An example is <3,4,5> since 3*3 + 4*4 =
9 + 16 = 25 = 5*5.

I want to write a function to extract pythagorean triples from an input
stream. The input is formatted so that only the first two components <a,b>
of a pythagorean triple are specified. The function signature will be:

std::istream &operator>>(std ::istream &is, PythagoreanTrip le &triple);

Can anyone write me some good working code that implements this
functionality? This is not for a school project in case you're wondering.
Thanks.
Jul 22 '05
37 3429
"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:6pymd.6198 98\$8_6.573199@a ttbi_s04...
"Jason Heyes" <ja********@opt usnet.com.au> wrote...
Well how would I know?

Well who else should know? You started the thread.
I'm the one asking for help remember?

Better than you might think...
This was the whole point of the exercise. You write code that solves the
problem so I can see how its done.

What to do when the numbers are not part of a triple is a _requirement_.
You took up setting requirements, don't you quit now!

And what "exercise" are you talking about? Is that all some kind of
elaborate scheme to see if the newsgroup is going to play along? I for
one am not going to.

Figure out what you need, state it clearly, and then we can help. If not,
you might think of finding a different place where those who have nothing
better to do will invent the problems to solve and solve them for you
while
you watch.

V

Ok. The requirements say to write a function that reads pythagorean triples.
So the appropriate action when a pythagorean triple is not specified in the
input is to fail. Its just the same as when an integer isn't specified in
the input and operator>>(std: :istream &, int &) fails. Why is this not
obvious? Here is how the function could be used in a program:

PythagoreanTrip le triple;
while (cin >> triple)
cout << "PythagoreanTri ple <" << triple.a << ", " << triple.b << ", " <<
triple.c << ">" << endl;

Please don't regard this as an amendment to the original requirements
because it isn't. The original requirements are complete. Everything I've
said here is obvious.
Jul 22 '05 #21
"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:RHymd.6199 56\$8_6.90787@at tbi_s04...
"Jason Heyes" <ja********@opt usnet.com.au> wrote...
The requirements will not change and they are clear (except about syntax
parsing). If you don't want to do the task then don't help. But don't be
afraid to put out your best effort. This isn't an exercise in me giving
criticism to others. I would never do that. I want to learn from others
by reading their best code. What could be wrong with that?

Wrong? The best code is written when the requirements are the clearest.
You are trying to get people to both write the requirements and the code
to meet them. What is this, circus? If you want to learn to set program
or product requirements, this is not the right place. Try newsgroup
comp.software-eng. At this point you've been given relevant solutions,
go use them. Come back when you have some other _language_ problem.

If you want to see plenty of code, google for it. Many good products are
shipped in source code form. Read it, learn it. Have you tried books?
Many good books have source code in them, didn't you know? What about
magazines? C/C++ User's Journal, Dr.Dobbs Journal, to name a couple.
They are good source of decent code as well..

V

This is the last time I ask people to write code to requirements in this
forum. It just gets too personal.
Jul 22 '05 #22

"Jason Heyes" <ja********@opt usnet.com.au> wrote in message
news:41******** **************@ news.optusnet.c om.au...
"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:6pymd.6198 98\$8_6.573199@a ttbi_s04...
"Jason Heyes" <ja********@opt usnet.com.au> wrote...
Well how would I know?

Well who else should know? You started the thread.
I'm the one asking for help remember?

Better than you might think...
This was the whole point of the exercise. You write code that solves the
problem so I can see how its done.

What to do when the numbers are not part of a triple is a _requirement_.
You took up setting requirements, don't you quit now!

And what "exercise" are you talking about? Is that all some kind of
elaborate scheme to see if the newsgroup is going to play along? I for
one am not going to.

Figure out what you need, state it clearly, and then we can help. If
not,
you might think of finding a different place where those who have nothing
better to do will invent the problems to solve and solve them for you
while
you watch.

V

Ok. The requirements say to write a function that reads pythagorean
triples. So the appropriate action when a pythagorean triple is not
specified in the input is to fail. Its just the same as when an integer
isn't specified in the input and operator>>(std: :istream &, int &) fails.
Why is this not obvious? Here is how the function could be used in a
program:

PythagoreanTrip le triple;
while (cin >> triple)
cout << "PythagoreanTri ple <" << triple.a << ", " << triple.b << ", "
<< triple.c << ">" << endl;

Please don't regard this as an amendment to the original requirements
because it isn't. The original requirements are complete. Everything I've
said here is obvious.

It still doesn't seem tremendously difficult, making a few assumptions on

class PythagoreanTrip le
{
public:
PythagoreanTrip le(int a, int b, int c);
};

istream& operator>>(istr eam& in, PythagoreanTrip le& t)
{
int a, b;
if (!(is >> a && is >> b))
return is;
double c = (int)sqrt((doub le)a*a + (double)b*b);
if (c != floor(c))
{
is.setstate(ios _base::fail);
return is;
}
t = PythagoreanTrip le(a, b, (int)c);
return is;
}

It can definitely be improved in various ways, but its a start. Its also
untested.

john
Jul 22 '05 #23
Jason Heyes wrote:
I would help if you could cogently explain why anybody not a student
would need to solve this trivial problem yet require help to do so.

The "trivial" problem relates to a recurring problem in design and coding
that I currently experience. A few questions that arise are:

* How should I design the PythagoreanTrip le class?
* Should I have a factory class for PythagoreanTrip le?
* Is it the role of operator>> to verify its inputs?
* Should PythagoreanTrip leFactory::crea te be written so that it returns
boolean?

These are questions I could answer with some good code - if only I had some.

The first 2 and your last point have nothing to do with what you asked.
Point 3 is actually a good point.
But:
1) you didn't raise that question in your posting at all.
2) sure it should verify its input. The interesting part however
is: How to react in case of invalid input?
But this question wasn't asked either.

As it turns out, your question is *not* about pythagorean triples. You
would have the very same problems when needing to read any other input,
eg. such as a date, from a formatted text stream.

Maybe you should rephrase your question?

--
Karl Heinz Buchegger
Jul 22 '05 #24
Jason Heyes wrote:

"Karl Heinz Buchegger" <kb******@gasca d.at> wrote in message
Jason Heyes wrote:

A pythagorean triple is a triple <a,b,c> whose components are positive
integers satisfying a*a + b*b = c*c. An example is <3,4,5> since 3*3 +
4*4 =
9 + 16 = 25 = 5*5.

I want to write a function to extract pythagorean triples from an input
stream. The input is formatted so that only the first two components
<a,b>
of a pythagorean triple are specified. The function signature will be:

std::istream &operator>>(std ::istream &is, PythagoreanTrip le &triple);

Can anyone write me some good working code that implements this
functionality? This is not for a school project in case you're wondering.
Thanks.
What I am wondering is: What is your *exact* problem?
The task sounds easy enough. Or not so easy, depending on what you
are willing to invest in syntax parsing.

Post your attempt at it, and tell us where your problem is, which
part of it you can't do or have no idea on how to do it.

--
Karl Heinz Buchegger

The task has nothing to do with syntax parsing. This should have been made
clearer in the requirements.

'an example is <3,4,5>'
So I take it for granted that your input equals "<3,4,5>"

Input := "<" number "," number "," number ">" .

If this is not what you want or where your problem is, then please
be specific. Every regular in this newsgroup knows what you mean
with 'ppythagorean triplet'. You don't need to talk at length about
this topic when at the same time you don't even tell us what your
real problem is.
As for my exact problem there are several
aspects of design and coding that aren't working for me. These will become
clearer as I see more and more code. The problem isn't easy to explain.
If you cannot express your problem, how should we know what to
Doing things this way is much easier, I believe.

Not really. Easier for you. But definitly not for us. We have
to use our crystal balls to guess your question. The performance
of your question up to now, should already have told you that.
--
Karl Heinz Buchegger
Jul 22 '05 #25
Jason Heyes wrote:

Ok. The requirements say to write a function that reads pythagorean triples.
So the appropriate action when a pythagorean triple is not specified in the
input is to fail. Its just the same as when an integer isn't specified in
the input and operator>>(std: :istream &, int &) fails. Why is this not
obvious?
You are funny, you know?

You start a thread which asks us to write code for something that looks
a lot like homework. Only after several answers it turns out that your
real problem is located in a very different section:

How to proceed if an input operator detects invalid input.

With no word have you addressed your real problem in the original
posting. You could eg. have asked

How do I proceed in in stream input operator if I detect an invalid
input? I know that eg reading an int may fail. How do I do the same
for my class? How do I force the stream to an invalid state? Or
are there better ways? What about throwing an exception?
Any thoughts welcome.

Instead you teach us a lot about phtagorean triplets which are really
unimportant to your real question. You would the very same problem
when you read eg. a date and get 30-feb-2004 as input.

To summarize: You don't ask the question you should ask but complain
Here is how the function could be used in a program:

PythagoreanTrip le triple;
while (cin >> triple)
cout << "PythagoreanTri ple <" << triple.a << ", " << triple.b << ", " <<
triple.c << ">" << endl;

Please don't regard this as an amendment to the original requirements
because it isn't. The original requirements are complete. Everything I've
said here is obvious.

Dream on.

Good luck to you.

--
Karl Heinz Buchegger
Jul 22 '05 #26
"John Harrison" <jo************ *@hotmail.com> wrote in message
news:30******** *****@uni-berlin.de...
It still doesn't seem tremendously difficult, making a few assumptions on

class PythagoreanTrip le
{
public:
PythagoreanTrip le(int a, int b, int c);
};

istream& operator>>(istr eam& in, PythagoreanTrip le& t)
{
int a, b;
if (!(is >> a && is >> b))
return is;
double c = (int)sqrt((doub le)a*a + (double)b*b);
if (c != floor(c))
{
is.setstate(ios _base::fail);
return is;
}
t = PythagoreanTrip le(a, b, (int)c);
return is;
}

It can definitely be improved in various ways, but its a start. Its also
untested.

john

This code achieves almost all of the required functionality so thats great.
As an improvement I would suggest adding tests for integers a and b so that
<-3,-4,5> is not mistaken for a pythagorean triple. But thats a minor issue.
Well done.
Jul 22 '05 #27
>
This code achieves almost all of the required functionality so thats
great. As an improvement I would suggest adding tests for integers a and b
so that <-3,-4,5> is not mistaken for a pythagorean triple. But thats a
minor issue. Well done.

Well gee, thank you very much.

I would be worried about the overflow that could occur when converting from
a double to an int.

You could consider using unsigned int instead of int if you don't
consider -3, -4 and 5 a Pythagorean triple.

john
Jul 22 '05 #28
"Jason Heyes" <ja********@opt usnet.com.au> wrote in message news:<41******* *************** *@news.optusnet .com.au>...

The "trivial" problem relates to a recurring problem in design and coding
that I currently experience. A few questions that arise are:

* How should I design the PythagoreanTrip le class?
* Should I have a factory class for PythagoreanTrip le?
* Is it the role of operator>> to verify its inputs?
* Should PythagoreanTrip leFactory::crea te be written so that it returns
boolean?

These are questions I could answer with some good code - if only I had some.
Well, we could write code. But if that's what you want to know, why
didn't you ask for these specific points? It's a lot easier to answer
these questions. No need to write code.
* How should I design the PythagoreanTrip le class?
Why do you think you need such a class? The basic requirement is a function
f: int x int -> int
C++ is not Java: when you want a function, write a function.
* Should I have a factory class for PythagoreanTrip le?
No class = no factory
* Is it the role of operator>> to verify its inputs?
Usually yes. Some errors might be caught by the class ctor or assignment,
but in general operator>> has to extract a number of fields from the
stream and then buid a class from those fields. If one of these fields
is missing or malformed, you cannot proceed. That means operator>> is
the only function capable of flagging that error.
* Should PythagoreanTrip leFactory::crea te be written so that it returns
boolean?

No. What would it return? Success or failure? That is better communicated
using exceptions. A Factory::create () should return the created object.

Regards,
Michiel Salters
Jul 22 '05 #29
> * How should I design the PythagoreanTrip le class?

The way you'd design any other class. Try to write up what it needs to
do, try to figure out what it needs for input to do whatever you want
it to do.
* Should I have a factory class for PythagoreanTrip le? That is a design choice and it seems you've already made it seeing
* Is it the role of operator>> to verify its inputs?
Depends. This is one of those things you need to figure out as said in
the first point. * Should PythagoreanTrip leFactory::crea te be written so that it returns boolean? Depends again on your needs.
These are questions I could answer with some good code - if only I had some.

Then write some. It would probably have cost you less time to write
something that works then that you've spend composing messages here
and replying to people that say do it yourself.
If you do make that class you can check it to your needs, does it
satisfy them all? If so you got a keeper, now go and make it
efficient.
Jul 22 '05 #30

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