help with mode of array

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;
		
		}
		
	
	}


  
Line 21 is an error; the size of the array is not known during compilation.

Function mode does not return value on all exit paths (a red herring, when is found neither true nor false?)

You have too much if and else.
result = -1
repeats = 1
for x in [0..size[
  count = std::count( arr+x, arr+size, arr[x] )
  if ( repeats < count )
    update repeats and result

return result

You will find that using consistent indentation and placement of the closing brace makes the code much easier to read.

Line 48: current is uninitialized the first time through the loop at line 44, so this could get set to anything. Technically it's undefined behavior so the program can do whatever it wants.

Line 57. Done starts uninitialized and then it can get set to true at line 54, but it's never set to false anywhere, so once it's set to true, it stays there.

I don't understand your logic.

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
int
mode(int arr[], int size)
{
    int bestIdx;		// index of best mode value seen
    int bestVal=0;		// best mode value seen
    bool tie = false;		// multiple values tie for best
    for (int i = 0; i < size; i++) {
	// Count how many times arr[i] occurs
	int count = 0;
	for (int o = 0; o < size; o++) {
	    if (arr[o] == arr[i]) {
		++count;
	    }
	}

	if (count > bestVal) {
	    // a new winner
	    bestVal = count;
	    bestIdx = i;
	    tie = false;
	} else if (count == bestVal && arr[i] != arr[i]) {
	    // found a tie
	    tie = true;
	}
    }

    return (tie ? -1 : arr[bestIdx]);
}

keskiverto writes:
Function mode does not return value on all exit paths

I don't think that's write. The original mode() function ends with:
1
2
3
4
5
6
	if(found==true){
		return arr[mode];
		}
	else if(found == false){
		return -1;
		}

found is either true or false, so one of these returns will execute.
Topic archived. No new replies allowed.