Thread: need urgent help with if/else loops

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    4

    need urgent help with if/else loops

    having some probs with my code, just a minor error. if anyone can help my aim-ricco4014, itd be much appreciated, the assignment is due in 2 hrs ><

  2. #2
    Registered User
    Join Date
    Feb 2008
    Posts
    4
    Write a program which reads three integer values and orders them.The program will then print them in ascending order
    (smallest integer first) and state if there are duplicate inputs.
    When executed, if there are no duplicate inputs your program’s output should be as follows:
    Please enter integer #1 : 4
    Please enter integer #2 : -5
    Please enter integer #3 : 10
    Output Message: -5 4 10 No duplicates found.
    On the other hand, if there are duplicate inputs your program’s output should be as follows:
    Please enter integer #1 : 2
    Please enter intefer #2 : 4
    Please enter integer #3 : 2
    Output Message: 2 2 4 Duplicates found.

    -----this is what the output should be and this is my code---- for some reason when i put in duplicates it displays them a bunch of times in the output message.

    #include <stdio.h>
    int main( void )
    {
    int i1;
    int i2;
    int i3;
    printf("Please enter integer #1:");
    scanf("&#37;d", &i1);
    printf("Please enter integer #2:");
    scanf("%d", &i2);
    printf("Please enter integer #3:");
    scanf("%d", &i3);

    if (i1 <= i2 && i1 <= i3)
    {
    {
    printf("%d ", i1);
    }
    if (i2 <= i3)
    {
    printf("%d %d ", i2, i3);
    }
    if (i3 <= i2)
    {
    printf("%d %d ", i3, i2);
    }
    }
    if (i2 <= i1 && i2 <= i3)
    {
    {
    printf("%d ", i2);
    }
    if (i1 <= i3)
    {
    printf("%d %d ", i1, i3);
    }
    if (i3 <= i1)
    {
    printf("%d %d ", i3, i1);
    }
    }
    if (i3 <= i1 && i3 <= i2)
    {
    {
    printf("%d ", i3);
    }
    if (i1 <= i2)
    {
    printf("%d %d ", i1, i2);
    }
    if (i2 <= i1)
    {
    printf("%d %d ", i2, i1);
    }
    }
    if (i1 == i2 || i2 == i3 || i1==i3)
    {
    printf("Duplicates Found.\n");
    }
    if (i1 != i2 && i2 != i3 && i1 != i3)
    {
    printf("No Duplicates Found.\n");
    }


    return 0;
    }
    Last edited by ricco401; 02-06-2008 at 06:00 PM.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Unfortunately for you, we don't do homework.

    Fortunately for you, if it's really just a minor error, post it here and I'm sure someone will be able to help you with it.

    In other words, we'll help you do your homework, but we won't write it for you.

    And posting it here is a much better idea than waiting for someone to contact you with aim. Here, many people can read it with very little effort, and possibly help you out. If it's easy to do something, people will be more inclined to do it.

    [edit] And there's another thing -- I managed to read your post before you posted your second post! Imagine that.

    Use code tags: [code] code here [/code] [/edit]

    [edit=2]
    for some reason when i put in duplicates it displays them a bunch of times in the output message.
    Why do you think? If i1==i2, what do you think is going to happen here?
    Code:
    if (i1 <= i2)
    {
    printf("&#37;d %d ", i1, i2);
    }
    if (i2 <= i1)
    {
    printf("%d %d ", i2, i1);
    }
    That's right, both conditions are true, and so both if statements will be executed.

    I think you're approaching the problem the wrong way. Why don't you actually try to sort the numbers first. If you do that, determining whether there are any duplicates will become much simpler. [/edit]
    Last edited by dwks; 02-06-2008 at 06:02 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    4
    :P well thanks, any help would be appreciated

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by ricco401
    for some reason when i put in duplicates it displays them a bunch of times in the output message.
    The reason being, of course, that you told the computer to do so.

    Take the case of 2 2 4 as in your sample input. Which of your if statements will be true? Consequently, which print statements will print? What can you do about it?

    PS: Despite the name of your thread, there are no loops in your program. There aren't any such things as if/else loops anyway.

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    4
    sorry im quite new to the language, so since the input "2 2 4" would satisfy more then 1 if statement, do i need to link them all together or how do i fix it?

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Ah, you could try my suggestion. First sort the three numbers. Printing them is very easy, if they're already sorted. Then just compare i1 to i2, and i2 to i3, to determine whether or not there are any duplicates.

    If you don't sort the three numbers, you end up with code like this: http://www.cs.mtu.edu/~shene/COURSES...ap03/sort.html
    *shudders*

    If you do:
    Code:
    printf("&#37;d %d %d\n", i1, i2, i3);
    Right, so how do you sort three numbers?

    Give it a try, and if you can't figure it out, let us know . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The fact that "2 2 4" would satisfy more than 1 if statement indicates that you're not asking the right questions.

    Currently, you're asking "is i1 less than or equal to i2" and "is i2 less than or equal to i1". You don't want to ask both of these questions, because you'll get misleading answers in some cases. Think through how you would decide which of three numbers is the smallest, using your flowchart-drawing gadget if you like. But by the time you get to the end of your asking-questions-phase, you should have one (and only one!) ordering of the three numbers that "fits". Then, once you know that ordering, you can move on to printing them out.

    As to "linking them all together", if you mean by that "hey can I use else statements too", then the answer is definitely yes.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by dwks View Post
    If you don't sort the three numbers, you end up with code like this: http://www.cs.mtu.edu/~shene/COURSES...ap03/sort.html
    *shudders*
    Ah the beauty of Fortran 90. Give me a minute and I'll see what a little F77 computed-goto can turn that into...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loops Trouble
    By rlframpton in forum C Programming
    Replies: 2
    Last Post: 04-17-2009, 01:08 AM
  2. Multiple thread for loops
    By lehe in forum C++ Programming
    Replies: 12
    Last Post: 03-29-2009, 12:01 PM
  3. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  4. loops with incrementing strings .... ?
    By twomers in forum C++ Programming
    Replies: 1
    Last Post: 12-12-2005, 11:29 AM
  5. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM