Majority number

0

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, where N is the number of vector elements

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);
}
    
asked by Ricardo Tovar 16.10.2016 в 04:13
source

3 answers

1

The error that is occurring is due to declaring float v; (a variable of type float) and the function bool mayoritario(float v[],int n, int num) is waiting for a variable array of float (or pointer to float to be more precise).

To solve the problem, you have to add the following to the definition of the variable:

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

This solves the problem.

    
answered by 08.11.2016 в 22:06
0

The majority function should accept one more parameter that is the number to be evaluated and the entire arrangement should 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;
}

Exit:

Evaluando el numero: 3 -> El numero: 3 es MAYORITARIO
    
answered by 16.10.2016 в 07:41
0

The fastest way is to use a map to count the number of repetitions of each number. Then we go through that map and see if any figure complies:

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 contenders, in a 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 admits a lot of room for improvement.

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

Greetings.

    
answered by 16.10.2016 в 20:46