Creating pythagorean triples from input.

J

Jason Heyes

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

Cy Edmunds

Jason Heyes said:
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.
 
J

Jason Heyes

Cy Edmunds said:
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.

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

John Harrison

Jason Heyes said:
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
 
K

Karl Heinz Buchegger

Jason said:
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.
 
M

Michiel Salters

Jason Heyes said:
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.
 
H

Howard

Jason Heyes said:
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
 
L

Larry Brasfield

Jason Heyes said:
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-lite/how-to-post.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.
 
J

Jason Heyes

Michiel Salters said:
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.
 
J

Jason Heyes

John Harrison said:
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.
 
V

Victor Bazarov

Jason said:
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
 
J

Jason Heyes

Karl Heinz Buchegger said:
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.

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

Victor Bazarov

Jason said:
[..] 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!
 
J

Jason Heyes

Larry Brasfield said:
Your request shows up here often enough to have become a FAQ.
See http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.2


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

Jason Heyes

Victor Bazarov said:
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.
 
J

Jason Heyes

Victor Bazarov said:
Jason said:
[..] 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?
 
E

E. Robert Tisdale

Jason said:
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::eek:stream& operator<<(std::eek:stream& os, const triple& t) {
return os << t.a << ' ' << t.b;
}
};
 
V

Victor Bazarov

Jason Heyes said:
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
 
V

Victor Bazarov

Jason Heyes said:
Victor Bazarov said:
Jason said:
[..] 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
 
J

Jason Heyes

Victor Bazarov said:
Well who else should know? You started the thread.


Better than you might think...


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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top