position of max

This is a discussion on position of max within the C++ Programming forums, part of the General Programming Boards category; i have written this code to print the position of max in the array. but how can i find all ...

  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
    21,448
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,448
    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).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 05: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, 11:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21