Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Mode Function

Mode Function

Scheduled Pinned Locked Moved C / C++ / MFC
data-structures
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    EvilSource
    wrote on last edited by
    #1

    The function has to find the mode (number most found in the array, for ex. array[]{1, 2, 1, 1} the mode is 1 since it appears 3 times in the array while 2 only apears once. movie is a class and it has a array of ints which movie.return_value(int) accesses. right now the mode equals the first number in the array, its like the second if isant getting evaluated right, anyway in my mind this should work and i cant find a way around it. void getmode(movie_data movie){ int numfound0=0, numfound1=0, mode=0, *num0ptr=&numfound0, *num1ptr=&numfound1, *modeptr=&mode; for(int j=0;j*num1ptr){ num1ptr=num0ptr; *modeptr=movie.return_value(j); } } } cout<<*num1ptr; movie.put_mode(modeptr); cout<

    P 1 Reply Last reply
    0
    • E EvilSource

      The function has to find the mode (number most found in the array, for ex. array[]{1, 2, 1, 1} the mode is 1 since it appears 3 times in the array while 2 only apears once. movie is a class and it has a array of ints which movie.return_value(int) accesses. right now the mode equals the first number in the array, its like the second if isant getting evaluated right, anyway in my mind this should work and i cant find a way around it. void getmode(movie_data movie){ int numfound0=0, numfound1=0, mode=0, *num0ptr=&numfound0, *num1ptr=&numfound1, *modeptr=&mode; for(int j=0;j*num1ptr){ num1ptr=num0ptr; *modeptr=movie.return_value(j); } } } cout<<*num1ptr; movie.put_mode(modeptr); cout<

      P Offline
      P Offline
      Paolo Messina
      wrote on last edited by
      #2

      Hi, Consider this code (yours is not readable):

      GetMode(int array[], int count)
      {
      int freq, max = 0;
      for (int i=0; i<count; i++)
      {
      freq = 0;
      for (int j=0; j<count; j++)
      {
      if (array[j]==array[i])
      freq++;
      }
      if (freq>max)
      max = freq;
      }
      return max;
      }

      You have to replace array[x] with movie.return_value(x) and maybe some other few things, but the algorythm seems pretty simple. You simply search for the most frequent value in the array. Paolo ------ "airplane is cool, but space shuttle is even better" (J. Kaczorowski)

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups