10 Replies - 2751 Views - Last Post: 22 February 2011 - 07:14 PM Rate Topic: -----

#1 iqbalmmz   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 94
  • Joined: 15-February 11

nested for loops; while and if statements

Posted 19 February 2011 - 07:47 PM

hey guys

I'm trying to figure out how to avoid printing out permutation of a Pythagorean triplet. Like I have the program all written but I am getting permutations of pythag triple.

Quote

A “Pythagorean triple” is three integers that are the sides of a right triangle, that is, the sum of the squares of the first two numbers is equal to the square of the third number. For example, (3, 4, 5) is a Pythagorean triple. Write a program that will list all Pythagorean triples where all three integers are less than or equal to 50. Do not list permutations of the same integers (e.g., do not list both (3, 4, 5) and (4, 3, 5) ). Use nested for loops.


This is what I have:

#include <stdio.h>
main ()
{
	int side1;
	int side2;
	int side3;
	
	for (side1 = 1; side1 <= 50; side1++){
			
		for (side2 = 1; side2 <= 50; side2++){
			
			for (side3 = 1; side3 <= 50; side3++){
				
				if ((side1 * side1) + (side2 * side2) == (side3 * side3)){
					printf("%d, %d, %d\n", side1, side2, side3);
				}
				
			}
		}
	}

	return 0;

}



Any help will be much appreciated, thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: nested for loops; while and if statements

#2 apw5020   User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 666
  • Joined: 26-March 09

Re: nested for loops; while and if statements

Posted 19 February 2011 - 09:06 PM

Here is one solution. Make an array called triples. When you find the first triple, the number you add to that array should be the sum of side1, side2, and side3. Then, when you find another triple, sum it and compare it against the triples already stored in your array. If it is already there, move on and if not then add it. Since 3+4+5=5+4+3 etc. it should skip over all duplicates.

This post has been edited by apw5020: 19 February 2011 - 09:07 PM

Was This Post Helpful? 1
  • +
  • -

#3 iqbalmmz   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 94
  • Joined: 15-February 11

Re: nested for loops; while and if statements

Posted 19 February 2011 - 09:09 PM

View Postapw5020, on 19 February 2011 - 09:06 PM, said:

Here is one solution. Make an array called triples. When you find the first triple, the number you add to that array should be the sum of side1, side2, and side3. Then, when you find another triple, sum it and compare it against the triples already stored in your array. If it is already there, move on and if not then add it. Since 3+4+5=5+4+3 etc. it should skip over all duplicates.


is there a way to do it without arrays? i know its pretty complicated, but we aren't allowed to use stuff we haven't covered in class.
Was This Post Helpful? 0
  • +
  • -

#4 apw5020   User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 666
  • Joined: 26-March 09

Re: nested for loops; while and if statements

Posted 19 February 2011 - 09:27 PM

Seems like a pain, but if you know how many unique triples there will be you could make that number of variables. Store in each of those variables the sum of the sides of the first instance of that triple. Then compare subsequent sums of triples to each of those variables. Do you see what I mean?

Does anyone have a better idea?

This post has been edited by apw5020: 19 February 2011 - 11:04 PM

Was This Post Helpful? 1
  • +
  • -

#5 iqbalmmz   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 94
  • Joined: 15-February 11

Re: nested for loops; while and if statements

Posted 19 February 2011 - 10:57 PM

View Postapw5020, on 19 February 2011 - 09:27 PM, said:

Seems like a pain, but if you know how many unique triples there will be you could make that number of variables. Store in each of those variables the sum of the sides of the first instance of that triple. Then compare subsequent sums of triples to each of those variables. Do you see what I mean? Also, make sure you initialize them to zero so you don't try to compare a number to nothingness!

Does anyone have a better idea?


I see exactly what you mean, and you're right that would be a pain. Theres gotta be a better way of doing this
Was This Post Helpful? 0
  • +
  • -

#6 apw5020   User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 666
  • Joined: 26-March 09

Re: nested for loops; while and if statements

Posted 19 February 2011 - 11:03 PM

How about this:

for (side3 = 1; side3 <= side2; side3++)
Was This Post Helpful? 1
  • +
  • -

#7 iqbalmmz   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 94
  • Joined: 15-February 11

Re: nested for loops; while and if statements

Posted 19 February 2011 - 11:24 PM

View Postapw5020, on 19 February 2011 - 11:03 PM, said:

How about this:

for (side3 = 1; side3 <= side2; side3++)


I made the changes you suggested but for some reason I can't compile that particular program?!? i'm trying to remotely log into my school's linux server but it doesn't connect, maybe the storm outside has something to do with it. I'm not sure what to do at this point
Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3803
  • View blog
  • Posts: 13,811
  • Joined: 08-August 08

Re: nested for loops; while and if statements

Posted 20 February 2011 - 06:58 AM

To avoid duplicates I would start side2 out at side1 + 1. That should be an easy change to your original code. Too easy to provide an example. ;)

Oh, and main() needs to return an int. Fix that or you'll likely lose points on your grade.

This post has been edited by CTphpnwb: 20 February 2011 - 07:05 AM

Was This Post Helpful? 1
  • +
  • -

#9 iqbalmmz   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 94
  • Joined: 15-February 11

Re: nested for loops; while and if statements

Posted 20 February 2011 - 10:55 AM

View PostCTphpnwb, on 20 February 2011 - 06:58 AM, said:

To avoid duplicates I would start side2 out at side1 + 1. That should be an easy change to your original code. Too easy to provide an example. ;)

Oh, and main() needs to return an int. Fix that or you'll likely lose points on your grade.


do you mean increment/increase side2 as side2 = side1 + 1 in the second for loop? Or when I am declaring my variables? Or in the printf statement, the algorithm for getting side2? Sorry, I'm a newb to this stuff :(
Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3803
  • View blog
  • Posts: 13,811
  • Joined: 08-August 08

Re: nested for loops; while and if statements

Posted 20 February 2011 - 11:08 AM

I mean that this line changes:
        for (side2 = 1; side2 <= 50; side2++){

This should be more than enough for you to figure it out. Think about what this line does and what I said above.
Was This Post Helpful? 1
  • +
  • -

#11 iqbalmmz   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 94
  • Joined: 15-February 11

Re: nested for loops; while and if statements

Posted 22 February 2011 - 07:14 PM

View PostCTphpnwb, on 20 February 2011 - 11:08 AM, said:

I mean that this line changes:
        for (side2 = 1; side2 <= 50; side2++){

This should be more than enough for you to figure it out. Think about what this line does and what I said above.

eureka!

#include <stdio.h>
int main ()
{
	int side1;
	int side2;
	int side3;
	
	for (side1 = 1; side1 <= 50; side1++){
			
		for (side2 = side1; side2 <= 50; side2++){
			
			for (side3 = side2; side3 <= 50; side3++){
				
				if ((side1 * side1) + (side2 * side2) == (side3 * side3)){
					printf("%d, %d, %d\n", side1, side2, side3);
				}
				
			}
		}
	}

	return 0;

}



so i just initialized side2 = side1 and side3=side2, i can't believe i couldn't think of this before!! thanks guys :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1