helps

Discussion in 'Programming General' started by PsychicFreak, Jul 18, 2010.

helps
  1. Unread #1 - Jul 18, 2010 at 8:46 PM
  2. PsychicFreak
    Joined:
    Jun 6, 2008
    Posts:
    53
    Referrals:
    0
    Sythe Gold:
    0

    PsychicFreak Member

    helps

    Create a program that will ask a user to input 3 numbers. Use the if-else statement to get the MINIMUM and MAXIMUM of the given numbers.
    Sample Output:

    Enter first number: 65
    Enter second number: 45
    Enter third number: 20

    The minimum is 20 and the maximum is 65.
     
  3. Unread #2 - Jul 18, 2010 at 9:46 PM
  4. blindkilla
    Joined:
    Jun 22, 2005
    Posts:
    1,896
    Referrals:
    0
    Sythe Gold:
    6
    Discord Unique ID:
    282000633404456960
    Discord Username:
    sogord

    blindkilla Guru
    $25 USD Donor New

    helps

    Does this have to be in c or c++?
     
  5. Unread #3 - Jul 18, 2010 at 10:16 PM
  6. PsychicFreak
    Joined:
    Jun 6, 2008
    Posts:
    53
    Referrals:
    0
    Sythe Gold:
    0

    PsychicFreak Member

    helps

    i have the codes here blind

    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>

    int main()
    {
    int a, b, c;

    printf("Enter first number: ");
    scanf("%d",&a);

    printf("Enter second number: ");
    scanf("%d",&b);

    printf("Enter third number: ");
    scanf("%d",&c);

    if (a>b&&c)
    {
    printf("\n The maximum number is:%d",a);
    }
    if (b>a&&c)
    { printf("\n The maximum number is:%d",b);
    }
    if (c>a&&b)
    {
    printf("\n The maximum number is:%d",c);
    }


    else if (a<b&&c)
    {
    printf("\n The minimum number is:%d",a);
    }
    else if (b<a&&c)
    { printf("\n The minimum number is:%d",b);
    }
    else if (c<a&&b)
    {
    printf("\n The minimum number is:%d",c);
    }






    getch ();
    return 0;
    }





    the result is maximum maximum how can i change the other one into minimum?
     
  7. Unread #4 - Jul 18, 2010 at 10:27 PM
  8. blindkilla
    Joined:
    Jun 22, 2005
    Posts:
    1,896
    Referrals:
    0
    Sythe Gold:
    6
    Discord Unique ID:
    282000633404456960
    Discord Username:
    sogord

    blindkilla Guru
    $25 USD Donor New

    helps

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    
    int main()
    {
    int a, b, c;
    
    printf("Enter first number: ");
    scanf("%d",&a);
    
    printf("Enter second number: ");
    scanf("%d",&b);
    
    printf("Enter third number: ");
    scanf("%d",&c);
    
    if (a>b && a > c)
    {
    printf("\n The maximum number is:%d",a);
    }
    else if (b > a && b > c)
    { printf("\n The maximum number is:%d",b);
    }
    else if (c > a && c > b)
    { 
    printf("\n The maximum number is:%d",c);
    }
    
    if (a < b && a < c)
    {
    printf("\n The minimum number is:%d",a);
    }
    else if (b < a && b < c)
    { printf("\n The minimum number is:%d",b);
    }
    else if ( c < a && c < b)
    { 
    printf("\n The minimum number is:%d",c);
    }
    
    
    Your conditions were wrong. You had, if (c<a&&b). Basically, if c is less than a and b is true (in this case, if b is anything but 0). What you needed was, if (c < a && c < b).

    Another thing was your if/else if structure. You had:

    if()..
    if()..
    if()..
    else if()..
    else if()..
    else if()..


    What I highlighted in red, is what I guess I could say is, grouped. The first three if statements would be checked, regardless of whether the previous was true or not. However, with the third if statement, and the following else if statements, if any of those evaluated to be true, it would go to the end of the group of if statements.

    Sorry if the explanation isn't too great. Let me know how it works out for you.
     
  9. Unread #5 - Jul 18, 2010 at 10:39 PM
  10. PsychicFreak
    Joined:
    Jun 6, 2008
    Posts:
    53
    Referrals:
    0
    Sythe Gold:
    0

    PsychicFreak Member

    helps

    ohh i see tnx blind :)) i got it hehehe ty bro :D
     
  11. Unread #6 - Jul 18, 2010 at 11:21 PM
  12. blindkilla
    Joined:
    Jun 22, 2005
    Posts:
    1,896
    Referrals:
    0
    Sythe Gold:
    6
    Discord Unique ID:
    282000633404456960
    Discord Username:
    sogord

    blindkilla Guru
    $25 USD Donor New

    helps

    No problem, good luck :D
     
  13. Unread #7 - Jul 21, 2010 at 12:32 AM
  14. aznguy94
    Joined:
    Feb 11, 2007
    Posts:
    304
    Referrals:
    0
    Sythe Gold:
    0

    aznguy94 Forum Addict

    helps

    The best way to do this is to actually SORT the numbers, not just PRINT them in sorted order.

    Basically, for a program like this, you want to use the bubble sort technique (http://en.wikipedia.org/wiki/Bubble_sort), except that you only have 3 variables to sort.

    Bubble sort basically works by comparing adjacent values and swapping them if they are out of order. You start from the first two and when you get to the last two you start over again with the first two.

    Example...

    you start with:
    4 7 3

    First pass:
    compare first two, 4 and 7
    they are in order so you don't swap
    you end with
    4 7 3

    compare second and third numbers, 7 and 3
    they are out of order so you swap
    you end with
    4 3 7

    Second pass:
    compare first two, 4 and 3
    they are out of order so you swap
    you end with
    3 4 7

    After one "pass" you swap enough times such that the last number in the series is in the correct spot. The first pass you make two comparisons so 7 is in the correct spot at the end. The second pass you make one comparison (since 7 is already correct) so that 4 is in the correct spot. if 4 is in the correct spot then so is 3. Every pass you make one less comparison than the pass before because one more number ends up in the correct spot.

    you make sum(n-1) comparisons to sort numbers using bubble sort

    3 numbers - 3 comparisons
    4 numbers - 6 comparisons
    5 numbers - 10 comparisons
    6 numbers - 15 comparisons

    etc.

    if you still don't understand see http://en.wikipedia.org/wiki/Bubble_sort#Step-by-step_example

    i hope you understand the basic concept of bubble sort such that later you can use loops to sort an undetermined amount of numbers

    ill go ahead and give you some sample code so you can see its implementation better

    Code:
    int firstNum, secondNum, thirdNum;
    cout << "Enter first number: ";
    cin >> firstNum;
    
    cout << "Enter second number: ";
    cin >> secondNum;
    
    cout << "Enter third number: ";
    cin >> thirdNum;
    
    int temp;
    
    if (firstNum > secondNum)
    {
         temp = firstNum;
         firstNum = secondNum; // these three lines swap firstNum and secondNum
         secondNum = temp;
    }
    
    if (secondNum > thirdNum)
    {
         temp = secondNum;
         secondNum = thirdNum;
         thirdNum = temp;
    }
    
    if (firstNum > secondNum)
    {
         temp = firstNum;
         firstNum = secondNum;
         secondNum = temp;
    }
    
    cout << firstNum << " " << secondNum << " " << thirdNum; // to print all numbers sorted
    
    cout << firstNum << " " << thirdNum; // to print min then max
    
     
  15. Unread #8 - Jul 21, 2010 at 10:38 AM
  16. blindkilla
    Joined:
    Jun 22, 2005
    Posts:
    1,896
    Referrals:
    0
    Sythe Gold:
    6
    Discord Unique ID:
    282000633404456960
    Discord Username:
    sogord

    blindkilla Guru
    $25 USD Donor New

    helps

    If you're going to bubble sort than make a function and pass in an array of values, which are looped through. Sorting like that basically defeats the purpose of a bubble sort.
     
  17. Unread #9 - Jul 21, 2010 at 3:54 PM
  18. aznguy94
    Joined:
    Feb 11, 2007
    Posts:
    304
    Referrals:
    0
    Sythe Gold:
    0

    aznguy94 Forum Addict

    helps

    Well I didn't say I was using bubble sort exactly, rather I'm borrowing the style from bubble sort. There's no reason to use arrays and loops since there are only 3 variables and I'm assuming he doesn't know arrays or loops yet.

    I just think its more useful to use a bubble-sort-like technique because you actually sort the numbers, rather than print them sorted. Also, it uses less if's than just printing them sorted. IMO it's a more elegant way of solving the problem but that's just me...
     
  19. Unread #10 - Jul 21, 2010 at 4:25 PM
  20. blindkilla
    Joined:
    Jun 22, 2005
    Posts:
    1,896
    Referrals:
    0
    Sythe Gold:
    6
    Discord Unique ID:
    282000633404456960
    Discord Username:
    sogord

    blindkilla Guru
    $25 USD Donor New

    helps

    Bubble sorting is kind of pointless for non arrays. No need to get into it when only sorting 3 numbers. It's just over complicating a simple task.
     
  21. Unread #11 - Jul 21, 2010 at 9:22 PM
  22. aznguy94
    Joined:
    Feb 11, 2007
    Posts:
    304
    Referrals:
    0
    Sythe Gold:
    0

    aznguy94 Forum Addict

    helps

    I don't get why you're arguing with me. Nowhere did I tell him to learn bubble sort. I'm just telling him how to sort 3 numbers using a technique similar to bubble sort. I'm just trying to help the guy, so why are you trying to stop that? Anyways, he can do what he wants...
     
< Help with account creator in java | question >

Users viewing this thread
1 guest


 
 
Adblock breaks this site