Thread: Find Maximum!

  1. #1
    Registered User alireza beygi's Avatar
    Join Date
    Dec 2011
    Location
    USA
    Posts
    17

    Find Maximum!

    I want to find third maximum of five numbers and it's position.
    My code find Maximum correctly but it's position is wrong!
    What's the problem?
    Thanks!
    Code:
    #include <stdio.h>
    #include <conio.h>
    int main ()
    {
        int x_max1;
        int x_max2;
        int x_max3;
        int order_m_3;
        int x [5];
        for (int i = 0; i < 5; i++)
            {
            printf ("Enter A Number:\n");
            scanf ("%d", &x [i]);
            }
        x_max1 = x [0];
        for (int i = 1; i < 5; i++)
        {
            if (x [i] > x_max1)
               x_max1 = x [i];
        }  
        x_max2 = -10000;
        for (int i = 1; i < 5; i++)
        {
            if (x [i] > x_max2 && x [i] != x_max1)
            x_max2 = x [i];
        }
        x_max3 = -10000;
        for (int i = 1; i < 5; i++)
        {
            if (x [i] > x_max3 && x [i] != x_max1 && x [i] != x_max2)
            x_max3 = x [i];
            order_m_3 = i + 1;
        }                 
        printf ("Third Max is %d and Position is %d", x_max3, order_m_3);
        getch ();
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You should work on your indentation, and get a editor with auto-indent. It would make your problem clear:
    Code:
    if (x [i] > x_max3 && x [i] != x_max1 && x [i] != x_max2)
        x_max3 = x [i];
    order_m_3 = i + 1;
    You want both those statements to be part of the "if", so use curly braces:
    Code:
    if (x [i] > x_max3 && x [i] != x_max1 && x [i] != x_max2) {
        x_max3 = x [i];
        order_m_3 = i + 1;
    }
    Last edited by anduril462; 01-06-2012 at 05:41 PM. Reason: Fixed incorrect wording

  3. #3
    Registered User alireza beygi's Avatar
    Join Date
    Dec 2011
    Location
    USA
    Posts
    17
    Oh! Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. find the maximum width & length of a box using for loops?
    By bac3bac0mm in forum C Programming
    Replies: 1
    Last Post: 03-17-2011, 10:23 PM
  2. Best way to find maximum number
    By mutombo in forum C Programming
    Replies: 3
    Last Post: 02-27-2009, 04:36 AM
  3. Find maximum number (while loop)
    By katherene in forum C++ Programming
    Replies: 1
    Last Post: 10-16-2006, 05:44 PM
  4. maximum number
    By drdodirty2002 in forum C Programming
    Replies: 7
    Last Post: 05-08-2003, 05:33 PM
  5. maximum
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 05-12-2002, 06:03 PM