473,837 Members | 1,548 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 3417
"Karl Heinz Buchegger" <kb******@gasca d.at> wrote in message
news:41******** *******@gascad. at...
Jason Heyes wrote:

Ok. The requirements say to write a function that reads pythagorean
triples.
So the appropriate action when a pythagorean triple is not specified in
the
input is to fail. Its just the same as when an integer isn't specified in
the input and operator>>(std: :istream &, int &) fails. Why is this not
obvious?


You are funny, you know?

You start a thread which asks us to write code for something that looks
a lot like homework. Only after several answers it turns out that your
real problem is located in a very different section:

How to proceed if an input operator detects invalid input.

With no word have you addressed your real problem in the original
posting. You could eg. have asked

How do I proceed in in stream input operator if I detect an invalid
input? I know that eg reading an int may fail. How do I do the same
for my class? How do I force the stream to an invalid state? Or
are there better ways? What about throwing an exception?
Any thoughts welcome.

Instead you teach us a lot about phtagorean triplets which are really
unimportant to your real question. You would the very same problem
when you read eg. a date and get 30-feb-2004 as input.

To summarize: You don't ask the question you should ask but complain
that nobody is going to answer the question you didn't ask.


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

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

This code achieves almost all of the required functionality so thats
great. As an improvement I would suggest adding tests for integers a and
b so that <-3,-4,5> is not mistaken for a pythagorean triple. But thats a
minor issue. Well done.


Well gee, thank you very much.

I would be worried about the overflow that could occur when converting
from a double to an int.

You could consider using unsigned int instead of int if you don't
consider -3, -4 and 5 a Pythagorean triple.

john


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

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

Jul 22 '05 #32
"Karl Heinz Buchegger" <kb******@gasca d.at> wrote in message
news:41******** *******@gascad. at...
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


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

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

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

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

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


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


Amen!
Jul 22 '05 #35

"Jason Heyes" <ja********@opt usnet.com.au> wrote in message
news:41******** **************@ news.optusnet.c om.au...
"John Harrison" <jo************ *@hotmail.com> wrote in message
news:30******** *****@uni-berlin.de...
>
This code achieves almost all of the required functionality so thats
great. As an improvement I would suggest adding tests for integers a and
b so that <-3,-4,5> is not mistaken for a pythagorean triple. But thats
a minor issue. Well done.


Well gee, thank you very much.

I would be worried about the overflow that could occur when converting
from a double to an int.

You could consider using unsigned int instead of int if you don't
consider -3, -4 and 5 a Pythagorean triple.

john


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

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


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

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

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



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

Amen!


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

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

"Jason Heyes" <ja********@opt usnet.com.au> wrote in message
news:41******** **************@ news.optusnet.c om.au...
"John Harrison" <jo************ *@hotmail.com> wrote in message
news:30******** *****@uni-berlin.de...
>
This code achieves almost all of the required functionality so thats
great. As an improvement I would suggest adding tests for integers a
and b so that <-3,-4,5> is not mistaken for a pythagorean triple. But
thats a minor issue. Well done.

Well gee, thank you very much.

I would be worried about the overflow that could occur when converting
from a double to an int.

You could consider using unsigned int instead of int if you don't
consider -3, -4 and 5 a Pythagorean triple.

john


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

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


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

john


LOL me neither.
Jul 22 '05 #38

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

Similar topics

6
2038
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
1677
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
4308
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
4719
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
15069
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
7430
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
9837
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
9682
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10563
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
10621
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
9392
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...
0
6998
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
5668
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4474
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
2
4038
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.