arrays and pointers...

closed account (zw6q5Di1)
Im supposed to write a program that has a function that accepts an array of integers and an integer that indicates


the main problem that I have with this problem is that I do not know how to find the mode, im trying to find a loop that finds the mode? help?!
Last edited on
hm, a few ideas came to my mind, but they all seem horribly inelegant.
1. make two new arrays, one storing the values and another storing their counts.
2. (probably easiest) for each index of the array, going through the array and finding counts. You'll also need to store the number with the highest count found so far and it's count.
3. sorting the array first, and then counting how many times a number appears in a row. You'll also need to keep track of the highest count and its corresponding number.
Last edited on
closed account (zw6q5Di1)
thanks for explaining, but i still dont know what you mean... could you give me an example?
1
2
3
4
5
6
7
8
9
10
11
12
int mode(int *array, int size) {
    int mode;
    count = 0;
    for (int *p = array; p < array + size; ++p) // for each value of array
            int count2 = std::count(array, array + size, *p;
            if (count2 > count) { // if it's count is larger than the highest count found so far
                 mode = *p; // store it's value
                 count = count2;  // and it's count
           }
        }
return mode;
}
Last edited on
To use pointers instead of arrays...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

int size, *values;

cout << "Size = ? ";
cin >> n;

values = new int[size];

for( int i=0; i<size; i++)
{
    cout << "values[" << i << "] = ? ";
    cin >> values[ i ];
}

// Now use values as if it were a normal array.


To calculate mode, you can follow the following approach.

1. Sort the array.

1
2
3
4
5
6
7
8
9
10

int compare (const void * a, const void * b)
{
    return ( *(int*)a - *(int*)b );
}

// qsort is available in stdlib.h. See this page http://www.cplusplus.com/reference/clibrary/cstdlib/qsort/

qsort(values, size, sizeof(int), compare);



2. Find out the most frequent element with a sigle scan. If there are 2 elements with the maxFrequency, then output -1 saying that there is no mode.

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


    int maxFrequency = -1;
    int mode = values[0];
    bool foundManyElementsWithMaxFrequency = false;
    
    for(int j=0; j<size; )
    {
        int cur = values[j];
        int localFrequency = 0;
        while(values[j] == cur) 
        {
           localFrequency++;
           j++;
        }
        
        if(localFrequency > maxFrequency)
        {
            maxFrequency = localFrequency;
            mode = cur;
            foundManyElementsWithMaxFrequency = false;
        }
        else if(localFrequency == maxFrequency)
        {
            foundManyElementsWithMaxFrequency = true;
        }
    }
    
    if( foundManyElementsWithMaxFrequency )
        mode = -1;
    
    cout << "\n Mode = " << mode;




Note: This method is O(n Log n). There may be an O(n) method. I am not very sure of it. If you don't know the O notation, then ignore it. It's a measure of how complex an algorithm is.
closed account (zw6q5Di1)
ok... i do not understand this, im not really experienced... could somebody give me the whole program? I tried msrams (the program right above) but I dont get it? could someone give it to me?
Topic archived. No new replies allowed.