Creating pythagorean triples from input. 
July 22nd, 2005, 09:55 PM
| | | 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. | 
July 22nd, 2005, 09:55 PM
| | | Re: Creating pythagorean triples from input.
"Jason Heyes" <jasonheyes@optusnet.com.au> wrote in message
news:41997f54$0$27449$afc38c87@news.optusnet.com.a u...[color=blue]
>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.
>[/color]
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/ | 
July 22nd, 2005, 09:55 PM
| | | Re: Creating pythagorean triples from input.
"Cy Edmunds" <cedmunds@spamless.rochester.rr.com> wrote in message
news:vofmd.9822$zk7.8862@twister.nyroc.rr.com...[color=blue]
> "Jason Heyes" <jasonheyes@optusnet.com.au> wrote in message
> news:41997f54$0$27449$afc38c87@news.optusnet.com.a u...[color=green]
>>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.
>>[/color]
>
> 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/[/color]
I wrote the requirements myself I'll have you know. | 
July 22nd, 2005, 09:55 PM
| | | Re: Creating pythagorean triples from input.
"Jason Heyes" <jasonheyes@optusnet.com.au> wrote in message
news:41997f54$0$27449$afc38c87@news.optusnet.com.a u...[color=blue]
>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.[/color]
Is all that < , > part of your input? If not it sounds very easy, just read
two integers and calculate the third.
john | 
July 22nd, 2005, 09:56 PM
| | | Re: Creating pythagorean triples from input.
Jason Heyes wrote:[color=blue]
>
> 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.[/color]
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 kbuchegg@gascad.at | 
July 22nd, 2005, 09:56 PM
| | | Re: Creating pythagorean triples from input.
"Jason Heyes" <jasonheyes@optusnet.com.au> wrote in message news:<41999f3c$0$1724$afc38c87@news.optusnet.com.a u>...[color=blue]
> "Cy Edmunds" <cedmunds@spamless.rochester.rr.com> wrote in message
> news:vofmd.9822$zk7.8862@twister.nyroc.rr.com...[color=green]
> > "Jason Heyes" <jasonheyes@optusnet.com.au> wrote in message
> > news:41997f54$0$27449$afc38c87@news.optusnet.com.a u...[/color][/color]
[color=blue][color=green][color=darkred]
> >> 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.
> >>[/color]
> >
> > Requests for coding from requirements are off topic for this newsgroup.[/color]
>
> I wrote the requirements myself I'll have you know.[/color]
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. | 
July 22nd, 2005, 09:56 PM
| | | Re: Creating pythagorean triples from input.
"Jason Heyes" <jasonheyes@optusnet.com.au> wrote in message
news:41997f54$0$27449$afc38c87@news.optusnet.com.a u...[color=blue]
>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.
>[/color]
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 | 
July 22nd, 2005, 09:57 PM
| | | Re: [FAQ] Creating pythagorean triples from input.
"Jason Heyes" <jasonheyes@optusnet.com.au> wrote in message news:41997f54$0$27449$afc38c87@news.optusnet.com.a u...[color=blue]
>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?[/color]
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
[color=blue]
> This is not for a school project in case you're wondering.[/color]
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.
[color=blue]
> Thanks.[/color]
--
--Larry Brasfield
email: donotspam_larry_brasfield@hotmail.com
Above views may belong only to me. | 
July 22nd, 2005, 09:58 PM
| | | Re: Creating pythagorean triples from input.
"Michiel Salters" <Michiel.Salters@logicacmg.com> wrote in message
news:fcaee77e.0411160452.262cfe1e@posting.google.c om...[color=blue]
> "Jason Heyes" <jasonheyes@optusnet.com.au> wrote in message
> news:<41999f3c$0$1724$afc38c87@news.optusnet.com.a u>...[color=green]
>> I wrote the requirements myself I'll have you know.[/color]
>
> 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.[/color]
Don't talk about what I can or can't do. Its insulting. | 
July 22nd, 2005, 09:58 PM
| | | Re: Creating pythagorean triples from input.
"John Harrison" <john_andronicus@hotmail.com> wrote in message
news:2vtqktF2nun0bU1@uni-berlin.de...[color=blue]
>
> "Jason Heyes" <jasonheyes@optusnet.com.au> wrote in message
> news:41997f54$0$27449$afc38c87@news.optusnet.com.a u...[color=green]
>>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.[/color]
>
> Is all that < , > part of your input? If not it sounds very easy, just
> read two integers and calculate the third.
>
> john[/color]
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. | 
July 22nd, 2005, 09:58 PM
| | | Re: Creating pythagorean triples from input.
Jason Heyes wrote:[color=blue]
> "John Harrison" <john_andronicus@hotmail.com> wrote in message
> news:2vtqktF2nun0bU1@uni-berlin.de...
>[color=green]
>>"Jason Heyes" <jasonheyes@optusnet.com.au> wrote in message
>>news:41997f54$0$27449$afc38c87@news.optusnet.com .au...
>>[color=darkred]
>>>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.[/color]
>>
>>Is all that < , > part of your input? If not it sounds very easy, just
>>read two integers and calculate the third.
>>
>>john[/color]
>
>
> 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.[/color]
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 | 
July 22nd, 2005, 09:58 PM
| | | Re: Creating pythagorean triples from input.
"Karl Heinz Buchegger" <kbuchegg@gascad.at> wrote in message
news:4199C45A.A8156BEE@gascad.at...[color=blue]
> Jason Heyes wrote:[color=green]
>>
>> 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.[/color]
>
> 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
> kbuchegg@gascad.at[/color]
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. | 
July 22nd, 2005, 09:58 PM
| | | Re: Creating pythagorean triples from input.
Jason Heyes wrote:[color=blue]
> [..] 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.[/color]
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! | 
July 22nd, 2005, 09:58 PM
| | | Re: [FAQ] Creating pythagorean triples from input.
"Larry Brasfield" <donotspam_larry_brasfield@hotmail.com> wrote in message
news:4Wqmd.11$do4.30995@news.uswest.net...[color=blue]
> "Jason Heyes" <jasonheyes@optusnet.com.au> wrote in message
> news:41997f54$0$27449$afc38c87@news.optusnet.com.a u...[color=green]
>>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?[/color]
>
> 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
>[color=green]
>> This is not for a school project in case you're wondering.[/color]
>
> 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.
>[color=green]
>> Thanks.[/color]
> --
> --Larry Brasfield
> email: donotspam_larry_brasfield@hotmail.com
> Above views may belong only to me.[/color]
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. | 
July 22nd, 2005, 09:58 PM
| | | Re: Creating pythagorean triples from input.
"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:kIwmd.11030$Ae.2466@newsread1.dllstx09.us.to. verio.net...[color=blue]
> Jason Heyes wrote:[color=green]
>> 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.[/color]
>
> 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[/color]
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. | 
July 22nd, 2005, 09:58 PM
| | | Re: Creating pythagorean triples from input.
"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:OTwmd.11031$Ae.7408@newsread1.dllstx09.us.to. verio.net...[color=blue]
> Jason Heyes wrote:[color=green]
>> [..] 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.[/color]
>
> 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![/color]
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? | 
July 22nd, 2005, 09:58 PM
| | | Re: Creating pythagorean triples from input.
Jason Heyes wrote:
[color=blue]
> 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.[/color]
#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;
}
}; | 
July 22nd, 2005, 09:58 PM
| | | Re: Creating pythagorean triples from input.
"Jason Heyes" <jasonheyes@optusnet.com.au> wrote...[color=blue]
> "Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
> news:kIwmd.11030$Ae.2466@newsread1.dllstx09.us.to. verio.net...[color=green]
>> Jason Heyes wrote:[color=darkred]
>>> 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.[/color]
>>
>> 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[/color]
>
> Well how would I know?[/color]
Well who else should know? You started the thread.
[color=blue]
> I'm the one asking for help remember?[/color]
Better than you might think...
[color=blue]
> This was the whole point of the exercise. You write code that solves the
> problem so I can see how its done.[/color]
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 | 
July 22nd, 2005, 09:58 PM
| | | Re: Creating pythagorean triples from input.
"Jason Heyes" <jasonheyes@optusnet.com.au> wrote...[color=blue]
> "Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
> news:OTwmd.11031$Ae.7408@newsread1.dllstx09.us.to. verio.net...[color=green]
>> Jason Heyes wrote:[color=darkred]
>>> [..] 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.[/color]
>>
>> 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![/color]
>
> 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?[/color]
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 | 
July 22nd, 2005, 09:58 PM
| | | Re: Creating pythagorean triples from input.
"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:6pymd.619898$8_6.573199@attbi_s04...[color=blue]
> "Jason Heyes" <jasonheyes@optusnet.com.au> wrote...[color=green]
>> Well how would I know?[/color]
>
> Well who else should know? You started the thread.
>[color=green]
>> I'm the one asking for help remember?[/color]
>
> Better than you might think...
>[color=green]
>> This was the whole point of the exercise. You write code that solves the
>> problem so I can see how its done.[/color]
>
> 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[/color]
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. | 
July 22nd, 2005, 09:58 PM
| | | Re: Creating pythagorean triples from input.
"E. Robert Tisdale" <E.Robert.Tisdale@jpl.nasa.gov> wrote in message
news:cne8e8$so2$1@nntp1.jpl.nasa.gov...[color=blue]
> Jason Heyes wrote:
>[color=green]
>> 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.[/color]
>
> #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;
> }
> };[/color]
Thanks for the input. | 
July 22nd, 2005, 09:58 PM
| | | Re: Creating pythagorean triples from input.
"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:RHymd.619956$8_6.90787@attbi_s04...[color=blue]
> "Jason Heyes" <jasonheyes@optusnet.com.au> wrote...[color=green]
>> 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?[/color]
>
> 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[/color]
This is the last time I ask people to write code to requirements in this
forum. It just gets too personal. | 
July 22nd, 2005, 09:59 PM
| | | Re: Creating pythagorean triples from input.
"Jason Heyes" <jasonheyes@optusnet.com.au> wrote in message
news:419afb1c$0$7560$afc38c87@news.optusnet.com.au ...[color=blue]
> "Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
> news:6pymd.619898$8_6.573199@attbi_s04...[color=green]
>> "Jason Heyes" <jasonheyes@optusnet.com.au> wrote...[color=darkred]
>>> Well how would I know?[/color]
>>
>> Well who else should know? You started the thread.
>>[color=darkred]
>>> I'm the one asking for help remember?[/color]
>>
>> Better than you might think...
>>[color=darkred]
>>> This was the whole point of the exercise. You write code that solves the
>>> problem so I can see how its done.[/color]
>>
>> 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[/color]
>
> 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.[/color]
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 | 
July 22nd, 2005, 09:59 PM
| | | Re: [FAQ] Creating pythagorean triples from input.
Jason Heyes wrote:[color=blue]
>[color=green]
> > 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.
> >[/color]
>
> 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.[/color]
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 kbuchegg@gascad.at | 
July 22nd, 2005, 09:59 PM
| | | Re: Creating pythagorean triples from input.
Jason Heyes wrote:[color=blue]
>
> "Karl Heinz Buchegger" <kbuchegg@gascad.at> wrote in message
> news:4199C45A.A8156BEE@gascad.at...[color=green]
> > Jason Heyes wrote:[color=darkred]
> >>
> >> 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.[/color]
> >
> > 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
> > kbuchegg@gascad.at[/color]
>
> The task has nothing to do with syntax parsing. This should have been made
> clearer in the requirements.[/color]
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.
[color=blue]
> 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.[/color]
If you cannot express your problem, how should we know what to
answer to help you.
[color=blue]
> Doing things this way is much easier, I believe.[/color]
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 kbuchegg@gascad.at | 
July 22nd, 2005, 09:59 PM
| | | Re: Creating pythagorean triples from input.
Jason Heyes wrote:[color=blue]
>
> 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?[/color]
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.
[color=blue]
> 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.[/color]
In your mind only.
Dream on.
Good luck to you.
--
Karl Heinz Buchegger kbuchegg@gascad.at | 
July 22nd, 2005, 09:59 PM
| | | Re: Creating pythagorean triples from input.
"John Harrison" <john_andronicus@hotmail.com> wrote in message
news:300gf2F2r6n1dU1@uni-berlin.de...[color=blue]
> 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[/color]
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. | 
July 22nd, 2005, 09:59 PM
| | | Re: Creating pythagorean triples from input.
>[color=blue]
> 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.[/color]
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 | 
July 22nd, 2005, 09:59 PM
| | | Re: [FAQ] Creating pythagorean triples from input.
"Jason Heyes" <jasonheyes@optusnet.com.au> wrote in message news:<419a9da1$0$24380$afc38c87@news.optusnet.com. au>...
[color=blue]
> 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.[/color]
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.
[color=blue]
> * How should I design the PythagoreanTriple class?[/color]
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.
[color=blue]
> * Should I have a factory class for PythagoreanTriple?[/color]
No class = no factory
[color=blue]
> * Is it the role of operator>> to verify its inputs?[/color]
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.
[color=blue]
> * Should PythagoreanTripleFactory::create be written so that it returns
> boolean?[/color]
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 | 
July 22nd, 2005, 09:59 PM
| | | Re: [FAQ] Creating pythagorean triples from input.
> * 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.[color=blue]
> * Should I have a factory class for PythagoreanTriple?[/color]
That is a design choice and it seems you've already made it seeing
your question 4.
[color=blue]
> * Is it the role of operator>> to verify its inputs?[/color]
Depends. This is one of those things you need to figure out as said in
the first point.[color=blue]
> * Should PythagoreanTripleFactory::create be written so that it returns boolean?[/color]
Depends again on your needs.
[color=blue]
> These are questions I could answer with some good code - if only I had some.[/color]
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. | 
July 22nd, 2005, 09:59 PM
| | | Re: Creating pythagorean triples from input.
"Karl Heinz Buchegger" <kbuchegg@gascad.at> wrote in message
news:419B1574.ED5F033A@gascad.at...[color=blue]
> Jason Heyes wrote:[color=green]
>>
>> 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?[/color]
>
> 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.[/color]
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? | 
July 22nd, 2005, 09:59 PM
| | | Re: Creating pythagorean triples from input.
"John Harrison" <john_andronicus@hotmail.com> wrote in message
news:300qapF2orkfnU1@uni-berlin.de...[color=blue][color=green]
> >
>> 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.[/color]
>
> 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[/color]
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. | 
July 22nd, 2005, 09:59 PM
| | | Re: Creating pythagorean triples from input.
"Karl Heinz Buchegger" <kbuchegg@gascad.at> wrote in message
news:419B12E2.1FAA4DBB@gascad.at...[color=blue]
> Jason Heyes wrote:[color=green]
>>
>> "Karl Heinz Buchegger" <kbuchegg@gascad.at> wrote in message
>> news:4199C45A.A8156BEE@gascad.at...[color=darkred]
>> > 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
>> > kbuchegg@gascad.at[/color]
>>
>> The task has nothing to do with syntax parsing. This should have been
>> made
>> clearer in the requirements.[/color]
>
> 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.
>[color=green]
>> 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.[/color]
>
> If you cannot express your problem, how should we know what to
> answer to help you.
>[color=green]
>> Doing things this way is much easier, I believe.[/color]
>
> 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
> kbuchegg@gascad.at[/color]
That assumes everyone who answers questions in this forum is perfect. | 
July 22nd, 2005, 10:00 PM
| | | Re: Creating pythagorean triples from input.
"Jason Heyes" <jasonheyes@optusnet.com.au> wrote in message
news:419afd29$0$2676
[color=blue]
> This is the last time I ask people to write code to requirements in this
> forum.[/color]
I think I speak for everyone here when I say: GOOD!
-Howard | 
July 22nd, 2005, 10:00 PM
| | | Re: Creating pythagorean triples from input.
Howard wrote:[color=blue]
> "Jason Heyes" <jasonheyes@optusnet.com.au> wrote in message
> news:419afd29$0$2676
>
>[color=green]
>>This is the last time I ask people to write code to requirements in this
>>forum.[/color]
>
>
>
> I think I speak for everyone here when I say: GOOD![/color]
Amen! | 
July 22nd, 2005, 10:00 PM
| | | Re: Creating pythagorean triples from input.
"Jason Heyes" <jasonheyes@optusnet.com.au> wrote in message
news:419b534b$0$2681$afc38c87@news.optusnet.com.au ...[color=blue]
> "John Harrison" <john_andronicus@hotmail.com> wrote in message
> news:300qapF2orkfnU1@uni-berlin.de...[color=green][color=darkred]
>> >
>>> 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.[/color]
>>
>> 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[/color]
>
> 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.
>[/color]
OK, use regular ints then would be my advice. I don't like unsigned types.
john | 
July 22nd, 2005, 10:00 PM
| | | Re: Creating pythagorean triples from input.
Victor Bazarov wrote:[color=blue]
> Howard wrote:
>[color=green]
>> "Jason Heyes" <jasonheyes@optusnet.com.au> wrote in message
>> news:419afd29$0$2676
>>
>>[color=darkred]
>>> This is the last time I ask people to write code to requirements in
>>> this forum.[/color]
>>
>>
>>
>>
>> I think I speak for everyone here when I say: GOOD![/color]
>
>
> Amen![/color]
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 | 
July 22nd, 2005, 10:01 PM
| | | Re: Creating pythagorean triples from input.
"John Harrison" <john_andronicus@hotmail.com> wrote in message
news:301cuvF2om2gjU1@uni-berlin.de...[color=blue]
>
> "Jason Heyes" <jasonheyes@optusnet.com.au> wrote in message
> news:419b534b$0$2681$afc38c87@news.optusnet.com.au ...[color=green]
>> "John Harrison" <john_andronicus@hotmail.com> wrote in message
>> news:300qapF2orkfnU1@uni-berlin.de...[color=darkred]
>>> >
>>>> 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[/color]
>>
>> 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.
>>[/color]
>
> OK, use regular ints then would be my advice. I don't like unsigned types.
>
> john[/color]
LOL me neither. | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 220,840 network members.
|