Thread: position of max

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    5

    position of max

    i have written this code to print the position of max in the array.
    but how can i find all the positions of max? (3.5.9).

    Code:
    #include<stdio.h>
    main()
    {
        int array[10]={4,5,6,9,8,9,4,1,2,9};
        int max,pos;
    
    
        max=array[0];
    
        for(int i=1;i<10;i++)
            if(array[i]>max)
            {
            max=array[i];
            pos=i;
            }
       printf("position:%d",pos);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I believe you need a two pass solution since the array is unsorted: first find the max value, then iterate over the array and print each position whose value matches the max value.
    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
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Keep an array of the positions of the largest value so far. If you find a greater one, clear it and start storing the positions over again.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    5
    Quote Originally Posted by laserlight View Post
    I believe you need a two pass solution since the array is unsorted: first find the max value, then iterate over the array and print each position whose value matches the max value.
    you mean this:
    Code:
    #include<stdio.h>
    main()
    {
        int array[10]={4,5,6,9,8,9,4,1,2,9};
        int max;
    
    
        max=array[0];
    
        for(int i=1;i<10;i++)
            if(array[i]>max)
                max=array[i];
    
    
        for(int j=0;j<10;j++)
        if (array[j]==max)
        printf("\nposition:%d",j);
    }

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by -chr-
    you mean this:
    Roughly yes, but with slightly better indentation, and declaring main to return an int and including <cstdio> instead of <stdio.h>.

    A single pass solution is possible by storing the currently known positions of the currently known maximum in a std::vector or array (i.e., what anon later suggested - I was unable to correct my post earlier due to network problems).
    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

  6. #6
    Registered User
    Join Date
    Dec 2008
    Posts
    5
    Quote Originally Posted by anon View Post
    Keep an array of the positions of the largest value so far. If you find a greater one, clear it and start storing the positions over again.
    i 'll try it although i find it a bit difficult.

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You may want to use a std::vector for the positions since you don't know how many there will be.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ranged numbers
    By Desolation in forum Game Programming
    Replies: 8
    Last Post: 07-25-2006, 10:02 PM
  2. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  3. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. LISP (DrScheme) any one?
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-31-2004, 12:52 PM