473,890 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Creating pythagorean triples from input.

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

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

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

Can anyone write me some good working code that implements this
functionality? This is not for a school project in case you're wondering.
Thanks.
Jul 22 '05
37 3429
Jason Heyes wrote:
"John Harrison" <jo************ *@hotmail.com> wrote in message
news:2v******** *****@uni-berlin.de...
"Jason Heyes" <ja********@opt usnet.com.au> wrote in message
news:41****** *************** **@news.optusne t.com.au...
A pythagorean triple is a triple <a,b,c> whose components are positive
integers satisfying a*a + b*b = c*c. An example is <3,4,5> since 3*3 + 4*4
= 9 + 16 = 25 = 5*5.

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

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

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


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

john

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

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

This is wrong code for the stated requirements.


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

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

V
Jul 22 '05 #11
"Karl Heinz Buchegger" <kb******@gasca d.at> wrote in message
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, PythagoreanTrip le &triple);

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


What I am wondering is: What is your *exact* problem?
The task sounds easy enough. Or not so easy, depending on what you
are willing to invest in syntax parsing.

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

--
Karl Heinz Buchegger
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************ ***********@hot mail.com> wrote in message
news:4W******** ********@news.u swest.net...
"Jason Heyes" <ja********@opt usnet.com.au> wrote in message
news:41******** *************** @news.optusnet. com.au...
A pythagorean triple is a triple <a,b,c> whose components are positive
integers satisfying a*a + b*b = c*c. An example is <3,4,5> since 3*3 + 4*4
= 9 + 16 = 25 = 5*5.

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

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

Can anyone write me some good working code that implements this
functionality?


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


I would help if you could cogently explain why anybody not a student
would need to solve this trivial problem yet require help to do so.
Thanks.

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


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

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

These are questions I could answer with some good code - if only I had some.
Jul 22 '05 #14
"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:kI******** *********@newsr ead1.dllstx09.u s.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 "pythagorea n triple". Throw an exception? Make up a default
0,0,0 triple?

V


Well how would I know? I'm the one asking for help remember? This was the
whole point of the exercise. You write code that solves the problem so I can
see how its done.
Jul 22 '05 #15
"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:OT******** *********@newsr ead1.dllstx09.u s.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, PythagoreanTrip le &triple);

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


#include <iostream>

class triple {
private:
// representation
unsigned int a, b;
public:
// constructors
triple(int x = 0, int y = 0): a(x), b(y) { }
// operators
triple& operator=(const triple& t) {
a = t.a; b = t.b;
return *this;
}
friend
std::istream& operator>>(std: :istream& is, triple& t) {
int a = 0;
if (is >> a) {
int b = 0;
if (is >> b) {
t = triple(a, b);
}
}
return is;
}
friend
std::ostream& operator<<(std: :ostream& os, const triple& t) {
return os << t.a << ' ' << t.b;
}
};
Jul 22 '05 #17
"Jason Heyes" <ja********@opt usnet.com.au> wrote...
"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:kI******** *********@newsr ead1.dllstx09.u s.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 "pythagorea n triple". Throw an exception? Make up a default
0,0,0 triple?

V


Well how would I know?


Well who else should know? You started the thread.
I'm the one asking for help remember?
Better than you might think...
This was the whole point of the exercise. You write code that solves the
problem so I can see how its done.


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

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

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

V
Jul 22 '05 #18
"Jason Heyes" <ja********@opt usnet.com.au> wrote...
"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:OT******** *********@newsr ead1.dllstx09.u s.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
"E. Robert Tisdale" <E.************ **@jpl.nasa.gov > wrote in message
news:cn******** **@nntp1.jpl.na sa.gov...
Jason Heyes wrote:
A pythagorean triple is a triple <a,b,c> whose components are positive
integers satisfying a*a + b*b = c*c. An example is <3,4,5> since 3*3 +
4*4 = 9 + 16 = 25 = 5*5.

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

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

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


#include <iostream>

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


Thanks for the input.
Jul 22 '05 #20

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

Similar topics

6
2040
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 to try to make something all be meself. I have it working great to find the hypotenuse, but am having some dufficulty making it produce a missing leg. As you know, a^2 + b^2 = c^2 I have a variable that does the input for the one of the legs...
3
1679
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; and that the official way to serialize the graph is to use the RDF/XML specification. So far so good; but N3 and/or N-Triple notations are also used, and it sure seems to me that N3 is "iso-morphic" to the graph; i.e. The triples (properly...
11
4314
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 listbox. I have done this so far: 'Allocate some local variables Dim LegA As Integer Dim LegB As Integer Dim Hyp As Integer Dim Triple As String Dim Found As Integer
5
4726
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 the relationship that the sum of the squares of two of the sides is equal to the square of the hypotenuse. Find all Pythagorean triples for side1, side2, and the hypotenuse that fall within a user-specified range. Limit the upper-bound to 500. Use a...
11
15074
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 set of three integer values for the sides of a right triangle is called a Pythagorean triple. These three sides must satisfy the following relationship: (side1)^2 + (side2)^2 = (hypotenuse)^2 Output all Pythagorean...
12
7432
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: Pythagorean Theorem // Written by: Hellbreaker // // #include <iostream> #include <cmath> using namespace std;
0
9976
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10802
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10451
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9618
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8008
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7160
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6035
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4259
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3267
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.