this is what I'm trying to do
Write a function that accepts as arguments the following:
A) An array of integers
B) An integer that indicates the number of elements in the array
The function should determine the mode of the array. That is, it should determine
which value in the array occurs most often. The mode is the value the function should
return. If the array has no mode (none of the values occur more than once), the function
should return -1.
this is whats happening
if there is a single value that occurs in the array more than all other then everything is fine the function will return that value. However lets say I put in 20,12,3,3,11,28,14,19,22,3,23,13.
i will get back -1 as opposed to 3.
halppleesh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<ctime>
using namespace std;
int mode(int [],int);
int main(){
int size;
cout << "size" << endl;
cin >> size;
cin.ignore();
srand(time(0));
const int size1 = size;
int arr[size1];
for(int i =0;i<size1;i++){
cout << "fill array plz" << i+1 << endl;
cin >> arr[i];
cin.ignore();
//arr[i]=1+rand()%10;
//cout << arr[i] << endl;
}
cout << endl << endl << endl;
cout << mode(arr,size1) << endl;
system("pause");
return 0;
}
int mode(int arr[],int size){
int hold,mode,modecount=0,current;
bool found=false,done;
for(int i=0;i<size;i++){
if(i==0){mode=i; found=true;}
for(int o = 0;o<size;o++){
if(arr[o]==arr[i]){
current+=1;
}
}
for(int c=0;c < i;c++){
if(arr[c]==arr[i] && i!=0){
done=true;
}
}
if(done==true && current==modecount){
found = false;
}
else if(current>modecount){
mode = i;
found=true;}
else{
found=true;
}
if(!done){
cout << arr[i] << "-" << current << endl;
}
hold =current;
current=0;
modecount = hold;
}
cout << endl << endl << "------------------------" << endl;
if(found==true){
return arr[mode];
}
else if(found == false){
return -1;
}
}
|