-
September 17th, 2009, 06:31 PM
#1
breaking out of a loop
hello. i am writing a program that prints all pythagorean triplets (ex. 3^2 + 4^2 = 5^2) until a + b + c = 1000. My code does that but keeps running after it finds that combination. I need it to print all the pythagorean triplets UP UNTIL it finds the combo (which happens to be 200, 375, 425) and then STOPS right away. As it stands right now it finds that combo and keeps going with the rest of the combinations that are still less than 1000. how can i stop the loops as soon as it finds the right combo?
here is my code:
Code:
//Written by: Jen G
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
int a;
int b;
int c;
int square;
int goal;
int sum;
for(a = 1; a < 1000; a++)
{
for(b = 1; b < 1000; b++)
{
if (a < b)
{
sum = (a * a) + (b * b);
for(c = 1; c < 1000; c++)
{
square = (c*c);
if (sum == square)
{
goal = a + b + c;
if (goal < 1000)
{
cout << "a: " << a << " b: " << b << " c: " << c << endl;
}
else if (goal == 1000)
{
cout << a << " + " << b << " + " << c << " = 1000" << endl;
cout << "abc = " << (a*b*c) << endl;
}
}
}
}
}
}
system("pause");
return 0;
}
Last edited by FeralDruidonWoW; September 17th, 2009 at 06:58 PM.
-
September 17th, 2009, 06:35 PM
#2
Re: breaking out of a loop
Originally Posted by FeralDruidonWoW
hello. i am writing a program that prints all pythagorean triplets (ex. 3^2 + 4^2 = 5^2) until a + b + c = 1000. My code does that but keeps running after it finds that combination.
The simplest solution is to just introduce a boolean, and put the value of that boolean in the loop condition:
Code:
bool foundMyNumbers = false;
for(a = 1; a < 1000 && !foundMyNumbers; a++)
{
for(b = 1; b < 1000 && !foundMyNumbers; b++)
//....
// etc...
//...
if ( I hit the magic combination )
foundMyNumbers = true;
//...
}
//...
}
Regards,
Paul McKenzie
-
September 17th, 2009, 06:39 PM
#3
Re: breaking out of a loop
Excellent! thanks for the advice. Program terminates as expected now
-
September 17th, 2009, 06:53 PM
#4
Re: breaking out of a loop
Originally Posted by FeralDruidonWoW
Excellent! thanks for the advice. Program terminates as expected now
In this particular case, all that was required of you was to explicitly return from main when a + b + c == 1000.
If your ultimate goal is to find the triplet that satisfies the condition outlined above then you're doing a little too much work.
The following is my solution from a while back to the same problem in C99:
Code:
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
for(uint32_t a = 1;; ++a)
{
for(uint32_t b = 999; b > a; --b)
{
const uint32_t c = 1000 - a - b;
if((a * a) + (b * b) == (c * c))
{
printf("%u\n", (a * b * c));
fflush(stdout);
return EXIT_SUCCESS;
}
}
}
}
Last edited by Plasmator; September 17th, 2009 at 07:00 PM.
Reason: Wrong literal.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|