Search
 
SCRIPT & CODE EXAMPLE
 

CPP

findung the mode in c++

int number = array[0];
int mode = number;
int count = 1;
int countMode = 1;

for (int i=1; i<size; i++)
{
      if (array[i] == number) 
      { // count occurrences of the current number
         ++count;
      }
      else
      { // now this is a different number
            if (count > countMode) 
            {
                  countMode = count; // mode is the biggest ocurrences
                  mode = number;
            }
           count = 1; // reset count for the new number
           number = array[i];
  }
}

cout << "mode : " << mode << endl;
Comment

PREVIOUS NEXT
Code Example
Cpp :: read struct from file c++ 
Cpp :: 2d array using vector 
Cpp :: maximum value in map in c++ 
Cpp :: structure and function c++ 
Cpp :: how to get input in cpp 
Cpp :: static_cast c++ 
Cpp :: print in c++ 
Cpp :: how to run a c++ program in the background 
Cpp :: check if an element is in a map c++ 
Cpp :: calling struct to a struct c++ 
Cpp :: c++ infinite for loop 
Cpp :: copy 2 dimensional array c++ 
Cpp :: change abstract title name latex 
Cpp :: prints out the elements in the array c++ 
Cpp :: how to iterate from second element in map c++ 
Cpp :: reverse string c++ 
Cpp :: check if file is empty c++ 
Cpp :: define unicode c++ 
Cpp :: c++ int main() 
Cpp :: string stream in c++ 
Cpp :: for loop in c++ 
Cpp :: C++ press enter to continue function 
Cpp :: how to erase a certain value from a vector in C++ 
Cpp :: c++ load file as vector 
Cpp :: swapping of two numbers 
Cpp :: how to sort a string alphabetically in c++ 
Cpp :: cpp pushfront vector 
Cpp :: who to include a library c++ 
Cpp :: find the missing number 
Cpp :: See Compilation Time in c++ Program 
ADD CONTENT
Topic
Content
Source link
Name
8+7 =