Majority number

In this exercise I am asked to write a funcion to know if a number is MAYORITARIO. That is, if there is an element stored in the vector that appears more than N/2 times being N the number of elements of the Vector

Error on line 15

#include <iostream>
using namespace std;

bool mayoritario(float v[],int n);
int n;
float v;
int main()
{

mayoritario(v,n);
cout<<mayoritario(v,n);
return 0;
}

bool mayoritario(float v[],int n){ //Linea 15
    int i,j,nveces,nvmaximo=0;
    for(int i=0 ;i<=n/2 ;i++){
        nveces=1;
        for(int j=i+1 ;j<n ;j++){
            if(v[i]==v[j]){
                nveces++;
            }
            if(nveces>nvmaximo){
                nvmaximo=nveces;
            }
        }
    }
     return (nvmaximo>n/2);
}
 0
Author: PaperBirdMaster, 2016-10-16

3 answers

The error being presented is because float v; (a float variable) is being declared and the bool mayoritario(float v[],int n, int num) function is waiting for a float array variable (or Pointer to float to be more precise).

To solve the problem, the following must be added to the variable definition:

float v[]={0,1,2,2,2,2,0,1,2,2};

This fixes the problem.

 1
Author: Alejandro Montilla, 2016-11-08 21:06:16

The majority function should accept one more parameter that is the number to be evaluated and the entire array must be traversed to make the comparisons. Example:

#include <iostream>
using namespace std;

bool mayoritario(float [],int n, int num);
int n=10;
float v[10]={3,1,3,1,3,3,1,1,3,3};

int main()
{
    int num=3; //nĂºmero a evaluar
    cout<<"Evaluando el numero: "<<num;
    cout<<" -> El numero: "<<num;
    if (mayoritario(v,n,num)) cout<<" es MAYORITARIO";
    else cout<<" no es MAYORITARIO";
    return 0;
}

bool mayoritario(float v[],int n, int num){
    int i,nveces=0;
    for(int i=0 ;i<n ;i++){
        if(v[i]==num){
            nveces++;
        }
    }
    if(nveces>n/2)
       return true;
    else
       return false;
}

Output:

Evaluando el numero: 3 -> El numero: 3 es MAYORITARIO
 0
Author: SkyMaster, 2016-10-16 05:41:59

The quickest thing is to use a map to count the number of repetitions of each number. Then you go through that map and see if any numbers match:

bool mayoritario(float v[],int n){
  std::map<int,int> totales;
  for(int i=0; i<n;i++)
    totales[v[i]]++;

  const int limite_min = n/2;
  for(auto& par: totales) {
    if(par.second>limite_min)
      return true;
  }

  return false;
}

To do it without using containers, in simple plan, they are worrying too much about the performance of the algorithm, you could do something similar like this:

bool mayoritario(float v[],int n){
  for(int i=0 ;i<n;i++){
    const int valor=v[i];
    int total = 0;
    for( int j=i+1;j<n; j++)
      if( v[j]==valor) total++;

    if( total > n/2 )
      return true;
  }
  return false;
}

This last Code only tries to be illustrative and allows a lot of room for improvement.

I've written this from mobile, so I can't test if compile. if there are errata or not compiles let me know and I correct it.

A greeting.

 0
Author: eferion, 2016-10-16 19:11:21