Thread: Minimum and maximum in all positions.

  1. #1
    Registered User
    Join Date
    Jan 2015
    Posts
    33

    Minimum and maximum in all positions.

    I wrote a program to find the minimum and the maximum values from a vector. It works fine. What I'm trying to do is show the positions of said values and it's not working quite right. When I insert 4 elements: 2 0 1 3 it says:

    "The min and max are 0 and 3
    The position of the min is:
    01The position of the max is:
    03"

    What am I doing wrong? Here is the code:

    Code:
     
    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
        int A[10], i, j, n, min, max, C[10], k=0, D[10], l=0;
        printf("Insert no. of elements in vector A\n");
        scanf("%d", &n);
        printf("What are the elements of vector A?\n");
        for (i = 0; i < n; i++)
                scanf("%d", &A[i]);
        min = max = A[0];
        for (i = 0; i < n; i++)
        {
            if (min >= A[i])
    
            {
                min = A[i];
                C[k] = i;
                k++;
                
            }
    
            if (max <= A[i])
            {
    
                max = A[i];
                D[l] = i;
                l++;
            }
        }
    
        printf("The min and the max are %d and %d\n", min, max);
        printf("The position of the min is:\n");
        for (i = 0; i < k; i++)
            printf("%d", C[i]);
        printf("The position of the max is:\n");
        for (i = 0; i < l; i++)
            printf("%d", D[i]);
        getch();
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    There can only be one minimum value and only one maximum value, though there might be multiple elements with these values, or maybe there array only has one element, in which case these values will be the same.

    I suggest that you find the minimum and maximum values, and then search the array for elements with these values in order to print their positions. This way, you will not need extra arrays.

    If you do want to use the extra arrays, then you need to reset k to 0 whenever you find a new current minimum. Then, if you do not find a new current minimum, you still need to check if the current element has the value of the current minimum, in which case you record its position and increment k. Do likewise for the maximum.
    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
    Registered User
    Join Date
    Jan 2015
    Posts
    33
    I managed to do it as this

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
        int A[10], i, j, n, min, max, k=0, l=0;
        printf("Insert no. of elements in vector A\n");
        scanf("%d", &n);
        printf("What are the elements of vector A?\n");
        for (i = 0; i < n; i++)
                scanf("%d", &A[i]);
        min = max = A[0];
        for (i = 0; i < n; i++)
        {
            if (min >= A[i])
    
            {
                min = A[i];
                k = i;
                
            }
    
            if (max <= A[i])
            {
    
                max = A[i];
                l = i;
            }
            
        }
    
        printf("The min and the max are %d and %d\n", min, max);
        printf("The position of the min is:\t%d\n", k);
        printf("The position of the max is:\t%d", l);
    
        getch();
    }
    The only problem left if that it doesn't show my two positions if I have two maximums or two minimums, it just shows me the last position of each. What can I do?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That's why I suggested that you do a search of the minimum/maximum values in order to display the positions where they occur.
    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

  5. #5
    Registered User
    Join Date
    Jan 2015
    Posts
    33
    I don't really know how to do that. I'm thinking of something like

    Code:
    if (A[i] == min)
       C[k] = A[i];
    And then in the end print min positions as C[k] and do the same for max with D[l]?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Something like this:
    Code:
    printf("The positions of the min are: ");
    for (i = 0; i < n; i++)
    {
        if (min == A[i])
        {
            printf("%d ", i);
        }
    }
    printf("\n");
    Quote Originally Posted by OctavianC
    And then in the end print min positions as C[k] and do the same for max with D[l]?
    Yes, that is possible too, maybe even slightly more efficient at the cost of space, assuming that n is large and there are few elements with the min/max values.
    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
    Registered User
    Join Date
    Jan 2015
    Posts
    33
    Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. finding about maximum or minimum point
    By Ph0x in forum C Programming
    Replies: 4
    Last Post: 10-29-2014, 02:44 PM
  2. Minimum/maximum number problem. Please help!
    By PYROMANIAC702 in forum C Programming
    Replies: 43
    Last Post: 07-15-2011, 12:28 AM
  3. maximum and minimum
    By aslak in forum C Programming
    Replies: 35
    Last Post: 12-14-2008, 03:54 PM
  4. Maximum and Minimum Values
    By swaugh in forum C Programming
    Replies: 7
    Last Post: 12-16-2006, 09:43 PM
  5. Maximum And Minimum
    By drdodirty2002 in forum C++ Programming
    Replies: 2
    Last Post: 10-19-2004, 12:39 AM

Tags for this Thread