Like Tree2Likes
  • 1 Post By anduril462
  • 1 Post By nonoob

What is wrong with this program?

This is a discussion on What is wrong with this program? within the C Programming forums, part of the General Programming Boards category; What is wrong with this program? A Pythagorean triplet is a set of three natural numbers, a < b < ...

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    5

    What is wrong with this program?

    What is wrong with this program?

    A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
    a^2 + b^2 = c^2

    For example, 32 + 42 = 9 + 16 = 25 = 52.

    There exists exactly one Pythagorean triplet for which a + b + c = 1000.
    Find the product abc.

    So i do this and here is my program to find a,b and c.

    Code:
    #include <stdio.h>
    main()
    {
    int a,b,c;
    
    for (a=0;a<=1000;a++)
    {
        for (b=0;b<=1000;b++)
           {
               for (c=0;c<=1000;c++)
                   {
                      if (a+b==c&&((a*a)+(b*b)==(c*c))&&(a<b<c)) 
                                 printf("%d,%d,%d",a,b,c);
            }
                }
            }
    getch();
    }
    Last edited by Simar Chhabra; 05-15-2012 at 10:29 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    What makes you think that there is something wrong with your program?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    5
    it doesn't work...

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    As part of the process of helping you to diagnose and fix your own problems, tell us how exactly does it not work.

    Is there a compile error? If so, what is the full error message? What do you understand by the error message?

    Or, did the program compile but crash when run? If so, use your debugger to find the point of the crash and tell us where that is. The error would likely lie somewhere before that point.

    Or, did you run the program, but it gave incorrect results? If so, what was the test input (none in this case), expected output and actual output?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    5
    There was no compiling error. The problem was that it gave incorrect results. The point of the program is to find a pythagorean triplet but in this case it got a large amount of 0's. so for example outputs were "0,1,1", "0,2,2" etc. until 1000 then a new form of output started: "1,0,1" "2,0,2" until 1,000. Then it ended.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    So, a simple solution here is to start your loops from 1 rather than 0.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    5
    Well, Unfortunately, I tried that before and just now, and the program failed to execute

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Then debug it.

    Look, I gave you a recipe for asking for help. Follow it. Otherwise, my answers will be just as vague as your problem description.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,425
    Code:
    (a<b<c)
    You can't do compound inequalities like that in C. You must express each part separately and join them with an && or ||.
    Simar Chhabra likes this.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Right. That piece of code wasn't there the last time I checked. You should also warn people when you update your code, or better yet, post the updated code.

    Furthermore, you don't need that check. Rather, you should start the outer loop from 1, but then you start the middle loop from a, and the inner loop from b.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    May 2012
    Posts
    5
    its ok i just did using python but I'm still trying to fully understand C so I already knew how to do it

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    What is your current code?

    Quote Originally Posted by Simar Chhabra
    its ok i just did using python but I'm still trying to fully understand C so I already knew how to do it
    Out of curiosity, but what is your Python version? If you did this using Python for loops, I can expect that you might get a little stuck with the C version, but if you did this using Python while loops, the logic is likely to be exactly the same. Furthermore, the condition to test should be the same, but your C version looks wrong (the a+b==c check shouldn't be there, rather, you should be checking for a + b + c == 1000).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Code:
    if (a + b + c == 1000 && a * a + b * b == c * c && a < b && b < c)
    stahta01 likes this.

  14. #14
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    Simar, why do you have this in your condition? 'a+b==c'. compare that to your definition of a pythagorean triple.

  15. #15
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    You don't even need a loop for c. You're looping through 1000 values when you know up front that only one of them at most can be correct.

    A little algebra knowledge allows one to turn this:
    a + b + c == 1000
    into this:
    c == 1000 - a - b
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Something is wrong with my box program :C
    By Rukris in forum C Programming
    Replies: 10
    Last Post: 10-25-2011, 09:26 PM
  2. Help! What is wrong with this program?
    By Northstar in forum C Programming
    Replies: 1
    Last Post: 11-05-2007, 12:05 AM
  3. do you see anything wrong with this program?
    By seal in forum C Programming
    Replies: 3
    Last Post: 09-22-2005, 07:43 AM
  4. What is wrong with this program ?
    By Nutshell in forum C Programming
    Replies: 8
    Last Post: 01-11-2002, 10:22 PM
  5. What's wrong with this program?
    By marCplusplus in forum C++ Programming
    Replies: 4
    Last Post: 12-19-2001, 12:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21