Can't seem to figure out this mode function


Hello,

Well, its back to school for me and just as expected, another programming question I can't seem to vanquish. The gist of this little problem is to take this program that the professor made and edit one function (the nMode function) so that it correctly outputs the mode of each array.

I've been wracking my brain over this for about three hours, the approach I'm going for is to:

1st: go through the array and find out the highest frequency (how many times any one number appears in the array) that any number may have, this way I think I can find whether there will be a single mode, or multiple modes using this information

but of course, my simple skills have once again hindered me and I can't seem to get an accurate read on the frequency.

Could someone take a look and point me in a meaningful direction?

Here's the entire program, my work is in the nMode function definition.
(sorry for the bulky code)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177

//File:   Assignment 1 - Mode Problem
 

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;

int * copy_Array(int *,int);
int * mark_Sort(int *,int);
int * nMode(int *,int);//****Create This Function*****
int * fill_Array(int,int);
int * random_fill_Array(int,int);
void print_Array(string,int *,int,int);
void print_Modes(string eq,int * a,int pline);

int main(int argc, char** argv) {
        //Read in or set the size of the array and it's limit values
        int array_size=20,max_value=4,per_line=20;
        srand(static_cast<unsigned int>(time(0)));
        //Create a dynamic 1 dimensional array
        int *one_Dim=fill_Array(array_size,max_value);
        //Print out and examine before solving the problem
        //with this test case
        mark_Sort(one_Dim, array_size);
        print_Array("The Sequential Array = ",one_Dim,array_size,per_line);
        //Calculate the mode
        int * mode_Array=nMode(one_Dim,array_size);
        //Print the modes
        print_Modes("The mode  = ",mode_Array,per_line);
        //Now try a few other examples
        array_size=10;max_value=7;
        delete [] one_Dim;
        one_Dim=fill_Array(array_size,max_value);
        mark_Sort(one_Dim, array_size);
        print_Array("The Sequential Array = ",one_Dim,array_size,per_line);
        delete [] mode_Array;
        mode_Array=nMode(one_Dim,array_size);
        print_Modes("The mode  = ",mode_Array,per_line);
        //Another example with no modes
        array_size=7;max_value=6;
        delete [] one_Dim;
        one_Dim=fill_Array(array_size,max_value);
        mark_Sort(one_Dim, array_size);
        print_Array("The Sequential Array = ",one_Dim,array_size,per_line);
        delete [] mode_Array;
        mode_Array=nMode(one_Dim,array_size);
        print_Modes("The mode  = ",mode_Array,per_line);
        //Another example with a random fill
        array_size=7;max_value=6;
        delete [] one_Dim;
        one_Dim=random_fill_Array(array_size,max_value);
        mark_Sort(one_Dim, array_size);
        print_Array("The Random Array = ",one_Dim,array_size,per_line);
        delete [] mode_Array;
        mode_Array=nMode(one_Dim,array_size);
        print_Modes("The mode  = ",mode_Array,per_line);
        array_size=14;max_value=5;
        delete [] one_Dim;
        one_Dim=random_fill_Array(array_size,max_value);
        mark_Sort(one_Dim, array_size);
        print_Array("The Random Array = ",one_Dim,array_size,per_line);
        delete [] mode_Array;
        mode_Array=nMode(one_Dim,array_size);
        print_Modes("The mode  = ",mode_Array,per_line);
        //End of test
        delete [] one_Dim;
        delete [] mode_Array;
        return 1;
}




    //This is your homework.
    //Right now this function is just a stub for you
    //to get working
    int * nMode(int *a,int len){
        /////////////////////////////////////////////////////////////
        //This is what I've done so far, but I can't quite get the 
        //results im looking for, I seem to be overlooking something
        /////////////////////////////////////////////////////////////
        int possible_number_modes=0;
        int max_frequency = 0;
        int current_frequency = 1;
        
        for(int element=0;element<len;element++){

            if(a[element]==a[element+1]){
                current_frequency++;
            }

            if(current_frequency>max_frequency){
                max_frequency = current_frequency;
                current_frequency = 1;
            }
            
        }
        cout << "max frequency: " << max_frequency << endl;
        int *modes=new int [1]; // modes array
        modes[0]=0;
        /////////////////////////////////////////////////////////////
        return modes;
    }




    //Printout the area in set notation standard array
    //{0,1,2,3,etc....}
    void print_Array(string eq,int * a,int len,int pline){
        //Print as a set
        cout<<eq<<"{";
        for(int i=0;i<len-1;++i){
            cout<<a[i]<<",";
            if(i%pline==(pline-1))cout<<endl;
        }
        if(len>0)cout<<a[len-1]<<"}"<<endl;
        else cout<<"}"<<endl;
    }
    //Printout the area in set notation for modes array
    //{0,1,2,3,etc....}  Note, the first position contains
    //the number of modes, i.e. the size of the array.
    void print_Modes(string eq,int * a,int pline){
        //Print as a set
        cout<<eq<<"{";
        for(int i=1;i<a[0];++i){
            cout<<a[i]<<",";
            if(i%pline==(pline-1))cout<<endl;
        }
        if(a[0]>0)cout<<a[a[0]]<<"}"<<endl;
        else cout<<"}"<<endl;
    }
    //This function creates a dynamic 1 dimensional array
    //of any size.  It is filled with numbers that repeat
    //{0,1,2,...,modit,0,1,2,...,modit,...,0,1,2,...,modit,etc...}
    //This is the only way to really test the mode function
    int * random_fill_Array(int array_size,int modit){
        int * fill=new int[array_size];
        for(int i=0;i<array_size;++i){
            fill[i]=rand()%(modit+1);
        }
        return fill;
    }
    //This function creates a dynamic 1 dimensional array
    //of any size.  It is filled with numbers that repeat
    //{0,1,2,...,modit,0,1,2,...,modit,...,0,1,2,...,modit,etc...}
    //This is the only way to really test the mode function
    int * fill_Array(int array_size,int modit){
        int *fill=new int[array_size];
        for(int i=0;i<array_size;++i){
            fill[i]=i%(modit+1);
        }
        return fill;
    }

    int * mark_Sort(int *a,int len){
        for(int i=0;i<len-1;++i){
            for(int j=i+1;j<len;++j){
                if(a[i]>a[j]){
                    a[i]=a[i]^a[j];
                    a[j]=a[i]^a[j];
                    a[i]=a[i]^a[j];
                }
            }
        }
        return a;
    }
    int * copy_Array(int *a,int len){
        int * c = new int[len];
        for(int i=0;i<len;++i){
            c[i]=a[i];
        }
        return c;
    }
Line 95 seems to be not what you want.

You want to "reset" the current_frequency if the current element is not equal to the next element, right?

You are also going to run off the end of the array.

You loop from element = 0 to element < len, which would be correct if you only accessed a[ element ],
but by accessing a[ element + 1 ], you will attempt to access a[ len ], which is one past the end of
the array (since an array's indices go from 0 ... to len - 1.


Alright, thanks for the help. I'll see what I can do from here
I've seem to have made some good progress, I was able to find the maximum frequency and was able to determine the number of modes in the array so that I could set the new dynamic array (modes) to that size.

now the problem lies in me finding the actual modes themselves, I've thrown a few things at it, but I get nasty results each time. I don't think I'm accessing and assigning the new array correctly.

A little more help would be greatly appreciated.


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
//This is your homework.
    //Right now this function is just a stub for you
    //to get working
    int * nMode(int *a,int len){

        /////////////////////////////////////////////////////////////
        //Find the Highest Frequency of any one number
        /////////////////////////////////////////////////////////////
        int possible_number_modes=0;
        int max_frequency = 0;
        int current_frequency = 1;
        int mode_element = 0;
        
        for(int element=0;element<(len-1);element++){

            if(a[element]==a[(element+1)]){
                current_frequency++;
            }
            else if (a[element]!=a[element+1]){
                if(current_frequency>max_frequency){
                    max_frequency=current_frequency;
                }
                current_frequency=1;
            }
        }

        cout << "max frequency: " << max_frequency << endl;
        


        /////////////////////////////////////////////////////////////
        //use frequency to find number of modes
        /////////////////////////////////////////////////////////////
        for( int element=0;element<(len-1);element++){
            if(a[element]==a[element+1]){//if the arrays element is == to the next element in the array
                current_frequency++;
                if(current_frequency==max_frequency){//if current frequency is == to the max frequency
                    possible_number_modes++;
                }
            }
            else if(a[element]!=a[element+1]){//if the arrays element is not == to the next element in the array, reset the current frequency
                current_frequency=1;
            }
        }
        cout << "number of modes: " << possible_number_modes << endl;
        int *modes=new int [possible_number_modes]; // modes array set to how many modes there are
        
        

        //////////////////////////////////////////////////////////////////////////
        //now find the mode(s)   
        /////////////////////////////////////////////////////////////////////////
        for(int element=0;element<(len-1);element++){//going through array
            if(a[element]==a[element+1]){//if element is == to next element, current frequency ++
                current_frequency++;
                if(current_frequency==max_frequency){//if current frequency is == to the max frequency, then this element must be a mode
                    modes[mode_element]=a[element];//assign this element to the new modes array
                    if(mode_element<possible_number_modes){//if the current element in the mode array is < than the number of modes
                        mode_element++;//increment mode element so it advances in the mode array
                    }
                    
                }
            }
            else if(a[element]!=a[element+1]){
                current_frequency=1;
            }
        }
    
        




        return modes;
       
    }

Topic archived. No new replies allowed.