Thread: Suggestions for how to improve upon this

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    25

    Suggestions for how to improve upon this

    Okay, so here's the problem:

    If the ages of Ram, Shyam, and Ajay are input through the keyboard, write a program to determine the youngest of the three.

    Here's my answer:

    Code:
    #include <stdio.h>
    main()
    {
    
    int ram, shyam, ajay;
    
    printf("Enter Ram, Shyam, and Ajay's ages.\n");
    scanf("%d %d %d", &ram, &shyam, &ajay);
    
    if ((ram < shyam) && (ram < ajay))
    	printf("youngest = Ram\n");
    if ((shyam < ram) && (shyam < ajay))
    	printf("youngest = Shyam\n");
    if ((ajay < shyam) && (ajay < ram))
    	printf("youngest = Ajay\n"); 
    if (ajay > (ram && shyam) && (ram == shyam))
    	printf("youngest = Ram, Shyam\n");
    if (ram > (ajay && shyam) && (ajay == shyam))
    	printf("youngest = Ajay, Shyam\n");
    if (shyam > (ajay && ram) && (ajay == ram))
    	printf("youngest = Ajay, Ram\n");
    if (ram == shyam == ajay)
    	printf("They are the same age.\n");
    }
    So I'm at the point where I can get really simple things to work, but I know what I have is too much for the simple task at hand. I'd really appreciate any suggestions or corrections. Thanks in advance.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should state how does it not work.

    That said, you are probably facing a problem due to expressions like this: ajay > (ram && shyam)
    What you really mean to write is: ajay > ram && ajay > shyam.

    The same goes for: ram == shyam == ajay.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Note that main returns int, always. Not nothing, as you have specified no return type.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    25
    Quote Originally Posted by laserlight View Post
    You should state how does it not work.

    That said, you are probably facing a problem due to expressions like this: ajay > (ram && shyam)
    What you really mean to write is: ajay > ram && ajay > shyam.

    The same goes for: ram == shyam == ajay.
    I'm sorry, I don't understand what you mean here. Could you try to explain it differently? I also don't know what you mean as far as explaining how it works, because I thought you could look at the code and tell. Thanks for your response.

  5. #5
    Why am I a programmer? shoutatchickens's Avatar
    Join Date
    Mar 2008
    Posts
    45
    You have to explicitly state your comparison on both sides of "&&"

    You can't say:
    Code:
    value1 > (value2 && value3)
    if what you really mean is

    Code:
    (value1 > value2) && (value1 > value3)
    and the same for ==

    Code:
    value1 == value2 && value1 == value3
    One of the disadvantages of being a 22 year old RPG programmer is having to repeatedly explain to your friends that you don't make videogames for a living.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by newbcore
    'm sorry, I don't understand what you mean here. Could you try to explain it differently?
    Simply put the expression (ram && shyam) returns true (1) if both ram and shyam are non-zero, and false (0) if either or both of them are zero.

    But with the expression ajay > (ram && shyam), what you really want to do is compare the value of ajay with the value of ram and the value of shyam. What you end up doing is comparing the value of ajay with 0 or 1, depending on the values of ram and shyam.

    Quote Originally Posted by newbcore
    I also don't know what you mean as far as explaining how it works, because I thought you could look at the code and tell.
    In this case the problem was obvious to me, but it usually helps if you state something like: "the code gives incorrect output. For example, I enter <such and such> and get <such and such> but I expected <such and such>".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    First I would start with an optimal 3-input sorting network:
    Code:
    if (a < b) {
        if (b < c) {
            // a < b < c (case 1)
        } else if (a < c) {
            // a < c <= b (case 2)
        } else {
            // c <= a < b (case 3)
        }
    } else {
        if (a < c) {
            // b <= a < c (case 4)
        } else if (b < c) {
            // b < c <= a (case 5)
        } else {
            // c <= b <= a (case 6)
        }
    }
    Then, inside cases 3, 4, and 6 you need to do some extra work to check if the first two are equal or not, and in the case of case 6 also checking if the last two are also equal, giving cases 3b, 4b, 6b, and 6c.
    Last edited by iMalc; 12-30-2008 at 02:27 PM.
    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"

  8. #8
    Registered User
    Join Date
    Nov 2008
    Posts
    25
    Thanks very much, everyone. I'll work on your suggestions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Free Book Suggestions!
    By valaris in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-08-2008, 10:25 AM
  2. Problem with vector class (and suggestions)
    By cs_student in forum C++ Programming
    Replies: 9
    Last Post: 09-09-2008, 02:21 AM
  3. Math Book Suggestions
    By curlious in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-09-2003, 10:43 AM
  4. Need Suggestions
    By Drew in forum C++ Programming
    Replies: 3
    Last Post: 09-18-2003, 05:46 AM
  5. Suggestions.
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 02-06-2002, 12:21 PM