473,890 Members | 1,761 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
"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:6pymd.6198 98$8_6.573199@a ttbi_s04...
"Jason Heyes" <ja********@opt usnet.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:

PythagoreanTrip le triple;
while (cin >> triple)
cout << "PythagoreanTri ple <" << 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 #21
"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:RHymd.6199 56$8_6.90787@at tbi_s04...
"Jason Heyes" <ja********@opt usnet.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********@opt usnet.com.au> wrote in message
news:41******** **************@ news.optusnet.c om.au...
"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:6pymd.6198 98$8_6.573199@a ttbi_s04...
"Jason Heyes" <ja********@opt usnet.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:

PythagoreanTrip le triple;
while (cin >> triple)
cout << "PythagoreanTri ple <" << 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 PythagoreanTrip le
{
public:
PythagoreanTrip le(int a, int b, int c);
};

istream& operator>>(istr eam& in, PythagoreanTrip le& t)
{
int a, b;
if (!(is >> a && is >> b))
return is;
double c = (int)sqrt((doub le)a*a + (double)b*b);
if (c != floor(c))
{
is.setstate(ios _base::fail);
return is;
}
t = PythagoreanTrip le(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 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.


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******@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.


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:

PythagoreanTrip le triple;
while (cin >> triple)
cout << "PythagoreanTri ple <" << 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 PythagoreanTrip le
{
public:
PythagoreanTrip le(int a, int b, int c);
};

istream& operator>>(istr eam& in, PythagoreanTrip le& t)
{
int a, b;
if (!(is >> a && is >> b))
return is;
double c = (int)sqrt((doub le)a*a + (double)b*b);
if (c != floor(c))
{
is.setstate(ios _base::fail);
return is;
}
t = PythagoreanTrip le(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********@opt usnet.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 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.
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 PythagoreanTrip le 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 PythagoreanTrip le?
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 PythagoreanTrip leFactory::crea te 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 PythagoreanTrip le 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 PythagoreanTrip le? 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 PythagoreanTrip leFactory::crea te 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

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
11215
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
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...
1
10908
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
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?
1
4665
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.