473,408 Members | 2,832 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,408 software developers and data experts.

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, PythagoreanTriple &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 #1
37 3348
"Jason Heyes" <ja********@optusnet.com.au> wrote in message
news:41***********************@news.optusnet.com.a u...
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, PythagoreanTriple &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.


No? Well it sure sounds like it.

Not that it matters. Requests for coding from requirements are off topic for
this newsgroup. Take a crack at writing your function and if it doesn't work
post what you have.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #2
"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message
news:vo*****************@twister.nyroc.rr.com...
"Jason Heyes" <ja********@optusnet.com.au> wrote in message
news:41***********************@news.optusnet.com.a u...
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, PythagoreanTriple &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.


No? Well it sure sounds like it.

Not that it matters. Requests for coding from requirements are off topic
for this newsgroup. Take a crack at writing your function and if it
doesn't work post what you have.

--
Cy
http://home.rochester.rr.com/cyhome/


I wrote the requirements myself I'll have you know.
Jul 22 '05 #3

"Jason Heyes" <ja********@optusnet.com.au> wrote in message
news:41***********************@news.optusnet.com.a u...
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, PythagoreanTriple &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.


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

john
Jul 22 '05 #4
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, PythagoreanTriple &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
kb******@gascad.at
Jul 22 '05 #5
"Jason Heyes" <ja********@optusnet.com.au> wrote in message news:<41**********************@news.optusnet.com.a u>...
"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message
news:vo*****************@twister.nyroc.rr.com...
"Jason Heyes" <ja********@optusnet.com.au> wrote in message
news:41***********************@news.optusnet.com.a u...
I want to write a function to extract pythagorean triples from an input
stream.
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.


Requests for coding from requirements are off topic for this newsgroup.


I wrote the requirements myself I'll have you know.


So? RTFF. Requests for coding from requirements are off topic, and
there's no exception in there for the case where you wrote them.

Besides, it's not like we believe you. You can't write the requirements
in that detail if you can't even try to come up with a first attempt
at implementing them.

Regards,
Michiel Salters.
Jul 22 '05 #6

"Jason Heyes" <ja********@optusnet.com.au> wrote in message
news:41***********************@news.optusnet.com.a u...
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, PythagoreanTriple &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.


You should be able to write it yourself. Are you asking for an algorithm?
Well, since a*a+b*b=c*c, try computing the square root of a*a+b*b. If the
result of that is the same as the floor() of that result, then you've got a
valid integer result, and a result that satisfies the requirements.
Otherwise you don't.

sqrt(3*3+4*4) = 5.0000... == 5.0 --> 5 satisfies
sqrt(3*3+5*5) = 5.8309... != 5.0 --> nothing satisifies

-Howard

Jul 22 '05 #7
"Jason Heyes" <ja********@optusnet.com.au> wrote in message news:41***********************@news.optusnet.com.a u...
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, PythagoreanTriple &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***********************@hotmail.com
Above views may belong only to me.
Jul 22 '05 #8
"Michiel Salters" <Mi*************@logicacmg.com> wrote in message
news:fc**************************@posting.google.c om...
"Jason Heyes" <ja********@optusnet.com.au> wrote in message
news:<41**********************@news.optusnet.com.a u>...
I wrote the requirements myself I'll have you know.


So? RTFF. Requests for coding from requirements are off topic, and
there's no exception in there for the case where you wrote them.

Besides, it's not like we believe you. You can't write the requirements
in that detail if you can't even try to come up with a first attempt
at implementing them.

Regards,
Michiel Salters.


Don't talk about what I can or can't do. Its insulting.
Jul 22 '05 #9
"John Harrison" <jo*************@hotmail.com> wrote in message
news:2v*************@uni-berlin.de...

"Jason Heyes" <ja********@optusnet.com.au> wrote in message
news:41***********************@news.optusnet.com.a u...
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, PythagoreanTriple &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.


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.
Jul 22 '05 #10
Jason Heyes wrote:
"John Harrison" <jo*************@hotmail.com> wrote in message
news:2v*************@uni-berlin.de...
"Jason Heyes" <ja********@optusnet.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, PythagoreanTriple &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.


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
already?

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

V
Jul 22 '05 #11
"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:41***************@gascad.at...
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, PythagoreanTriple &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
kb******@gascad.at


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***********************@hotmail.com> wrote in message
news:4W****************@news.uswest.net...
"Jason Heyes" <ja********@optusnet.com.au> wrote in message
news:41***********************@news.optusnet.com.a u...
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, PythagoreanTriple &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***********************@hotmail.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 PythagoreanTriple class?
* Should I have a factory class for PythagoreanTriple?
* Is it the role of operator>> to verify its inputs?
* Should PythagoreanTripleFactory::create 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.********@comAcast.net> wrote in message
news:kI*****************@newsread1.dllstx09.us.to. verio.net...
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
already?

The set of requirements is a bit vague as to what to do if the pair is not
part of a "pythagorean 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.********@comAcast.net> wrote in message
news:OT*****************@newsread1.dllstx09.us.to. verio.net...
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, PythagoreanTriple &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********@optusnet.com.au> wrote...
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:kI*****************@newsread1.dllstx09.us.to. verio.net...
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
already?

The set of requirements is a bit vague as to what to do if the pair is
not
part of a "pythagorean 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********@optusnet.com.au> wrote...
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:OT*****************@newsread1.dllstx09.us.to. verio.net...
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
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:6pymd.619898$8_6.573199@attbi_s04...
"Jason Heyes" <ja********@optusnet.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:

PythagoreanTriple triple;
while (cin >> triple)
cout << "PythagoreanTriple <" << 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 #20
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message
news:cn**********@nntp1.jpl.nasa.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, PythagoreanTriple &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 #21
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:RHymd.619956$8_6.90787@attbi_s04...
"Jason Heyes" <ja********@optusnet.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********@optusnet.com.au> wrote in message
news:41**********************@news.optusnet.com.au ...
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:6pymd.619898$8_6.573199@attbi_s04...
"Jason Heyes" <ja********@optusnet.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:

PythagoreanTriple triple;
while (cin >> triple)
cout << "PythagoreanTriple <" << 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
your still imperfectly specified problem

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

istream& operator>>(istream& in, PythagoreanTriple& t)
{
int a, b;
if (!(is >> a && is >> b))
return is;
double c = (int)sqrt((double)a*a + (double)b*b);
if (c != floor(c))
{
is.setstate(ios_base::fail);
return is;
}
t = PythagoreanTriple(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 PythagoreanTriple class?
* Should I have a factory class for PythagoreanTriple?
* Is it the role of operator>> to verify its inputs?
* Should PythagoreanTripleFactory::create 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
kb******@gascad.at
Jul 22 '05 #24
Jason Heyes wrote:

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:41***************@gascad.at...
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, PythagoreanTriple &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
kb******@gascad.at


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


Your requirements talk about:
'an example is <3,4,5>'
So I take it for granted that your input equals "<3,4,5>"
Reading that is definitly parsing.

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.
Please don't make me try.
If you cannot express your problem, how should we know what to
answer to help you.
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
kb******@gascad.at
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
that nobody is going to answer the question you didn't ask.
Here is how the function could be used in a program:

PythagoreanTriple triple;
while (cin >> triple)
cout << "PythagoreanTriple <" << 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.


In your mind only.
Dream on.

Good luck to you.

--
Karl Heinz Buchegger
kb******@gascad.at
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
your still imperfectly specified problem

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

istream& operator>>(istream& in, PythagoreanTriple& t)
{
int a, b;
if (!(is >> a && is >> b))
return is;
double c = (int)sqrt((double)a*a + (double)b*b);
if (c != floor(c))
{
is.setstate(ios_base::fail);
return is;
}
t = PythagoreanTriple(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********@optusnet.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 PythagoreanTriple class?
* Should I have a factory class for PythagoreanTriple?
* Is it the role of operator>> to verify its inputs?
* Should PythagoreanTripleFactory::create 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 PythagoreanTriple 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 PythagoreanTriple?
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 PythagoreanTripleFactory::create 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 PythagoreanTriple 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 PythagoreanTriple? That is a design choice and it seems you've already made it seeing
your question 4.
* 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 PythagoreanTripleFactory::create 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
"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:41***************@gascad.at...
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
that nobody is going to answer the question you didn't ask.


I didn't ask the question you wanted me to ask. This is why you repeatedly
probe me about my "real" problem. My "real" problem has always been to write
good code that achieves the required functionality. But you don't want to
help me do that. You would rather discuss specific problems whose solutions
are known to you.

The truth is that I don't know what my problem boils down to. So when you
ask for the real problem, all I can do is point back to the requirements.
This is done so that you may help me discover the real problem. When the
real problem is found, the solutions are obvious. Understand now?
Jul 22 '05 #31
"John Harrison" <jo*************@hotmail.com> wrote in message
news:30*************@uni-berlin.de...

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


The components of a pythagorean triple must be positive. This is why tests
for a and b are necessary. Even if you use unsigned int, tests for a and b
are still necessary. You don't want <0,0,0> to count as a pythagorean
triple.

The requirements don't talk about numerical issues. Take this as permission
to ignore overflow.

Jul 22 '05 #32
"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:41***************@gascad.at...
Jason Heyes wrote:

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:41***************@gascad.at...
> 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, PythagoreanTriple &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
> kb******@gascad.at


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


Your requirements talk about:
'an example is <3,4,5>'
So I take it for granted that your input equals "<3,4,5>"
Reading that is definitly parsing.

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.
Please don't make me try.


If you cannot express your problem, how should we know what to
answer to help you.
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
kb******@gascad.at


That assumes everyone who answers questions in this forum is perfect.
Jul 22 '05 #33

"Jason Heyes" <ja********@optusnet.com.au> wrote in message
news:419afd29$0$2676
This is the last time I ask people to write code to requirements in this
forum.

I think I speak for everyone here when I say: GOOD!

-Howard
Jul 22 '05 #34
Howard wrote:
"Jason Heyes" <ja********@optusnet.com.au> wrote in message
news:419afd29$0$2676

This is the last time I ask people to write code to requirements in this
forum.


I think I speak for everyone here when I say: GOOD!


Amen!
Jul 22 '05 #35

"Jason Heyes" <ja********@optusnet.com.au> wrote in message
news:41**********************@news.optusnet.com.au ...
"John Harrison" <jo*************@hotmail.com> wrote in message
news:30*************@uni-berlin.de...
>
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


The components of a pythagorean triple must be positive. This is why tests
for a and b are necessary. Even if you use unsigned int, tests for a and b
are still necessary. You don't want <0,0,0> to count as a pythagorean
triple.

The requirements don't talk about numerical issues. Take this as
permission to ignore overflow.


OK, use regular ints then would be my advice. I don't like unsigned types.

john
Jul 22 '05 #36
Victor Bazarov wrote:
Howard wrote:
"Jason Heyes" <ja********@optusnet.com.au> wrote in message
news:419afd29$0$2676

This is the last time I ask people to write code to requirements in
this forum.



I think I speak for everyone here when I say: GOOD!

Amen!


The more I read this thread, the more it appears it was a troll. And
from the amount of replies and flames it generated, looks like a heck of
a brilliant one. :)

-Arijit
Jul 22 '05 #37
"John Harrison" <jo*************@hotmail.com> wrote in message
news:30*************@uni-berlin.de...

"Jason Heyes" <ja********@optusnet.com.au> wrote in message
news:41**********************@news.optusnet.com.au ...
"John Harrison" <jo*************@hotmail.com> wrote in message
news:30*************@uni-berlin.de...
>
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


The components of a pythagorean triple must be positive. This is why
tests for a and b are necessary. Even if you use unsigned int, tests for
a and b are still necessary. You don't want <0,0,0> to count as a
pythagorean triple.

The requirements don't talk about numerical issues. Take this as
permission to ignore overflow.


OK, use regular ints then would be my advice. I don't like unsigned types.

john


LOL me neither.
Jul 22 '05 #38

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

Similar topics

6
by: 3than7 | last post by:
I am writing an application to solve Pythagorean Theorum Problems. This is on my own time, i am using a book to learn c++, and after doing a fahrenheit to celsuis program from that book, i wanted...
3
by: Leeh | last post by:
I'm new to the world of RDF and RDF/XML so pardon my naive question: I understand that the "real" RDF model is the conceptual network of nodes (Subjects and Objects) connected by predicate arcs;...
11
by: corwood | last post by:
I am in a VB .NET class, and one of the assignments is to use loops to generate a list of all the pythagorean triples where legA and legB <100 and hypotenuse < 200, and then put this list into a...
5
by: stephanieanne2 | last post by:
The Problem: A right triangle can have sides that are all integers. The set of three integer values for the sides of a right triangle is called a Pythagorean triple. These three sides must satisfy...
11
by: inferi9 | last post by:
hi everyone I am new here and I have this C++ program that I have to write but it keep given me nothing useful. here is the question: A right triangle can have sides that are all integers. A...
12
by: abkierstein | last post by:
This is my 1st program and I need some help. I've almost got this one finished but I don't know where to go from here. There is something wrong with the sides I've assigned. Any tips? // Program:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...
0
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...

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.