Thread: Pythagorean Triples program

  1. #1
    Do you C what I C? jamesallen4u's Avatar
    Join Date
    Oct 2011
    Posts
    43

    Pythagorean Triples program

    Hello,

    I am writing a basic program in C whose function is to print all the Pythagorean Triples like these:

    3,4,5

    6,8,10

    5,12,13

    ...

    all the way up to 45.

    This is my code:

    Code:
    #include <stdio.h>
    
    int main()
    {
        int LegOne, LegTwo, Hypotenuse;
    
        Hypotenuse = 0;
    
        while ( Hypotenuse < 50 )
        {
            LegTwo = 1;
            
           while ( LegTwo < 50 )
           {
               LegOne = 1;
           
               while ( LegOne < 50 )
               {
                   if ( LegOne*LegOne + LegTwo*LegTwo == Hypotenuse*Hypotenuse && LegOne < LegTwo )
                  {    
                      printf("\n\t\t The sides are %4d,%4d,%4d", LegOne,LegTwo,Hypotenuse);
                  }
                  LegOne++;
               }
               LegTwo++;
          }
          Hypotenuse++;
      }
    
    printf(\n\n);
    
    }
    This code gives me this output:

    Pythagorean Triples program-output-png


    As you can see, the numbers to the far left are not in order, they are scrambled all over the place. Is there a way to make the left numbers in ascending order without using "pow"? Thanks a lot.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    But they are in order... they are in the exact order you calculated them in... If you want a different order, you can store your results in an array of structs, then sort the array any way you like. Alternatively you can cheat... change the order of the columns (note that the right column is in order)


    BTW... yes you can do this with while() loops (obviusly) but I would think this would be a lot simpler using for() loops.

  3. #3
    Do you C what I C? jamesallen4u's Avatar
    Join Date
    Oct 2011
    Posts
    43
    Quote Originally Posted by CommonTater View Post
    But they are in order... they are in the exact order you calculated them in... If you want a different order, you can store your results in an array of structs, then sort the array any way you like.
    I know they are in the order I calculated them in, but I don't know a way to get ascending numbers ie. 3,5,6,9...

    Also I don't know what structs are, I am new to C and programming in general.

    Quote Originally Posted by CommonTater View Post
    Alternatively you can cheat... change the order of the columns (note that the right column is in order)
    I was very tempted to do that but I want to learn how to do it the "right way"

    Quote Originally Posted by CommonTater View Post
    BTW... yes you can do this with while() loops (obviusly) but I would think this would be a lot simpler using for() loops.




    Unfortunately for() is beyond my scope of learning, but one of my friends looked at my code and said it could be done solely with while().

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by jamesallen4u View Post
    I was very tempted to do that but I want to learn how to do it the "right way"
    Actually, in this case changing the display order to match the way your loops run would be the right way...

    The hard way would be to build that array of structs and sort the thing... Take the easy way out...



    Unfortunately for() is beyond my scope of learning, but one of my friends looked at my code and said it could be done solely with while().
    Sounds to me like you need to sit down with a good book or tutorial on C (Google is your friend!) and undertake a deliberate study of the language. Any good text will present concepts in an order where you should be able to grasp them easily enough... just start on page 1, read and review until you understand, type up and compile all the exercises, play with the code, break it, fix it... find out what does and doesn't work... then move on to page 2... and so on till you run out of pages.

    C is not something you can learn piecemeal... 'cause you will end up with situations like this one... on an every increasing scale. It will be the stuff you don't know that comes back to bite you.

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by CommonTater View Post
    Sounds to me like you need to sit down with a good book or tutorial on C (Google is your friend!) and undertake a deliberate study of the language. Any good text will present concepts in an order where you should be able to grasp them easily enough... just start on page 1, read and review until you understand, type up and compile all the exercises, play with the code, break it, fix it... find out what does and doesn't work... then move on to page 2... and so on till you run out of pages.
    ^_^ You just described an efficient algorithm for learning C through book reading!!!
    Devoted my life to programming...

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by jamesallen4u View Post
    I know they are in the order I calculated them in, but I don't know a way to get ascending numbers ie. 3,5,6,9...
    They are in ascending order ... by the last column.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    2
    They are in ascending order ... by the last column because you while(Hypotenuse) first.
    If you first loop LegOne then loop LegTwo then Hypotenuse), you will get the right order.


    Code:
    LegOne = 1;
    while(LegOne < 50)
    {
      LegTwo = 1;
      while(LegTwo < 50)
      {
         Hypotenuse = 1;
         while(Hypotenuse <50)
         {
                if (.....)
                  printf();
                Hypotenuse ++;
         }
         LegTwo ++;
      }
       LegOne ++;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pythagorean Help
    By n3n in forum C++ Programming
    Replies: 12
    Last Post: 09-25-2010, 06:21 PM
  2. pythagorean triples
    By jack_carver in forum C Programming
    Replies: 4
    Last Post: 10-01-2009, 08:21 PM
  3. Pythagorean Numbers
    By NightLightSky in forum C++ Programming
    Replies: 4
    Last Post: 09-15-2009, 09:34 PM
  4. Pythagorean Theorem Program Comment, criticize, or whatever
    By Sshakey6791 in forum C++ Programming
    Replies: 7
    Last Post: 02-13-2009, 02:48 AM
  5. Pythagorean Programming
    By mikeprogram in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2005, 01:29 PM

Tags for this Thread