hello , i want to ask is there any function that gets the mode of an array without using pointers ???plz tell me if there is

Recommended Answers

All 2 Replies

Use a loop and iterate through the array with the loop counter as the index into the array.

Here is another way it might be done using c++ algorithms

> is there any function that gets the mode of an array without using pointers ?

No. The array subscripting operator is a binary operator on a pointer and an integral value. http://publib.boulder.ibm.com/infocenter/lnxpcomp/v7v91/index.jsp?topic=%2Fcom.ibm.vacpp7l.doc%2Flanguage%2Fref%2Fclrc05arsubex.htm

If the distribution is unimodal and the array can be modified (sorted), a simple way to get the modal value is to sort the array and then make a single pass through it to pick up the value that occurs most often. This takes O( N log N ) time. For example:

#include <iterator>
#include <algorithm>

template< typename RAND_ACCESS_ITER >
typename std::iterator_traits<RAND_ACCESS_ITER>::value_type
        a_modal_value( RAND_ACCESS_ITER begin, RAND_ACCESS_ITER end )
{
  typedef typename std::iterator_traits<RAND_ACCESS_ITER>::value_type value_type ;

  std::sort( begin, end ) ;

  std::size_t max_freq = 0 ;
  std::size_t curr_freq = 1 ;
  value_type mode = *begin ;
  value_type previous = *begin ;

  for( ++begin ; begin != end ; ++begin )
  {
    if( *begin == previous ) ++curr_freq ;
    else
    {
      if( curr_freq > max_freq )
      {
        max_freq = curr_freq ;
        mode = previous ;
      }
      previous = *begin ;
      curr_freq = 1 ;
    }
  }
  return curr_freq > max_freq ? *(--end) : mode ;
}

If the sequence is mutimodal, or the array can not be modified, it can still be done in O( N log N ) time by using auxiliary storage for a std::map<> For example,

#include <iterator>
#include <map>

template< typename ITERATOR, typename OUTPUT_ITERATOR >
void copy_modal_values( ITERATOR begin, ITERATOR end, // input sequence
         OUTPUT_ITERATOR result ) // all modes to be placed in this sequence
{
  typedef typename std::iterator_traits<ITERATOR>::value_type key_type ;
  typedef std::map<key_type,std::size_t> map_type ;
  map_type frequency_map ;
  while( begin != end ) ++frequency_map[*begin++] ;

  std::size_t max_freq = 0 ;
  for( typename map_type::iterator iter = frequency_map.begin() ;
       iter != frequency_map.end() ; ++iter )
    if( iter->second > max_freq ) max_freq = iter->second ;

  for( typename map_type::iterator iter = frequency_map.begin() ;
       iter != frequency_map.end() ; ++iter )
    if( iter->second == max_freq ) *result++ = iter->first ;
}

A simple test driver:

#include <iostream>

int main()
{
  int a[] = { 1, 2, 6, 7, 6, 2, 8, 8, 6, 8, 2, 9, 8, 2 } ;
  enum { N = sizeof(a) / sizeof(*a) } ;

  std::cout << "mode(s) of [ " ;
  std::copy( a, a+N, std::ostream_iterator<int>(std::cout," ") ) ;
  std::cout << "] is (are) [ " ;
  copy_modal_values( a, a+N, std::ostream_iterator<int>(std::cout," ") ) ;
  std::cout << "]\n" ;

  std::cout << "a mode is: " << a_modal_value( a, a+N ) << '\n' ;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.