473,890 Members | 1,720 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
Jason Heyes wrote:
"John Harrison" <jo************ *@hotmail.com> wrote in message
news:2v******** *****@uni-berlin.de...
"Jason Heyes" <ja********@opt usnet.com.au> wrote in message
news:41****** *************** **@news.optusne t.com.au...
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::istre am &operator>>(std ::istream &is, PythagoreanTrip le &triple);

Can anyone write me some good working code that implements this
functionalit y? This is not for a school project in case you're wondering.
Thanks.

Is all that < , > part of your input? If not it sounds very easy, just
read two integers and calculate the third.

john

No the < , > symbols are not part of the input. Here is some code that reads
two integers and calculates the third:

int a, b;
if (!(is >> a && is >> b))
return is;
int c = (int)sqrt(a*a + b*b);

This is wrong code for the stated requirements.

So, if you know that it's wrong, you must know _what_ is wrong with it,
mustn't you? I mean, besides that it's not really a C++ program...
If do indeed know what is wrong with that code, why don't you fix it

The set of requirements is a bit vague as to what to do if the pair is not
part of a "pythagorea n triple". Throw an exception? Make up a default
0,0,0 triple?

V
Jul 22 '05 #11
"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. 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.
Please don't make me try. Doing things this way is much easier, I believe.
Jul 22 '05 #12
Jason Heyes wrote:
[..] This should have been made
clearer in the requirements. 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.
Please don't make me try. Doing things this way is much easier, I believe.

You mean, it is easier to direct people to do work giving them some vague
requirements and then ask them to do it again when the requirements change
instead of actually trying to iterate through the implementation process
yourself? Rejoice, brethren! Another marketing manager is born!
Jul 22 '05 #13
"Larry Brasfield" <do************ ***********@hot mail.com> wrote in message
news:4W******** ********@news.u swest.net...
"Jason Heyes" <ja********@opt usnet.com.au> wrote in message
news:41******** *************** @news.optusnet. com.au...
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?

Your request shows up here often enough to have become a FAQ.
See http://www.parashift.com/c++-faq-lit...t.html#faq-5.2
This is not for a school project in case you're wondering.

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.
Thanks.

--
--Larry Brasfield
email: do************* **********@hotm ail.com
Above views may belong only to me.

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.
Jul 22 '05 #14
"Victor Bazarov" <v.********@com Acast.net> wrote in message
Jason Heyes wrote:
No the < , > symbols are not part of the input. Here is some code that
reads two integers and calculates the third:

int a, b;
if (!(is >> a && is >> b))
return is;
int c = (int)sqrt(a*a + b*b);

This is wrong code for the stated requirements.

So, if you know that it's wrong, you must know _what_ is wrong with it,
mustn't you? I mean, besides that it's not really a C++ program...
If do indeed know what is wrong with that code, why don't you fix it

The set of requirements is a bit vague as to what to do if the pair is not
part of a "pythagorea n triple". Throw an exception? Make up a default
0,0,0 triple?

V

Well how would I know? I'm the one asking for help remember? This was the
whole point of the exercise. You write code that solves the problem so I can
see how its done.
Jul 22 '05 #15
"Victor Bazarov" <v.********@com Acast.net> wrote in message
Jason Heyes wrote:
[..] This should have been made
clearer in the requirements. 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.
Please don't make me try. Doing things this way is much easier, I
believe.

You mean, it is easier to direct people to do work giving them some vague
requirements and then ask them to do it again when the requirements change
instead of actually trying to iterate through the implementation process
yourself? Rejoice, brethren! Another marketing manager is born!

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?
Jul 22 '05 #16
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.

#include <iostream>

class triple {
private:
// representation
unsigned int a, b;
public:
// constructors
triple(int x = 0, int y = 0): a(x), b(y) { }
// operators
triple& operator=(const triple& t) {
a = t.a; b = t.b;
return *this;
}
friend
std::istream& operator>>(std: :istream& is, triple& t) {
int a = 0;
if (is >> a) {
int b = 0;
if (is >> b) {
t = triple(a, b);
}
}
return is;
}
friend
std::ostream& operator<<(std: :ostream& os, const triple& t) {
return os << t.a << ' ' << t.b;
}
};
Jul 22 '05 #17
"Jason Heyes" <ja********@opt usnet.com.au> wrote...
"Victor Bazarov" <v.********@com Acast.net> wrote in message
Jason Heyes wrote:
No the < , > symbols are not part of the input. Here is some code that
reads two integers and calculates the third:

int a, b;
if (!(is >> a && is >> b))
return is;
int c = (int)sqrt(a*a + b*b);

This is wrong code for the stated requirements.
So, if you know that it's wrong, you must know _what_ is wrong with it,
mustn't you? I mean, besides that it's not really a C++ program...
If do indeed know what is wrong with that code, why don't you fix it

The set of requirements is a bit vague as to what to do if the pair is
not
part of a "pythagorea n triple". Throw an exception? Make up a default
0,0,0 triple?

V

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
Jul 22 '05 #18
"Jason Heyes" <ja********@opt usnet.com.au> wrote...
"Victor Bazarov" <v.********@com Acast.net> wrote in message
Jason Heyes wrote:
[..] This should have been made
clearer in the requirements. 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.
Please don't make me try. Doing things this way is much easier, I
believe.

You mean, it is easier to direct people to do work giving them some vague
requirements and then ask them to do it again when the requirements
change
instead of actually trying to iterate through the implementation process
yourself? Rejoice, brethren! Another marketing manager is born!

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
Jul 22 '05 #19
"E. Robert Tisdale" <E.************ **@jpl.nasa.gov > wrote in message
news:cn******** **@nntp1.jpl.na sa.gov...
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.

#include <iostream>

class triple {
private:
// representation
unsigned int a, b;
public:
// constructors
triple(int x = 0, int y = 0): a(x), b(y) { }
// operators
triple& operator=(const triple& t) {
a = t.a; b = t.b;
return *this;
}
friend
std::istream& operator>>(std: :istream& is, triple& t) {
int a = 0;
if (is >> a) {
int b = 0;
if (is >> b) {
t = triple(a, b);
}
}
return is;
}
friend
std::ostream& operator<<(std: :ostream& os, const triple& t) {
return os << t.a << ' ' << t.b;
}
};

Thanks for the input.
Jul 22 '05 #20

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